skip to main |skip to sidebar

2008-01-13

[AS3]trace() 与 logging 的压力测试

trace() 其实很慢!以后再发布时,一定要注意去掉 trace()mx.logging.Target

源代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
scriptTimeLimit="5"
width="520" height="490">
<mx:Script>
<![CDATA[
import mx.logging.ILogger;
import mx.logging.Log;
import mx.logging.targets.TraceTarget;

private static const logger:ILogger = Log.getLogger("TraceTest");

private static const NONE:String = "无 trace()";
private static const TRACE:String = "有 trace()";
private static const LOGGER:String = "logger.debug()"

private var tt:TraceTarget;

private function test(testCase:String):void {
var loopCount:int = int(count_ns.value);
var start:int, now:int;
var i:int;
output("@" + testCase);
try {
switch (testCase) {
case NONE:
start = getTimer();
for (i = 0; i < loopCount; i++) {
"abcdefghijklmnopqrstuvwxyz";
}
now = getTimer();
break;
case TRACE:
start = getTimer();
for (i = 0; i < loopCount; i++) {
trace("abcdefghijklmnopqrstuvwxyz");
}
now = getTimer();
break;
case LOGGER:
start = getTimer();
for (i = 0; i < loopCount; i++) {
logger.debug("abcdefghijklmnopqrstuvwxyz");
}
now = getTimer();
break;
}
}
catch (e:Error) {
now = getTimer();
output(" " + i + "次时弹出错误: " + e);
}
var time:int = now - start;
output(" 经过时间(ms): " + nf.format(time));
output(" 1秒中的循环次数: " + nf.format(i * 1000 / time));
output("----------------------------------------");
}

private function updateTraceTarget():void {
if (!tt) {
tt = new TraceTarget();
tt.includeDate = false;
tt.includeTime = false;
tt.includeLevel = true;
tt.includeCategory = true;
}

if (traceTarget_ch.selected) {
Log.addTarget(tt);
} else {
Log.removeTarget(tt);
}
}

private function output(msg:String):void {
output_ta.text += msg + "\n";
}
]]>
</mx:Script>

<mx:NumberFormatter id="nf" precision="0" />
<mx:Panel title="trace()与logger.debug()的压力测试"
paddingTop="8" paddingRight="8" paddingBottom="8" paddingLeft="8">
<mx:HBox>
<mx:Label text="循环回数:" />
<mx:NumericStepper id="count_ns" value="1000000" maximum="10000000" />
</mx:HBox>
<mx:HBox>
<mx:Button label="{NONE}" click="test(NONE)" />
<mx:Button label="{TRACE}" click="test(TRACE)" />
<mx:Button label="{LOGGER}" click="test(LOGGER)" />
<mx:CheckBox id="traceTarget_ch" label="添加TraceTarget" change="updateTraceTarget()"/>
</mx:HBox>
<mx:TextArea id="output_ta" width="400" height="300" />
<mx:Label color="red" text="*Application.scriptTimeLimit设为了5秒。" />
</mx:Panel>
</mx:Application>

没有评论: