root/as3/Profiler/src/Profiler.as

リビジョン 385, 4.8 kB (コミッタ: jinten_ken, コミット時期: 4 年 前)

FlashPlayer?のバージョンチェック機構を追加。

Line 
1 /**
2  * Licensed under the MIT License
3  *
4  * Copyright (c) 2008 jinten.net
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  */
25 package  {
26         import flash.sampler.NewObjectSample;
27         import flash.utils.Dictionary;
28         import net.jinten.debug.utils.DebugUtils;
29        
30         /**
31          * Profilerの機能を使用するための基本クラスです。 アプリケーションに対して、1つのインスタンスしか生成できません。
32          * @author ken - Jinten.net
33          * @version 0.2
34          */
35         public class Profiler {
36                
37                 //----- Variables -----//
38                
39                 private static var _instance:Profiler = null;
40                 /**
41                  * Profilerインスタンスです。初めてこのプロパティを呼んだときにインスタンスが生成され、プロファイルが開始されます。
42                  * ただし、プロファイルを行うためには、バージョン9,0,115,0以上のDebugプレイヤで再生されている必要があります。
43                  * アプリケーションを再生しているFlashPlayerが条件を満たしていない場合はnullを返します。
44                  */
45                 public static function get instance():Profiler {
46                         if (_instance == null && DebugUtils.isProfilerEnabled()) {
47                                 _instance = new Profiler(new SingletonEssence);
48                         }
49                         return _instance;
50                 }
51                
52                 private var _running:Boolean = false;
53                 /**
54                  * 監視中か否かのフラグです。
55                  */
56                 public function get running():Boolean {
57                         return _running;
58                 }
59                
60                 //----- Private Variables -----//
61                
62                 private static var _dict:Dictionary = new Dictionary();
63                
64                
65                 //----- methods -----//
66                
67                 /**
68                  * コンストラクタ
69                  * 直接インスタンスを生成することはできません。
70                  * instanceプロパティを参照してください。
71                  */
72                 public function Profiler(essense:SingletonEssence) {
73                         if (essense == null) {
74                                 throw Error('use Profiler.instance');
75                         }
76                         startSampling();
77                 }
78                
79                 /**
80                  * メモリの監視を開始します。
81                  * @return
82                  */
83                 public function startSampling():void {
84                         _running = true;
85                         flash.sampler.startSampling();
86                 }
87                
88                 /**
89                  * メモリの監視を一時的に停止します。
90                  * @return
91                  */     
92                 public function pauseSampling():void {
93                         _running = false;
94                         flash.sampler.pauseSampling();
95                 }
96                
97                 /**
98                  * メモリの監視を完全に停止します。(現在監視中のデータも破棄されます)
99                  * @return
100                  */
101                 public function stopSampling():void {
102                         _running = false;
103                         flash.sampler.stopSampling();
104                 }
105                
106                 /**
107                  * startSampling()メソッド以後に生成されたインスタンスについて解析した結果をStringで返します。
108                  * @return 解析結果
109                  */
110                 public function getWatchString():String {
111                         pauseSampling();
112                         watch();
113                        
114                         var str:String = '';
115                         for (var type:String in _dict) {
116                                 var d:Dictionary = _dict[type] as Dictionary;
117                                 var num:int = 0;
118                                 for (var exist_object:* in d) {
119                                         num++;
120                                 }
121                                 str += type + ':' + num + '\n';
122                         }
123                         startSampling();
124                         return str;
125                 }
126                
127                 /**
128                  * @private
129                  * startSampling()メソッド以後に生成されたインスタンスについて解析します。
130                  */
131                 private function watch():void {
132                         //pauseSampling();
133                         var samples:Object = flash.sampler.getSamples();
134                        
135                         for each(var sample:Object in samples) {
136                                 if (sample is NewObjectSample) {
137                                         var nos:NewObjectSample = NewObjectSample(sample);
138                                         var type:String = String(nos.type);
139                                         if (nos.object != undefined) {
140                                                 if (!(_dict[type] is Dictionary)) {
141                                                         _dict[type] = new Dictionary(true);
142                                                 }
143                                                 _dict[type][nos.object] = null;
144                                         }
145                                 }
146                         }
147                         flash.sampler.clearSamples();
148                         //startSampling();
149                 }
150         }
151 }
152
153 class SingletonEssence { }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。