Skip to content

Commit

Permalink
Сбор статистики фоновыми заданиями
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBeaver committed Oct 12, 2023
1 parent 5f6ef53 commit 2d2de40
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 223 deletions.
14 changes: 0 additions & 14 deletions src/ScriptEngine.HostedScript/HostedScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class HostedScriptEngine : IDisposable
private bool _isInitialized;

private readonly OneScriptLibraryOptions _workingConfig;

private CodeStatProcessor _codeStat;

public HostedScriptEngine(ScriptingEngine engine)
{
Expand Down Expand Up @@ -143,22 +141,10 @@ private Process InitProcess(IHostApplication host, IExecutableModule module)
var process = new Process(host, module, _engine);
return process;
}

public void EnableCodeStatistics()
{
_codeStat = new CodeStatProcessor();
_engine.SetCodeStatisticsCollector(_codeStat);
}

public CodeStatDataCollection GetCodeStatData()
{
return _codeStat.GetStatData();
}

public void Dispose()
{
_engine?.Dispose();
_codeStat?.EndCodeStat();
}
}
}
184 changes: 90 additions & 94 deletions src/ScriptEngine/Machine/CodeStat/CodeStatProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,94 +1,90 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/

using System.Diagnostics;
using System.Collections.Generic;

namespace ScriptEngine.Machine
{
public class CodeStatProcessor : ICodeStatCollector
{
private Dictionary<CodeStatEntry, int> _codeStat = new Dictionary<CodeStatEntry, int>();
private Dictionary<CodeStatEntry, Stopwatch> _watchers = new Dictionary<CodeStatEntry, Stopwatch>();
private Stopwatch _activeStopwatch = null;
private HashSet<string> _preparedScripts = new HashSet<string>();

public CodeStatProcessor()
{
}

public bool IsPrepared(string ScriptFileName)
{
return _preparedScripts.Contains(ScriptFileName);
}

public void MarkEntryReached(CodeStatEntry entry, int count = 1)
{
int oldValue = 0;
_codeStat.TryGetValue(entry, out oldValue);
_codeStat[entry] = oldValue + count;

if (count == 0)
{
if (!_watchers.ContainsKey(entry))
{
_watchers.Add(entry, new Stopwatch());
}
}
else
{
_activeStopwatch?.Stop();
_activeStopwatch = _watchers[entry];
_activeStopwatch.Start();
}
}

public void MarkPrepared(string scriptFileName)
{
_preparedScripts.Add(scriptFileName);
}

public CodeStatDataCollection GetStatData()
{
CodeStatDataCollection data = new CodeStatDataCollection();
foreach (var item in _codeStat)
{
if (!IsPrepared(item.Key.ScriptFileName))
{
continue;
}
data.Add(new CodeStatData(item.Key, _watchers[item.Key].ElapsedMilliseconds, item.Value));
}

return data;
}

public void EndCodeStat()
{
_activeStopwatch?.Stop();
}

public void StopWatch(CodeStatEntry entry)
{
if (_watchers.ContainsKey(entry))
{
_watchers[entry].Stop();
}
}

public void ResumeWatch(CodeStatEntry entry)
{
_activeStopwatch?.Stop();

if (_watchers.ContainsKey(entry))
{
_activeStopwatch = _watchers[entry];
_activeStopwatch.Start();
}
}
}
}
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/

using System.Diagnostics;
using System.Collections.Generic;

namespace ScriptEngine.Machine
{
public class CodeStatProcessor : ICodeStatCollector
{
private Dictionary<CodeStatEntry, int> _codeStat = new Dictionary<CodeStatEntry, int>();
private Dictionary<CodeStatEntry, Stopwatch> _watchers = new Dictionary<CodeStatEntry, Stopwatch>();
private Stopwatch _activeStopwatch = null;
private HashSet<string> _preparedScripts = new HashSet<string>();

public bool IsPrepared(string ScriptFileName)
{
return _preparedScripts.Contains(ScriptFileName);
}

public void MarkEntryReached(CodeStatEntry entry, int count = 1)
{
int oldValue = 0;
_codeStat.TryGetValue(entry, out oldValue);
_codeStat[entry] = oldValue + count;

if (count == 0)
{
if (!_watchers.ContainsKey(entry))
{
_watchers.Add(entry, new Stopwatch());
}
}
else
{
_activeStopwatch?.Stop();
_activeStopwatch = _watchers[entry];
_activeStopwatch.Start();
}
}

public void MarkPrepared(string scriptFileName)
{
_preparedScripts.Add(scriptFileName);
}

public CodeStatDataCollection GetStatData()
{
CodeStatDataCollection data = new CodeStatDataCollection();
foreach (var item in _codeStat)
{
if (!IsPrepared(item.Key.ScriptFileName))
{
continue;
}
data.Add(new CodeStatData(item.Key, _watchers[item.Key].ElapsedMilliseconds, item.Value));
}

return data;
}

public void EndCodeStat()
{
_activeStopwatch?.Stop();
}

public void StopWatch(CodeStatEntry entry)
{
if (_watchers.ContainsKey(entry))
{
_watchers[entry].Stop();
}
}

public void ResumeWatch(CodeStatEntry entry)
{
_activeStopwatch?.Stop();

if (_watchers.ContainsKey(entry))
{
_activeStopwatch = _watchers[entry];
_activeStopwatch.Start();
}
}
}
}
8 changes: 1 addition & 7 deletions src/ScriptEngine/Machine/MachineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public void SetMemory(ExecutionContext memory)
ContextsAttached();

_mem = memory;
_codeStatCollector = _mem.Services.TryResolve<ICodeStatCollector>();
}

internal MachineStoredState SaveState()
Expand All @@ -107,7 +108,6 @@ internal MachineStoredState SaveState()
CallStack = _callStack,
OperationStack = _operationStack,
StopManager = _stopManager,
CodeStatCollector = _codeStatCollector,
Memory = _mem
};
}
Expand All @@ -120,7 +120,6 @@ internal void RestoreState(MachineStoredState state)
_callStack = state.CallStack;
_exceptionsStack = state.ExceptionsStack;
_stopManager = state.StopManager;
_codeStatCollector = state.CodeStatCollector;
_mem = state.Memory;

SetFrame(_callStack.Peek());
Expand Down Expand Up @@ -571,11 +570,6 @@ private bool ShouldRethrowException(ScriptException exc)
return false;
}

public void SetCodeStatisticsCollector(ICodeStatCollector collector)
{
_codeStatCollector = collector;
}

private CodeStatEntry CurrentCodeEntry()
{
return new CodeStatEntry(CurrentScript?.Source, _currentFrame.MethodName, _currentFrame.LineNumber);
Expand Down
9 changes: 6 additions & 3 deletions src/ScriptEngine/ScriptingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void AttachExternalAssembly(System.Reflection.Assembly asm)
public void Initialize()
{
SetDefaultEnvironmentIfNeeded();

EnableCodeStatistics();
UpdateContexts();

_attachedScriptsFactory = new AttachedScriptsFactory(this);
Expand Down Expand Up @@ -186,10 +186,13 @@ private set
}
}

public void SetCodeStatisticsCollector(ICodeStatCollector collector)
private void EnableCodeStatistics()
{
var collector = Services.TryResolve<ICodeStatCollector>();
if (collector == default)
return;

ProduceExtraCode |= CodeGenerationFlags.CodeStatistics;
MachineInstance.Current.SetCodeStatisticsCollector(collector);
}

#region IDisposable Members
Expand Down
Loading

0 comments on commit 2d2de40

Please sign in to comment.