There are times when we need to test our code in concurrent manner where there are some numbers of instances running simultaneously.
This is different with the invocation such as below code where the method will be executed in sequential manner, this kind of test may be useful to test how long it may take to execute the method.
for (int iTest = 0; iTest < 100; iTest++) |
For this testing concurrency, I’m using Joseph Albahari’s Threading in C# articles in ThreadPooling section as references to the MultiThreadRunner class that I created.
Code below in plain English : Submit 5 invocations of TestingMethodA with 10 maximum concurrent threads.
MultiThreadRunner runner = new MultiThreadRunner(); |
The MultiThreadRunner class :
class MultiThreadRunner /// <summary> // Adjust the min threads for (int i = 1; i <= iRow; i++) lock (workerLocker) ThreadPool.GetAvailableThreads(out iEndWorker, out iCompletion); /// <summary> ThreadPool.GetMaxThreads(out iMaxWorker, out iCompletion); if (concurrentNoOfInstances != 0) |
or get the complete project from here :
Hope this helps with your test as well :D