您当前的位置: 首页 » asp.net编程学习
» CodeTimer .net下的性能计数器
CodeTimer .net下的性能计数器
一直在找一个简单的可以比较.net方法的执行性能的工具,在博客园上面,http://www.cnblogs.com/JeffreyZhao/archive/2009/03/10/codetimer.html这篇文章提供了一个不错的计数器,使用起来也十分方便,但我的环境是windows2003,所以只能用其他网友根据这个修改i的计数器,网址:http://www.cnblogs.com/eaglet/archive/2009/03/10/1407791.html
因为这个是winform的,所以我稍微改了下,改成WEB输出了,执行结果如下:
.gif)
完整代码如下:
using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
namespace MYORM.COMMON
{
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);
HttpContext.Current.Response.Write("<font color=\"blue\">"+name+"</font><br />");
// 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;
HttpContext.Current.Response.Write("全部耗时:\t\t" +watch.ElapsedMilliseconds.ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("执行一次耗时:" +(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms<br />");
// HttpContext.Current.Response.Write("CPU time:\t\t" + (ticks * 100).ToString("N0") + "ns<br />");
// HttpContext.Current.Response.Write("CPU time (one time):\t" + (ticks * 100 / iteration).ToString("N0") + "ns<br />");
// 5. Print GC
//for (int i = 0; i <= GC.MaxGeneration; i++)
//{
// int count = GC.CollectionCount(i) - gcCounts[i];
// HttpContext.Current.Response.Write("\tGen " + i + ": \t\t\t" + count + "<br />");
//}
HttpContext.Current.Response.Write("<br /><br />");
}
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;
HttpContext.Current.Response.Write(name+"<br />");
// 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;
HttpContext.Current.Response.Write("\tTime Elapsed:\t\t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("\tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("\tCPU time:\t\t" + (ticks * 100).ToString("N0") + "ns<br />");
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];
HttpContext.Current.Response.Write("\tGen " + i + ": \t\t\t" + count+"<br />");
}
}
}
}
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
namespace MYORM.COMMON
{
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);
HttpContext.Current.Response.Write("<font color=\"blue\">"+name+"</font><br />");
// 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;
HttpContext.Current.Response.Write("全部耗时:\t\t" +watch.ElapsedMilliseconds.ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("执行一次耗时:" +(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms<br />");
// HttpContext.Current.Response.Write("CPU time:\t\t" + (ticks * 100).ToString("N0") + "ns<br />");
// HttpContext.Current.Response.Write("CPU time (one time):\t" + (ticks * 100 / iteration).ToString("N0") + "ns<br />");
// 5. Print GC
//for (int i = 0; i <= GC.MaxGeneration; i++)
//{
// int count = GC.CollectionCount(i) - gcCounts[i];
// HttpContext.Current.Response.Write("\tGen " + i + ": \t\t\t" + count + "<br />");
//}
HttpContext.Current.Response.Write("<br /><br />");
}
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;
HttpContext.Current.Response.Write(name+"<br />");
// 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;
HttpContext.Current.Response.Write("\tTime Elapsed:\t\t" +
watch.ElapsedMilliseconds.ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("\tTime Elapsed (one time):" +
(watch.ElapsedMilliseconds / iteration).ToString("N0") + "ms<br />");
HttpContext.Current.Response.Write("\tCPU time:\t\t" + (ticks * 100).ToString("N0") + "ns<br />");
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];
HttpContext.Current.Response.Write("\tGen " + i + ": \t\t\t" + count+"<br />");
}
}
}
}
使用如下(GetUser和GetUserBySql是两个方法):
MYORM.COMMON.CodeTimer.Time("MYORM", 10000, () => { GetUser(); });
MYORM.COMMON.CodeTimer.Time("SQL", 10000, () => { GetUserBySql(); });
MYORM.COMMON.CodeTimer.Time("SQL", 10000, () => { GetUserBySql(); });
上一篇:没有文章了
名字:
全部评论:
loading...
申明:本站部分文章来自网络,由于各种原因对文章的来源无从考究,如果您是“
CodeTimer .net下的性能计数器
”的原作者,若侵犯您的版权,请与我联系!联系方法:email:ahuinan@21cn.com QQ:106494262
文章档案
- 作者:阿会楠
- 来源:搜索吧
- 日期:2012/1/13 11:09:00
- 点击:loading...
网友投票(您觉得这篇文章怎样?)
请稍侯......
请稍侯......