Created
January 12, 2018 14:21
-
-
Save pietrom/aa22d4547320a84c1c5df7e16acedc49 to your computer and use it in GitHub Desktop.
Concurrent executer, for concurrency-related tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace TestingSupport { | |
public class ConcurrentExecutor { | |
public int ThreadCount { get; set; } = 50; | |
public int RepetitionsCount { get; set; } = 200; | |
public bool RandomDelayBetweenExecutions { get; set; } = true; | |
private readonly Random random = new Random(); | |
public void Execute(Action action) { | |
Barrier startBarrier = new Barrier(ThreadCount); | |
Barrier endBarrier = new Barrier(ThreadCount + 1); | |
for (int i = 0; i < ThreadCount; i++) { | |
new Thread(() => { | |
startBarrier.SignalAndWait(); | |
for (int j = 0; j < RepetitionsCount; j++) { | |
if (RandomDelayBetweenExecutions) { | |
Thread.Sleep(random.Next(10)); | |
} | |
action(); | |
} | |
endBarrier.SignalAndWait(); | |
}).Start(); | |
} | |
endBarrier.SignalAndWait(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment