一个简单的性能计数器
2009-05-13 13:39
218 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
public interface IAction
{
void Action();
}
public static class CodeTimer
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
public delegate void ActionDelegate();
private static long GetCurrentThreadTimes()
{
long l;
long kernelTime, userTimer;
GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
out userTimer);
return kernelTime + userTimer;
}
static CodeTimer()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
public static void Time(string name, int iteration, ActionDelegate action)
{
if (String.IsNullOrEmpty(name))
{
return;
}
if (action == null)
{
return;
}
//1. Print name
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2. Record the latest GC counts
//GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.Collect(GC.MaxGeneration);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3. Run action
Stopwatch watch = new Stopwatch();
watch.Start();
long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
for (int i = 0; i < iteration; i++) action();
long ticks = GetCurrentThreadTimes() - ticksFst;
watch.Stop();
// 4. Print CPU
Console.ForegroundColor = currentForeColor;
Console.WriteLine("/tTime Elapsed:/t/t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("/tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
Console.WriteLine("/tCPU time:/t/t" + (ticks * 100).ToString("N0")
+ "ns");
Console.WriteLine("/tCPU time (one time):/t" + (ticks * 100 /
iteration).ToString("N0") + "ns");
// 5. Print GC
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("/tGen " + i + ": /t/t/t" + count);
}
Console.WriteLine();
}
public static void Time(string name, int iteration, IAction action)
{
if (String.IsNullOrEmpty(name))
{
return;
}
if (action == null)
{
return;
}
//1. Print name
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2. Record the latest GC counts
//GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.Collect(GC.MaxGeneration);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3. Run action
Stopwatch watch = new Stopwatch();
watch.Start();
long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
for (int i = 0; i < iteration; i++) action.Action();
long ticks = GetCurrentThreadTimes() - ticksFst;
watch.Stop();
// 4. Print CPU
Console.ForegroundColor = currentForeColor;
Console.WriteLine("/tTime Elapsed:/t/t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("/tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
Console.WriteLine("/tCPU time:/t/t" + (ticks * 100).ToString("N0")
+ "ns");
Console.WriteLine("/tCPU time (one time):/t" + (ticks * 100 /
iteration).ToString("N0") + "ns");
// 5. Print GC
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("/tGen " + i + ": /t/t/t" + count);
}
Console.WriteLine();
}
}
测试类
public class TestSleep3000 : IAction
{
#region IAction Members
public void Action()
{
Thread.Sleep(3000);
}
#endregion
}
public class TestEmptyMethod : IAction
{
#region IAction Members
public void Action()
{
}
#endregion
}
public class TestStringConcat : IAction
{
string s = "";
#region IAction Members
public void Action()
{
s += "a";
}
#endregion
}
public class TestStringBuilderConcat : IAction
{
StringBuilder s = new StringBuilder();
#region IAction Members
public void Action()
{
s.Append ("a");
}
#endregion
}
测试代码
采用接口
CodeTimer.Time("Thread Sleep", 1, new TestSleep3000());
CodeTimer.Time("Thread Sleep", 10000000, new TestEmptyMethod());
CodeTimer.Time("String Concat", 100000, new TestStringConcat());
CodeTimer.Time("StringBuilder Conca", 100000,
new TestStringBuilderConcat());
测试结果
Thread Sleep
Time Elapsed: 2,997ms
Time Elapsed (one time):2,997ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
Empty Method
Time Elapsed: 138ms
Time Elapsed (one time):0ms
CPU time: 125,000,000ns
CPU time (one time): 12ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
String Concat
Time Elapsed: 10,547ms
Time Elapsed (one time):0ms
CPU time: 10,546,875,000ns
CPU time (one time): 105,468ns
Gen 0: 4102
Gen 1: 2661
Gen 2: 2545
StringBuilder Conca
Time Elapsed: 4ms
Time Elapsed (one time):0ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
采用委托
CodeTimer.Time("Thread Sleep", 1, delegate() { Thread.Sleep(3000); });
CodeTimer.Time("Empty Method", 10000000, delegate() { });
string a = "";
CodeTimer.Time("String Concat", 100000, delegate() { a += "a"; });
StringBuilder s = new StringBuilder();
CodeTimer.Time("StringBuilder Conca", 100000, delegate() { s.Append("a"); });
测试结果
Thread Sleep
Time Elapsed: 2,989ms
Time Elapsed (one time):2,989ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
Empty Method
Time Elapsed: 156ms
Time Elapsed (one time):0ms
CPU time: 156,250,000ns
CPU time (one time): 15ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
String Concat
Time Elapsed: 10,425ms
Time Elapsed (one time):0ms
CPU time: 10,406,250,000ns
CPU time (one time): 104,062ns
Gen 0: 4102
Gen 1: 2661
Gen 2: 2545
StringBuilder Conca
Time Elapsed: 4ms
Time Elapsed (one time):0ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
public interface IAction
{
void Action();
}
public static class CodeTimer
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
out long lpExitTime, out long lpKernelTime, out long lpUserTime);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
public delegate void ActionDelegate();
private static long GetCurrentThreadTimes()
{
long l;
long kernelTime, userTimer;
GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
out userTimer);
return kernelTime + userTimer;
}
static CodeTimer()
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
public static void Time(string name, int iteration, ActionDelegate action)
{
if (String.IsNullOrEmpty(name))
{
return;
}
if (action == null)
{
return;
}
//1. Print name
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2. Record the latest GC counts
//GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.Collect(GC.MaxGeneration);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3. Run action
Stopwatch watch = new Stopwatch();
watch.Start();
long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
for (int i = 0; i < iteration; i++) action();
long ticks = GetCurrentThreadTimes() - ticksFst;
watch.Stop();
// 4. Print CPU
Console.ForegroundColor = currentForeColor;
Console.WriteLine("/tTime Elapsed:/t/t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("/tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
Console.WriteLine("/tCPU time:/t/t" + (ticks * 100).ToString("N0")
+ "ns");
Console.WriteLine("/tCPU time (one time):/t" + (ticks * 100 /
iteration).ToString("N0") + "ns");
// 5. Print GC
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("/tGen " + i + ": /t/t/t" + count);
}
Console.WriteLine();
}
public static void Time(string name, int iteration, IAction action)
{
if (String.IsNullOrEmpty(name))
{
return;
}
if (action == null)
{
return;
}
//1. Print name
ConsoleColor currentForeColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(name);
// 2. Record the latest GC counts
//GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.Collect(GC.MaxGeneration);
int[] gcCounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
}
// 3. Run action
Stopwatch watch = new Stopwatch();
watch.Start();
long ticksFst = GetCurrentThreadTimes(); //100 nanosecond one tick
for (int i = 0; i < iteration; i++) action.Action();
long ticks = GetCurrentThreadTimes() - ticksFst;
watch.Stop();
// 4. Print CPU
Console.ForegroundColor = currentForeColor;
Console.WriteLine("/tTime Elapsed:/t/t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms");
Console.WriteLine("/tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms");
Console.WriteLine("/tCPU time:/t/t" + (ticks * 100).ToString("N0")
+ "ns");
Console.WriteLine("/tCPU time (one time):/t" + (ticks * 100 /
iteration).ToString("N0") + "ns");
// 5. Print GC
for (int i = 0; i <= GC.MaxGeneration; i++)
{
int count = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine("/tGen " + i + ": /t/t/t" + count);
}
Console.WriteLine();
}
}
测试类
public class TestSleep3000 : IAction
{
#region IAction Members
public void Action()
{
Thread.Sleep(3000);
}
#endregion
}
public class TestEmptyMethod : IAction
{
#region IAction Members
public void Action()
{
}
#endregion
}
public class TestStringConcat : IAction
{
string s = "";
#region IAction Members
public void Action()
{
s += "a";
}
#endregion
}
public class TestStringBuilderConcat : IAction
{
StringBuilder s = new StringBuilder();
#region IAction Members
public void Action()
{
s.Append ("a");
}
#endregion
}
测试代码
采用接口
CodeTimer.Time("Thread Sleep", 1, new TestSleep3000());
CodeTimer.Time("Thread Sleep", 10000000, new TestEmptyMethod());
CodeTimer.Time("String Concat", 100000, new TestStringConcat());
CodeTimer.Time("StringBuilder Conca", 100000,
new TestStringBuilderConcat());
测试结果
Thread Sleep
Time Elapsed: 2,997ms
Time Elapsed (one time):2,997ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
Empty Method
Time Elapsed: 138ms
Time Elapsed (one time):0ms
CPU time: 125,000,000ns
CPU time (one time): 12ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
String Concat
Time Elapsed: 10,547ms
Time Elapsed (one time):0ms
CPU time: 10,546,875,000ns
CPU time (one time): 105,468ns
Gen 0: 4102
Gen 1: 2661
Gen 2: 2545
StringBuilder Conca
Time Elapsed: 4ms
Time Elapsed (one time):0ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
采用委托
CodeTimer.Time("Thread Sleep", 1, delegate() { Thread.Sleep(3000); });
CodeTimer.Time("Empty Method", 10000000, delegate() { });
string a = "";
CodeTimer.Time("String Concat", 100000, delegate() { a += "a"; });
StringBuilder s = new StringBuilder();
CodeTimer.Time("StringBuilder Conca", 100000, delegate() { s.Append("a"); });
测试结果
Thread Sleep
Time Elapsed: 2,989ms
Time Elapsed (one time):2,989ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
Empty Method
Time Elapsed: 156ms
Time Elapsed (one time):0ms
CPU time: 156,250,000ns
CPU time (one time): 15ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
String Concat
Time Elapsed: 10,425ms
Time Elapsed (one time):0ms
CPU time: 10,406,250,000ns
CPU time (one time): 104,062ns
Gen 0: 4102
Gen 1: 2661
Gen 2: 2545
StringBuilder Conca
Time Elapsed: 4ms
Time Elapsed (one time):0ms
CPU time: 0ns
CPU time (one time): 0ns
Gen 0: 0
Gen 1: 0
Gen 2: 0
相关文章推荐
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器:CodeTimer
- 简单的创建一个性能计数器
- 一个简单的性能计数器:CodeTimer
- 通过编码方式使用性能计数器来进行性能计数的一个简单例子 - ZT
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器:CodeTimer
- 一个简单的性能计数器
- 一个简单的性能计数器:CodeTimer
- 对老赵写的简单性能计数器的修改
- 三、JMeter基础之----- 一个简单的性能测试
- 一个简单的计数器程序
- 一个简单看安卓性能的方法
- 一个高效的C++性能计数器模板
- JMeter基础之一个简单的性能测试
- JMeter基础之一 一个简单的性能测试
- 关于sqlserver、access、mysql数据库性能的一个简单测试