How do you want to count the number of packets per second that a server program can handle? The answer is to use the total number of packets processed, divided by the time it takes to process the packets. To point out here, the running of a program, the use of CPU time, and the actual running time is not the same. The bylaws are as follows:
private void ShowRunTime()
{
TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime;
Stopwatch stw = new Stopwatch();
stw.Start();
int Circles = 1000;
for (int i = 0; i < Circles; ++i)
{
Console.WriteLine(i.ToString());
}
double Msecs = Process.GetCurrentProcess().TotalProcessorTime.Subtract(ts1).TotalMilliseconds;
stw.Stop();
Console.WriteLine(string.Format("循环次数:{0} CPU时间(毫秒)={1} 实际时间(毫秒)={2}", Circles, Msecs, stw.Elapsed.TotalMilliseconds, stw.ElapsedTicks));
Console.WriteLine(string.Format("1 tick = {0}毫秒", stw.Elapsed.TotalMilliseconds / stw.Elapsed.Ticks));
}
The program output is as follows:
Cycles: 1000 CPU time (milliseconds) =50.072 actual time (ms) =666.9071
1 tick = 0.0001 ms
As can be seen in this example, the gap between the two is large, for the following reasons:
1 Windows is a multitasking operating system that allocates CPU time polling on a per-thread basis. That is, a program running halfway, may be stripped of CPU resources for other programs to run.
2 The program itself will not occupy CPU time waiting process. This wait may be the initiative of our program, such as starting a process, and then waiting for the end of the process, or we may not be aware of, such as the Console.WriteLine method of example, guess that its internal a series of asynchronous I/O operations and then wait for the completion of the operation, This does not occupy the CPU time of the calling process, but it consumes a lot of latency.
Summarize:
1 performance measurement, should use the program running time to measure, of course, also need to use CPU time as a reference, if the gap between the two is very large, need to consider why this situation.
2). NET Stopwatch class can be accurate to 1/10000 milliseconds, the basic can meet the measurement accuracy.