C# 监测每个方法的执行次数和占用时间(测试3)
原文:http://www.cnblogs.com/RicCC/archive/2010/03/15/castle-dynamic-proxy.html
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个
操作日志的作用:
可用来分析方法的执行效率
分析方法执行效率慢的原因
根据传入的参数测试,找到运行瓶颈问题
拦截器:
/// <summary>
/// 拦截器
/// </summary>
public class CallingLogInterceptor : IInterceptor
{
private DateTime dt { get; set; }
private TimeSpan ts { get; set; } /// <summary>
/// 方法执行前
/// </summary>
/// <param name="invocation"></param>
private void PreProceed(IInvocation invocation)
{
dt = DateTime.Now;
} /// <summary>
/// 方法执行后
/// </summary>
/// <param name="invocation"></param>
private void PostProceed(IInvocation invocation)
{
ts = DateTime.Now - dt;
MethodOperationInfo.Add(invocation, ts.TotalMilliseconds);
} /// <summary>
/// 拦截
/// </summary>
/// <param name="invocation"></param>
public void Intercept(IInvocation invocation)
{
this.PreProceed(invocation);
invocation.Proceed();//调用
this.PostProceed(invocation);
}
}
操作日志:
/// <summary>
/// 操作日志
/// </summary>
public class MethodOperationInfo
{
public string NameSpaceName { get; set; }
public string ClassName { get; set; }
public string MethodName { get; set; }
public string Parameters { get; set; }
public string ParameterTypes { get; set; }
public double TotalMilliseconds { get; set; }
public int ExecuteNumber { get; set; } public static List<MethodOperationInfo> list = new List<MethodOperationInfo>();//存放 详细信息
public static Dictionary<string, MethodOperationInfo> dic = new Dictionary<string, MethodOperationInfo>();//存放 统计信息 /// <summary>
/// 保证数据长度相同
/// </summary>
/// <param name="obj"></param>
/// <param name="len"></param>
/// <param name="afterFill">后填充/前填充</param>
/// <returns></returns>
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;
}
} /// <summary>
/// 获取方法的参数类型
/// 如:(System.String, System.Object, System.Int32)
/// </summary>
/// <param name="invocation"></param>
/// <returns></returns>
public static string GetParameterTypes(IInvocation invocation)
{
MethodInfo mInfo = invocation.Method;
ParameterInfo[] pInfos = mInfo.GetParameters(); string str = "";
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;
} /// <summary>
/// 获取方法的参数
/// </summary>
/// <param name="invocation"></param>
/// <returns></returns>
public static string GetParameter(IInvocation invocation)
{
string Parameters = "";//参数
if ((invocation.Arguments != null) && (invocation.Arguments.Length > ))
{
Parameters = JsonConvert.SerializeObject(invocation.Arguments);
}
return Parameters;
} /// <summary>
/// 添加信息
/// </summary>
/// <param name="invocation"></param>
/// <param name="TotalMilliseconds"></param>
public static void Add(IInvocation invocation, double TotalMilliseconds)
{
string NameSpaceName = invocation.TargetType.Namespace;
string ClassName = invocation.TargetType.Name;
string MethodName = invocation.Method.Name;//方法名
string ParameterTypes = GetParameterTypes(invocation);
string Parameters = GetParameter(invocation);//参数 //添加到 list
var model = new MethodOperationInfo
{
NameSpaceName = NameSpaceName,
ClassName = ClassName,
MethodName = MethodName,
ParameterTypes = ParameterTypes,
Parameters = Parameters,
TotalMilliseconds = TotalMilliseconds,
ExecuteNumber =
};
list.Add(model); //添加到 dictionary
string key = MethodName + ParameterTypes;
if (dic.ContainsKey(key))
{
dic[key].TotalMilliseconds += TotalMilliseconds;
dic[key].ExecuteNumber += ;
}
else
{
dic.Add(key, model.MemberwiseClone() as MethodOperationInfo);
}
} /// <summary>
/// 显示日志
/// </summary>
/// <param name="ShowDetailRecord">是否显示详情记录-比较占用时间</param>
/// <param name="IsFilter">是否开启过滤</param>
public static void Show(bool ShowDetailRecord = true, bool IsFilter = false)
{
StringBuilder sb = new StringBuilder();
DateTime beforDT = DateTime.Now;
List<string> list_Show_Method = new List<string>() { "GetSingle_Value1" };//可改为配置参数 //每个方法-耗时
string str = "";
double TotalMilliseconds = ;
foreach (var item in dic)
{
TotalMilliseconds += item.Value.TotalMilliseconds; str += $"命名空间:{GetSameLenString(item.Value.NameSpaceName, 40)} ";
str += $"类名:{GetSameLenString(item.Value.ClassName, 30)} ";
str += $"方法:{GetSameLenString(item.Key, 80)} ";
str += $"次数:{GetSameLenString(item.Value.ExecuteNumber, 10)} ";
str += $"耗时:{GetSameLenString(item.Value.TotalMilliseconds, 10, false) }毫秒 ";
str += $"\r\n";
}
sb.Append(str + "\r\n\r\n"); //方法-总耗时
str = "";
str += $"所有方法-耗时:{TotalMilliseconds}毫秒 ";
str += $"{TotalMilliseconds / 1000}秒 ";
str += $"{(TotalMilliseconds / 1000 / 60).ToString("f2")}分钟 ";
str += $"当前时间:{DateTime.Now} ";
sb.Insert(, str + "\r\n\r\n"); //方法每次-耗时
if (ShowDetailRecord)
{
for (int i = ; i < list.Count; i++)
{
Console.WriteLine($"处理数据-当前行:{list.Count - i}");
var item = list[i]; //数据过滤
if (IsFilter && !list_Show_Method.Contains(item.MethodName))
{
continue;
} sb.Append($"命名空间:{GetSameLenString(item.NameSpaceName, 40)} ");
sb.Append($"类名:{GetSameLenString(item.ClassName, 30)} ");
sb.Append($"方法:{GetSameLenString(item.MethodName + item.ParameterTypes, 80)} ");
sb.Append($"次数:{GetSameLenString(item.ExecuteNumber, 10)} ");
sb.Append($"耗时:{GetSameLenString(item.TotalMilliseconds, 10, false) }毫秒 ");
sb.Append($"参数:{GetSameLenString(item.Parameters, 50)} ");
sb.Append($"\r\n");
}
} //计算日志-耗时
sb.Insert(, $"计算日志-耗时:{DateTime.Now.Subtract(beforDT).TotalSeconds.ToString("#0.00000")}秒 \r\n\r\n"); System.IO.File.WriteAllText($"LOG_{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.txt", sb.ToString());
Console.WriteLine("完成!");
} }
测试类:
/// <summary>
/// 测试类1
/// </summary>
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) { }
}
入口:
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();
//entity.DealWithSensorRecord(74619, 174619);
//int start = Convert.ToInt32(GetAppSetting("start_Id"));
//int end = Convert.ToInt32(GetAppSetting("end_Id"));
//for (int i = start; i < end;)
//{
// int start_Id = i;
// i = i + 10000;
// int end_Id = i > end ? end : i;
// Console.WriteLine($"开始计算:start_Id:{start_Id} end_Id:{end_Id}");
// entity.DealWithSensorRecord(start_Id, end_Id);
//} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
MethodOperationInfo.Show(); TimeSpan ts = DateTime.Now.Subtract(beforDT1);
string str = $"总共耗时:{ts.TotalSeconds.ToString()}秒 或 {ts.TotalMinutes.ToString("f3")}分";
System.IO.File.AppendAllText("1.txt", $"{str}\r\n\r\n"); Console.WriteLine(str);
Console.WriteLine("操作完成!");
Console.ReadLine();
} /// <summary>
/// 获取 配置文件信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetAppSetting(string key)
{
return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[key].Value;
}
}
会往Debug写入一个日志文件。
预览效果1:


