-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChaosScriptRunner.cs
76 lines (69 loc) · 2.19 KB
/
ChaosScriptRunner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Collections.Generic;
using ChaosLoad.Models;
using System.Threading;
using System.Linq;
using ChaosLoad.PlatformLoaders;
namespace ChaosLoad
{
public class ChaosScriptRunner
{
public bool Finished { get; set; } = false;
private int runningThreadCount;
private bool allThreadsStarted = false;
private readonly object lck = new object();
private readonly IEnumerable<IPlatformLoader> loaders;
public ChaosScriptRunner(IEnumerable<IPlatformLoader> loaders)
{
this.loaders = loaders;
}
public void Run(ChaosScript chaosScript)
{
foreach (var template in chaosScript.Templates)
{
var commandText = System.IO.File.ReadAllText(template.ScriptPath);
for (var i = 0; i < template.Threads; i++)
{
var templateThread = new Thread(() => RunPlatformLoader(
chaosScript.ConnectionString,
chaosScript.Type,
commandText,
template.RunCount,
template.Sleep)
);
templateThread.Start();
IncrementThreeadCount();
}
}
allThreadsStarted = true;
}
private void RunPlatformLoader(string connectionString, TemplateType type, string commandText, int repeat, int sleep)
{
var loader = loaders.Single(x => x.HandlesType == type);
loader.RunTask(
connectionString,
commandText,
repeat,
() => DecrementThreeadCount(),
sleep
);
}
private void IncrementThreeadCount()
{
lock (lck)
{
runningThreadCount += 1;
}
}
private void DecrementThreeadCount()
{
lock (lck)
{
runningThreadCount -= 1;
if (allThreadsStarted && runningThreadCount == 0)
{
Finished = true;
}
}
}
}
}