Skip to content

Commit 2d2de40

Browse files
committed
Сбор статистики фоновыми заданиями
1 parent 5f6ef53 commit 2d2de40

File tree

5 files changed

+208
-223
lines changed

5 files changed

+208
-223
lines changed

src/ScriptEngine.HostedScript/HostedScriptEngine.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public class HostedScriptEngine : IDisposable
2727
private bool _isInitialized;
2828

2929
private readonly OneScriptLibraryOptions _workingConfig;
30-
31-
private CodeStatProcessor _codeStat;
3230

3331
public HostedScriptEngine(ScriptingEngine engine)
3432
{
@@ -143,22 +141,10 @@ private Process InitProcess(IHostApplication host, IExecutableModule module)
143141
var process = new Process(host, module, _engine);
144142
return process;
145143
}
146-
147-
public void EnableCodeStatistics()
148-
{
149-
_codeStat = new CodeStatProcessor();
150-
_engine.SetCodeStatisticsCollector(_codeStat);
151-
}
152-
153-
public CodeStatDataCollection GetCodeStatData()
154-
{
155-
return _codeStat.GetStatData();
156-
}
157144

158145
public void Dispose()
159146
{
160147
_engine?.Dispose();
161-
_codeStat?.EndCodeStat();
162148
}
163149
}
164150
}
Lines changed: 90 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,90 @@
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+
}

src/ScriptEngine/Machine/MachineInstance.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void SetMemory(ExecutionContext memory)
9696
ContextsAttached();
9797

9898
_mem = memory;
99+
_codeStatCollector = _mem.Services.TryResolve<ICodeStatCollector>();
99100
}
100101

101102
internal MachineStoredState SaveState()
@@ -107,7 +108,6 @@ internal MachineStoredState SaveState()
107108
CallStack = _callStack,
108109
OperationStack = _operationStack,
109110
StopManager = _stopManager,
110-
CodeStatCollector = _codeStatCollector,
111111
Memory = _mem
112112
};
113113
}
@@ -120,7 +120,6 @@ internal void RestoreState(MachineStoredState state)
120120
_callStack = state.CallStack;
121121
_exceptionsStack = state.ExceptionsStack;
122122
_stopManager = state.StopManager;
123-
_codeStatCollector = state.CodeStatCollector;
124123
_mem = state.Memory;
125124

126125
SetFrame(_callStack.Peek());
@@ -571,11 +570,6 @@ private bool ShouldRethrowException(ScriptException exc)
571570
return false;
572571
}
573572

574-
public void SetCodeStatisticsCollector(ICodeStatCollector collector)
575-
{
576-
_codeStatCollector = collector;
577-
}
578-
579573
private CodeStatEntry CurrentCodeEntry()
580574
{
581575
return new CodeStatEntry(CurrentScript?.Source, _currentFrame.MethodName, _currentFrame.LineNumber);

src/ScriptEngine/ScriptingEngine.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void AttachExternalAssembly(System.Reflection.Assembly asm)
8383
public void Initialize()
8484
{
8585
SetDefaultEnvironmentIfNeeded();
86-
86+
EnableCodeStatistics();
8787
UpdateContexts();
8888

8989
_attachedScriptsFactory = new AttachedScriptsFactory(this);
@@ -186,10 +186,13 @@ private set
186186
}
187187
}
188188

189-
public void SetCodeStatisticsCollector(ICodeStatCollector collector)
189+
private void EnableCodeStatistics()
190190
{
191+
var collector = Services.TryResolve<ICodeStatCollector>();
192+
if (collector == default)
193+
return;
194+
191195
ProduceExtraCode |= CodeGenerationFlags.CodeStatistics;
192-
MachineInstance.Current.SetCodeStatisticsCollector(collector);
193196
}
194197

195198
#region IDisposable Members

0 commit comments

Comments
 (0)