预览效果2:


预览效果3:(删掉了命名空间和类名)

C# 监测每个方法的执行次数和占用时间(测试3)的更多相关文章
- C# 监测每个方法的执行次数和占用时间(测试4)
今天也要做这个功能,就百度一下,结果搜索到了自己的文章.一开始还没注意,当看到里面的一个注释的方法时,一开始还以为自己复制错了代码,结果仔细一看网页的文章,我去,原来是自己写的,写的确实不咋地. 把自 ...
- C# 监测每个方法的执行次数和占用时间(测试2)
在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...
- C# 监测每个方法的执行次数和占用时间(测试1)
在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 鲍新建
随机推荐
- virsh详解
来个表情包表达我此时的心情 man virsh virsh [options]... [<command_string>] virsh [options]... <command&g ...
- es6(12)--类,对象
//类,对象 { //基本定义和生成实例 class Parent{ //定义构造函数 constructor(name='QQQ'){ this.name=name; } } let v_paren ...
- [PHP-DI] 理解依赖注入
理解依赖注入 依赖注入 和 依赖注入容器 是不同的: 依赖注入 (Dependency injection) 是编写更好代码的一种方法 容器 (Container) 是帮助注入依赖关系的工具 你不需要 ...
- 20165205 学习基础与C语言基础调查
学习基础和C语言基础调查 从<做中学>学到的经验 首先,在老师的这几篇文章中,最核心的一片文章就是<做中学>这一篇了,在文章中强调了不断联系的重要性,然后在学以致用的过程中发现 ...
- UVA-10020-贪心
题意:给你一些数轴上的线段,要求寻找出某些线段能够完全覆盖[0,M],并且取的线段数目最小. 解题思路: 贪心思路, 1.每个线段都有一个L和R,代表它的起点和终点,对于所有R <= 0 , ...
- mysql错误:Column count doesn't match value count at row 1
mysql错误:Column count doesn't match value count at row 1 mysql错误:Column count doesn't match value cou ...
- Python学习笔记_week1
一.编程语言的分类 编译型和解释型.静态语言和动态语言.强类型定义语言和弱类型定义语言,Python是一门动态解释型的强类型定义语言. Python的优缺点 Python解释器:CPython.IPy ...
- MyBatis与Hibernate的区别?
1.MyBatis学习成本低,Hibernate学习成本高: 2.MyBatis程序员编写SQL,Hibernate自动生成SQL:前者灵活及可优化高,后者不灵活及可优化低: 3.MyBatis适合需 ...
- 1. ibatis 查询的sql列存在相同的列名
如果SQL语句存在两个相同的查询列名,则映射时,取第一个列名的值进行映射 <?xml version="1.0" encoding="UTF-8" ?&g ...
- Python基础2 列表 字典 集合
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...