|
1 | | -/*---------------------------------------------------------- |
2 | | -This Source Code Form is subject to the terms of the |
3 | | -Mozilla Public License, v.2.0. If a copy of the MPL |
4 | | -was not distributed with this file, You can obtain one |
5 | | -at http://mozilla.org/MPL/2.0/. |
6 | | -----------------------------------------------------------*/ |
7 | | - |
8 | | -using System.Diagnostics; |
9 | | -using System.Collections.Generic; |
10 | | - |
11 | | -namespace ScriptEngine.Machine |
12 | | -{ |
13 | | - public class CodeStatProcessor : ICodeStatCollector |
14 | | - { |
15 | | - private Dictionary<CodeStatEntry, int> _codeStat = new Dictionary<CodeStatEntry, int>(); |
16 | | - private Dictionary<CodeStatEntry, Stopwatch> _watchers = new Dictionary<CodeStatEntry, Stopwatch>(); |
17 | | - private Stopwatch _activeStopwatch = null; |
18 | | - private HashSet<string> _preparedScripts = new HashSet<string>(); |
19 | | - |
20 | | - public CodeStatProcessor() |
21 | | - { |
22 | | - } |
23 | | - |
24 | | - public bool IsPrepared(string ScriptFileName) |
25 | | - { |
26 | | - return _preparedScripts.Contains(ScriptFileName); |
27 | | - } |
28 | | - |
29 | | - public void MarkEntryReached(CodeStatEntry entry, int count = 1) |
30 | | - { |
31 | | - int oldValue = 0; |
32 | | - _codeStat.TryGetValue(entry, out oldValue); |
33 | | - _codeStat[entry] = oldValue + count; |
34 | | - |
35 | | - if (count == 0) |
36 | | - { |
37 | | - if (!_watchers.ContainsKey(entry)) |
38 | | - { |
39 | | - _watchers.Add(entry, new Stopwatch()); |
40 | | - } |
41 | | - } |
42 | | - else |
43 | | - { |
44 | | - _activeStopwatch?.Stop(); |
45 | | - _activeStopwatch = _watchers[entry]; |
46 | | - _activeStopwatch.Start(); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - public void MarkPrepared(string scriptFileName) |
51 | | - { |
52 | | - _preparedScripts.Add(scriptFileName); |
53 | | - } |
54 | | - |
55 | | - public CodeStatDataCollection GetStatData() |
56 | | - { |
57 | | - CodeStatDataCollection data = new CodeStatDataCollection(); |
58 | | - foreach (var item in _codeStat) |
59 | | - { |
60 | | - if (!IsPrepared(item.Key.ScriptFileName)) |
61 | | - { |
62 | | - continue; |
63 | | - } |
64 | | - data.Add(new CodeStatData(item.Key, _watchers[item.Key].ElapsedMilliseconds, item.Value)); |
65 | | - } |
66 | | - |
67 | | - return data; |
68 | | - } |
69 | | - |
70 | | - public void EndCodeStat() |
71 | | - { |
72 | | - _activeStopwatch?.Stop(); |
73 | | - } |
74 | | - |
75 | | - public void StopWatch(CodeStatEntry entry) |
76 | | - { |
77 | | - if (_watchers.ContainsKey(entry)) |
78 | | - { |
79 | | - _watchers[entry].Stop(); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - public void ResumeWatch(CodeStatEntry entry) |
84 | | - { |
85 | | - _activeStopwatch?.Stop(); |
86 | | - |
87 | | - if (_watchers.ContainsKey(entry)) |
88 | | - { |
89 | | - _activeStopwatch = _watchers[entry]; |
90 | | - _activeStopwatch.Start(); |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | -} |
| 1 | +/*---------------------------------------------------------- |
| 2 | +This Source Code Form is subject to the terms of the |
| 3 | +Mozilla Public License, v.2.0. If a copy of the MPL |
| 4 | +was not distributed with this file, You can obtain one |
| 5 | +at http://mozilla.org/MPL/2.0/. |
| 6 | +----------------------------------------------------------*/ |
| 7 | + |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Collections.Generic; |
| 10 | + |
| 11 | +namespace ScriptEngine.Machine |
| 12 | +{ |
| 13 | + public class CodeStatProcessor : ICodeStatCollector |
| 14 | + { |
| 15 | + private Dictionary<CodeStatEntry, int> _codeStat = new Dictionary<CodeStatEntry, int>(); |
| 16 | + private Dictionary<CodeStatEntry, Stopwatch> _watchers = new Dictionary<CodeStatEntry, Stopwatch>(); |
| 17 | + private Stopwatch _activeStopwatch = null; |
| 18 | + private HashSet<string> _preparedScripts = new HashSet<string>(); |
| 19 | + |
| 20 | + public bool IsPrepared(string ScriptFileName) |
| 21 | + { |
| 22 | + return _preparedScripts.Contains(ScriptFileName); |
| 23 | + } |
| 24 | + |
| 25 | + public void MarkEntryReached(CodeStatEntry entry, int count = 1) |
| 26 | + { |
| 27 | + int oldValue = 0; |
| 28 | + _codeStat.TryGetValue(entry, out oldValue); |
| 29 | + _codeStat[entry] = oldValue + count; |
| 30 | + |
| 31 | + if (count == 0) |
| 32 | + { |
| 33 | + if (!_watchers.ContainsKey(entry)) |
| 34 | + { |
| 35 | + _watchers.Add(entry, new Stopwatch()); |
| 36 | + } |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + _activeStopwatch?.Stop(); |
| 41 | + _activeStopwatch = _watchers[entry]; |
| 42 | + _activeStopwatch.Start(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void MarkPrepared(string scriptFileName) |
| 47 | + { |
| 48 | + _preparedScripts.Add(scriptFileName); |
| 49 | + } |
| 50 | + |
| 51 | + public CodeStatDataCollection GetStatData() |
| 52 | + { |
| 53 | + CodeStatDataCollection data = new CodeStatDataCollection(); |
| 54 | + foreach (var item in _codeStat) |
| 55 | + { |
| 56 | + if (!IsPrepared(item.Key.ScriptFileName)) |
| 57 | + { |
| 58 | + continue; |
| 59 | + } |
| 60 | + data.Add(new CodeStatData(item.Key, _watchers[item.Key].ElapsedMilliseconds, item.Value)); |
| 61 | + } |
| 62 | + |
| 63 | + return data; |
| 64 | + } |
| 65 | + |
| 66 | + public void EndCodeStat() |
| 67 | + { |
| 68 | + _activeStopwatch?.Stop(); |
| 69 | + } |
| 70 | + |
| 71 | + public void StopWatch(CodeStatEntry entry) |
| 72 | + { |
| 73 | + if (_watchers.ContainsKey(entry)) |
| 74 | + { |
| 75 | + _watchers[entry].Stop(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void ResumeWatch(CodeStatEntry entry) |
| 80 | + { |
| 81 | + _activeStopwatch?.Stop(); |
| 82 | + |
| 83 | + if (_watchers.ContainsKey(entry)) |
| 84 | + { |
| 85 | + _activeStopwatch = _watchers[entry]; |
| 86 | + _activeStopwatch.Start(); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments