C# 监测每个方法的执行次数和占用时间(测试1)
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个
原文:http://www.cnblogs.com/RicCC/archive/2010/03/15/castle-dynamic-proxy.html
代码:
using Castle.Core.Interceptor;
using Castle.DynamicProxy;
using ConsoleApplication1.test;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text; namespace ConsoleApplication2.AOP
{
class Class5
{
static void Main(string[] args)
{
ProxyGenerator generator = new ProxyGenerator();//代理
CallingLogInterceptor interceptor = new CallingLogInterceptor();//定义 拦截器
Class5_test1 entity = generator.CreateClassProxy<Class5_test1>(interceptor);
//SensorRecordService entity = generator.CreateClassProxy<SensorRecordService>(interceptor); DateTime beforDT1 = DateTime.Now;//开始时间
try
{
entity.test1();
//Console.WriteLine(entity.DealWithSensorRecord(74619, 87619).Replace("<br>", "\r\n"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} TimeSpan ts1 = DateTime.Now.Subtract(beforDT1);
Console.WriteLine($"总耗时:{ts1.TotalSeconds.ToString()}秒 或 {ts1.TotalMinutes.ToString("f3")}分"); MethodOperationInfo.Show(); Console.WriteLine("操作完成!");
Console.ReadLine();
} }
public class Class5_test1
{
public virtual void test1()
{
System.Threading.Thread.Sleep( * );
int num = ;
for (int i = ; i < ; i++)
{
num += ;
}
test1();
test1(, );
test1(, "");
}
public virtual void test1(int i)
{
}
public virtual void test1(int i, int j)
{
}
public virtual void test1(int i, string j)
{
}
} //拦截器
public class CallingLogInterceptor : IInterceptor
{
private DateTime dt { get; set; }
private TimeSpan ts { get; set; } //方法执行前
private void PreProceed(IInvocation invocation)
{
dt = DateTime.Now;
}
//方法执行后
private void PostProceed(IInvocation invocation)
{
ts = DateTime.Now - dt;
//Console.Write($"类名:{invocation.TargetType} 方法名:{invocation.Method.Name} 耗时:{ts.TotalMilliseconds}毫秒\r\n");
MethodOperationInfo.Add(invocation.Method, ts.TotalMilliseconds);
}
//拦截
public void Intercept(IInvocation invocation)
{
this.PreProceed(invocation);
invocation.Proceed();//调用
this.PostProceed(invocation);
}
} public class MethodOperationInfo
{
public string ClassName { get; set; }
public string MethodName { get; set; }
public double TotalMilliseconds { get; set; }
public int Num { get; set; } public static Dictionary<string, MethodOperationInfo> dic = new Dictionary<string, MethodOperationInfo>();
public static void Add(string MethodName, double TotalMilliseconds)
{
if (dic.ContainsKey(MethodName))
{
dic[MethodName].TotalMilliseconds += TotalMilliseconds;
dic[MethodName].Num += ;
}
else
{
dic.Add(MethodName, new MethodOperationInfo
{
MethodName = MethodName,
TotalMilliseconds = TotalMilliseconds,
Num =
});
}
}
public static void Add(MethodInfo mInfo, double TotalMilliseconds)
{
string MethodName = GetMethodNameHavePara(mInfo);
if (dic.ContainsKey(MethodName))
{
dic[MethodName].TotalMilliseconds += TotalMilliseconds;
dic[MethodName].Num += ;
}
else
{
dic.Add(MethodName, new MethodOperationInfo
{
MethodName = MethodName,
TotalMilliseconds = TotalMilliseconds,
Num =
});
}
} public static string GetMethodNameHavePara(MethodInfo mInfo)
{
string str = "";
//str += GetSameLenString(mInfo.ReflectedType.FullName, 50);//类名(含命名空间) var pInfos = mInfo.GetParameters();
str += mInfo.Name;
str += "(";
for (int j = ; j < pInfos.Length; j++)
{
var p = pInfos[j];
string pTypeName = $"{p.ParameterType.ToString()}, ";
if (p.ParameterType.IsGenericType && (p.ParameterType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
pTypeName = $"{Nullable.GetUnderlyingType(p.ParameterType).Name}?, ";
}
str += pTypeName;
}
str = str.TrimEnd(' ').TrimEnd(',');
str += ")"; return str;
}
public static string GetSameLenString(object obj, int len, bool afterFill = true)
{
string name = obj.ToString();
int count = len - name.Length; if (afterFill)
{
for (int i = ; i < count; i++)
{
name += " ";
}
return name; }
else
{
string value = "";
for (int i = ; i < count; i++)
{
value += " ";
}
value += name;
return value;
}
} public static void Show()
{
string str = "";
double TotalMilliseconds = ; foreach (var item in dic)
{
TotalMilliseconds += item.Value.TotalMilliseconds;
str += $"方法:{GetSameLenString(item.Key, 80)} ";
str += $"次数:{GetSameLenString(item.Value.Num, 10)} ";
str += $"耗时:{GetSameLenString(item.Value.TotalMilliseconds, 10, false) }毫秒 ";
str += $"\r\n";
} str += "\r\n";
str += "\r\n";
str += $"总耗时:{TotalMilliseconds}毫秒 ";
str += $"{TotalMilliseconds / 1000}秒 ";
str += $"{(TotalMilliseconds / 1000 / 60).ToString("f2")}分钟 ";
str += $"当前时间:{DateTime.Now} ";
str += "\r\n"; System.IO.File.WriteAllText("1.txt", str); Console.WriteLine("--------------------------\r\n\r\n");
Console.WriteLine(str);
} }
}
效果:

C# 监测每个方法的执行次数和占用时间(测试1)的更多相关文章
- C# 监测每个方法的执行次数和占用时间(测试3)
原文:http://www.cnblogs.com/RicCC/archive/2010/03/15/castle-dynamic-proxy.html 在Nuget引用 Castle.Dynamic ...
- C# 监测每个方法的执行次数和占用时间(测试4)
今天也要做这个功能,就百度一下,结果搜索到了自己的文章.一开始还没注意,当看到里面的一个注释的方法时,一开始还以为自己复制错了代码,结果仔细一看网页的文章,我去,原来是自己写的,写的确实不咋地. 把自 ...
- C# 监测每个方法的执行次数和占用时间(测试2)
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...
- C# 监测每个方法的执行次数和占用时间(测试5)
又找到了一个bug 测试的类: public class Class11_1 { public virtual List<int> test2_1(List<tb_SensorRec ...
- 事件之onTouch方法的执行过程 及和 onClick执行发生冲突的解决办法
转载:http://blog.csdn.net/jiangwei0910410003/article/details/17504315#quote 博主推荐: 风萧兮兮易水寒,“天真”一去兮不复还.如 ...
- Android中onTouch方法的执行过程以及和onClick执行发生冲突的解决办法
$*********************************************************************************************$ 博主推荐 ...
- ORACLE查看SQL的执行次数/频率
在ORACLE数据库应用调优中,一个SQL的执行次数/频率也是常常需要关注的,因为某个SQL执行太频繁,要么是由于应用设计有缺陷,需要在业务逻辑上做出优化处理,要么是业务特殊性所导致.如果执行频繁的S ...
- PLSQL_查询SQL的执行次数和频率(案例)
2014-12-25 Created By BaoXinjian
- PLSQL_监控有些SQL的执行次数和频率
原文:PLSQL_监控有些SQL的执行次数和频率 2014-12-25 Created By 鲍新建
随机推荐
- Google C++命令规范
最近发现自己在开发程序的过程中,经常会将好几种命名规范进行混用,这样使得程序的可读性下降,于是乎依然决定学习并使用Google的命令规范,并且坚持使用. copy from https://www.c ...
- Spark2.X分布式弹性数据集
跑一下这个结果 参考代码 package com.spark.test import org.apache.spark.sql.SparkSession import org.apache.spark ...
- C# JToken类的使用,实现解析动态json数据、遍历、查找
在原来解析json数据是,一般都是用反序列化来实现json数据的解读,这需要首先知道json数据的结构并且建立相应的类才能反序列化,一旦遇到动态的json数据,这种方法就不使用. 为了解决动态解析js ...
- css动画animation-keyframes
随着css3的流行,现在很多可以使用css3实现的动画效果,基本上就选择css3实现,尤其是在移动端的(移动端对css3的支持度相对比较高,PC端有很多IE8及以下的浏览器拖着后腿呢). 最近做了一个 ...
- 安全测试8_Web安全实战1(DVWA部署)
1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...
- (转)CSS3:nth-child()伪类选择器,奇偶数行自定义样式first-child
原文地址 Table表格奇偶数行定义样式: CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年,但 ...
- QT_QSlider的总结
当鼠标选中QSlider 上时,通过点击的数值为setpageStep():通过左右方向键按钮移动的数值为setsingleStep(). 鼠标滚轮上面两者都不行,不知道是什么原因! 应用: http ...
- vmware三种网络模式:桥接模式|仅主机|NAT模式
VMware 网络模式 1. Bridged(桥接)桥接模式下,宿主机物理网卡(主机网卡)和虚拟网卡通过 VMnet0 虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,物理网卡和虚拟网卡 ...
- hdfs线上修改 nameserivce
hdfs线上修改 nameserivce(ns1 修改为 ns2) 1.去core-site.xml.hdfs-site.xml 把ns1 -> ns2 同步所有节点 2.去journal 数据 ...
- MySQL 中 mysqld_safe 与 mysqld 区别,以及 mysqld_safe 的使用介绍
[mysqld_safe 与 mysqld 区别] 直接运行mysqld程序来启动MySQL服务的方法很少见 mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启 ...