Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The official MS Tool to stress your apps is ACT, however it's not ready to test enterprise applications, basically I missed two things
- Support to Client Certificates
- Support to .NET languages
So, my decission is to build my own Thread based tester, of course in c#. It's my first time I code using threads, so here is my first approach.
What I want is to call several times to the same method (without parameters, and returns void), the first step is to define a delegate like this:
public delegate void WorkerMethodDelegate();
Now I'm going to define a new class, that receives this delegate in the constructor:
public MultiThreadRunner(WorkerMethodDelegate m)
{
_method = m;
}
I need a public method "Run" that indicates how many threads should run this method:
public void Run(int times)
{
_threads = new Thread[times];
for (int i=0;i<times;i++)
{
_threads[i] = new Thread(new ThreadStart(_method));
_threads[i].Start();
}
}
To finish this class I need to know how long should I wait untill all my threads have finished, so I complete the Run method:
public void Run(int times)
{
_threads = new Thread[times];
for (int i=0;i<times;i++)
{
_threads[i] = new Thread(new ThreadStart(_method));
_threads[i].Start();
}
bool finished = false;
while (finished!=true)
{
finished = threadPoolFinished();
Thread.Sleep(500);
}
}
private bool threadPoolFinished()
{
bool result = true;
int numThreads = _threads.Length;
for (int i=0;i<numThreads;i++)
{
result &= ( _threads[i].ThreadState == ThreadState.Stopped );
}
return result;
}
And that's it !!
I don't know how comformant is this code, but IT JUST WORKS.
I'm waiting for your comments about how to improve this class, so here is the complete class.
using System;
using System.Threading;
namespace PerfTestRunner
{
public delegate void WorkerMethodDelegate();
public class MultiThreadRunner
{
WorkerMethodDelegate _method;
Thread[] _threads;
public MultiThreadRunner(WorkerMethodDelegate m)
{
_method = m;
}
public void Run(int times)
{
_threads = new Thread[times];
for (int i=0;i<times;i++)
{
_threads[i] = new Thread(new ThreadStart(_method));
_threads[i].Start();
}
bool finished = false;
while (finished!=true)
{
finished = threadPoolFinished();
Thread.Sleep(500);
}
}
private bool threadPoolFinished()
{
bool result = true;
int numThreads = _threads.Length;
for (int i=0;i<numThreads;i++)
{
result &= ( _threads[i].ThreadState == ThreadState.Stopped );
}
return result;
}
}
}
Comments
- Anonymous
June 08, 2009
PingBack from http://cellulitecreamsite.info/story.php?id=10460