Flash / Actionscript CPU profiler

后端 未结 9 774
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 03:06

Have you found such a tool and used it successfully?

9条回答
  •  既然无缘
    2020-12-14 03:50

    I was also looking for a profiler for AS, but I wanted an freeware/open source solution that works with FlashDevelop and Flex SDK. I found none. So I wrote a simple python script and an even simpler AS class. The script essentially takes any AS file and adds profiling code (i.e. calls to measure the total runtime of that function with an accuracy of 1 ms - the resolution of the flash.utils.getTimer() call) to each function definition. The script sometimes makes mistakes, but these are usually easy to fix by hand. Then you need to add one more line manually: dump the profiling statistics somewhere at some point. This method is obviously far from accurate, but it nonetheless gives you good feel of bottlenecks in your code. I used it for a 100k file with success.

    Here is the AS class:

    package  {
        public class Profiler {
            private static var instance:Profiler;
    
            public static function get profiler():Profiler {
                if (!Profiler.instance) Profiler.instance = new Profiler;
                return Profiler.instance;
            }
    
            private var data:Object = {};
    
            public function profile(fn:String, dur:int):void {
                if (!data.hasOwnProperty(fn)) data[fn] = new Number(0);
                data[fn] += dur / 1000.0;
            }
    
            public function clear():void {
                data = { };
            }
    
            public function get stats():String {
                var st:String = "";
                for (var fn:String in data) {
                    st += fn + ":\t" + data[fn] + "\n";
                }
                return st;
            }
        }
    }
    

    And here is the python script that does the trick:

    import sre, sys
    
    rePOI = sre.compile(r'''\bclass\b|\bfunction\b|\breturn\b|["'/{}]''')
    reFun = sre.compile(r'\bfunction\b\s*((?:[gs]et\s+)?\w*)\s*\(')
    reCls = sre.compile(r'class\s+(\w+)[\s{]')
    reStr = sre.compile(r'''(["'/]).*?(? 0 and stack[-1]['depth'] == depth:
                    lastf = stack.pop()
                    line = "Profiler.profiler.profile('" + lastf['name'] + \
                        "', flash.utils.getTimer() - __start__);\n"
                    body = body[:pos] + line + body[pos:]
                    endpos += len(line)
    
            pos = endpos
            match = rePOI.search(body, pos)
        return body
    
    def main():
        if len(sys.argv) >= 2: inf = open(sys.argv[1], 'rU')
        else: inf = sys.stdin
        if len(sys.argv) >= 3: outf = open(sys.argv[2], 'wU')
        else: outf = sys.stdout
        outf.write(addProfilingCalls(inf.read()))
        inf.close()
        outf.close()
    
    if __name__ == "__main__":
        main()
    

    Feel free to use, distribute and modify both.

提交回复
热议问题