L
logdenav
Hello
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.
The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.
When the User received the callback from MyTimer, it restart a new
timer for 1 second.
It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.
With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!
Have you an idea ?
What will it be the best implemation to do that ?
Thanks
The c# program :
using System;
namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;
public MyTimer(int ms)
{
this.ms = ms;
}
public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}
private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}
}
class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;
private static object obj = new object();
private static int total = 0;
public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}
public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}
private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}
t.Set();
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user = new User(i, 1000);
user.start();
}
}
}
}
I'm testing the performance of the System.Timers.Timer class.
I created a small program that create 100 User objects.
Each USer object create a MyTimer object.
The constructor of the User class contains a name and a time to wait
for the timer.
Calling start on the User objet initiate the MyTimer to callback in 1
second.
When the User received the callback from MyTimer, it restart a new
timer for 1 second.
It works fine with 10 users but with 100 users and more, It seems that
few User callbacks
do not appears.
With 10 users, each second the total is : 10, 20, 30, 40, 50, ....
With 100 users the total is : 100, 200, 270, 365, .....
It's not regular !!!
Have you an idea ?
What will it be the best implemation to do that ?
Thanks
The c# program :
using System;
namespace TimersPerf
{
public delegate void MyTimerHandler (double delta);
class MyTimer
{
public event MyTimerHandler CallBack;
System.Timers.Timer t=null;
private DateTime startTime, endTime;
private int ms;
public MyTimer(int ms)
{
this.ms = ms;
}
public void Set()
{
if (t==null)
{
t = new System.Timers.Timer(ms);
t.AutoReset = false;
t.Elapsed+=new System.Timers.ElapsedEventHandler(t_Elapsed);
startTime = DateTime.Now;
t.Start();
}
}
private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs
e)
{
endTime = DateTime.Now;
TimeSpan delta = endTime-startTime;
t.Stop();
t.Dispose();
t = null;
CallBack(delta.TotalMilliseconds);
}
}
class User
{
private MyTimer t;
private int name;
private int ms;
private int cpt =0;
private static object obj = new object();
private static int total = 0;
public User(int name, int ms)
{
this.name = name;
this.ms = ms;
t = new MyTimer(ms);
}
public void start()
{
t.CallBack+=new MyTimerHandler(CallBack);
t.Set();
}
private void CallBack(double delta)
{
cpt++;
lock(obj)
{
total++;
Console.WriteLine("name={0}, delta={1}, cpt={2}, total={3}", name,
delta, cpt, total);
}
t.Set();
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
c.foo();
Console.ReadLine();
}
public void foo()
{
int count = 10;
User[] user = new User[count];
for(int i=0; i<count; i++)
{
user = new User(i, 1000);
user.start();
}
}
}
}