今天也要做这个功能,就百度一下,结果搜索到了自己的文章。一开始还没注意,当看到里面的一个注释的方法时,一开始还以为自己复制错了代码,结果仔细一看网页的文章,我去,原来是自己写的,写的确实不咋地。

把自己的文章的代码复制了下来,稍微改了一下,运行看了一下效果,然后仔细一看,计算的总时间不对,如下图:

上一篇文章的地址:https://www.cnblogs.com/guxingy/p/10142242.html

改了几个地方:

    /// <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; Console.WriteLine($"方法名称:{invocation.Method.Name}" );
Console.WriteLine($"开始时间:{dt.ToString("yyyy-MM-dd HH:mm:ss fff")}");
Console.WriteLine($"结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
Console.WriteLine($"所用时间:{ts.TotalSeconds}"); 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>
/// 测试类1
/// </summary>
public class Class5_test1
{
public virtual void test1()
{
test4();
test3();
test2();
}
public virtual void test2()
{
System.Threading.Thread.Sleep();
}
public virtual void test3()
{
System.Threading.Thread.Sleep();
}
public virtual void test4()
{
System.Threading.Thread.Sleep();
}
}
    public class MyProxyGenerator
{
/// <summary>
/// 创建一个代理对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateProxy<T>() where T : class
{
ProxyGenerator generator = new ProxyGenerator();//代理
CallingLogInterceptor interceptor = new CallingLogInterceptor();//定义 拦截器
T entity = generator.CreateClassProxy<T>(interceptor);
return entity;
}
}
    public class Class2
{
public static void test1()
{
Class5_test1 entity = MyProxyGenerator.CreateProxy<Class5_test1>();
entity.test1();
MethodOperationInfo.ShowContainsDetail(); Console.Read();
}
}

说明:所有的测试,只是修改了一下Class5_test1 这个类给

测试1

    /// <summary>
/// 测试类1
/// </summary>
public class Class5_test1
{
public virtual void test1()
{
test2();
test3();
test4();
}
public virtual void test2()
{
System.Threading.Thread.Sleep();
}
public virtual void test3()
{
System.Threading.Thread.Sleep();
}
public virtual void test4()
{
System.Threading.Thread.Sleep();
}
}

问题:这里test1调用了test2、test3、test4,那么执行test1所用时间,应该是test2、test3、test4所用时间的总和,正确所用时间至少应该是6秒(1+2+3),结果这里输出的是3秒多,肯定不对。

测试2

    /// <summary>
/// 测试类1
/// </summary>
public class Class5_test1
{
public virtual void test1()
{
test4();
test3();
test2();
}
public virtual void test2()
{
System.Threading.Thread.Sleep();
}
public virtual void test3()
{
System.Threading.Thread.Sleep();
}
public virtual void test4()
{
System.Threading.Thread.Sleep();
}
}

问题:这里的test1方法的所用时间是1秒多,更不对了。

结论:

  测试1 和 测试2 的区别,只是在test1里面改变了test2、test3、test4的执行顺序,结果时间却迥然不同,更奇怪的是,test1的所用时间,更是莫名的接近调用的倒数第二个方法。

  测试1里面,test1的所用时间,很接近test4。

  测试2里面,test1的所用时间,很接近test2。

  反正就是所用时间,很接近 方法体里面 最后一个 调用的方法,原因找到了再补充,了解一下动态代理的原理或许就知道了,也获取是自己写错了,估计是第一种可能

乘着自己好奇,又测试了一下,看下面哦

    /// <summary>
/// 测试类1
/// </summary>
public class Class5_test1
{
public virtual void test1()
{
Console.WriteLine("开始执行test1");
test4();
test3();
test2();
Console.WriteLine("执行完成test1");
}
public virtual void test2()
{
System.Threading.Thread.Sleep();
}
public virtual void test3()
{
System.Threading.Thread.Sleep();
}
public virtual void test4()
{
System.Threading.Thread.Sleep();
}
}

主要是是在,test1方法里面添加了方法的执行情况, 输出 开始执行 和 执行完成。

找到原因了:拦截器对象只创建了一个,所以dt一直都是最后调用的那个方法的开始时间

      还是自己写的代码有问题啊

赋上 改好的全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using Castle.Core.Interceptor;
using Castle.DynamicProxy;
using System.Reflection;
using Newtonsoft.Json;
using System.Security.Cryptography; namespace ConsoleApplication3
{
//原文:https://www.cnblogs.com/guxingy/p/10142242.html public class Class2
{
public static void test1()
{
Class5_test1 entity = MyProxyGenerator.CreateProxy<Class5_test1>();
entity.test1();
MethodOperationInfo.ShowContainsDetail();
}
} public class MyProxyGenerator
{
/// <summary>
/// 创建一个代理对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T CreateProxy<T>() where T : class
{
ProxyGenerator generator = new ProxyGenerator();//代理
CallingLogInterceptor interceptor = new CallingLogInterceptor();//定义 拦截器
T entity = generator.CreateClassProxy<T>(interceptor);
return entity;
}
} /// <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;
//Console.WriteLine($"开始执行,方法名称:{invocation.Method.Name},开始时间:{dt.ToString("yyyy-MM-dd HH:mm:ss fff")}");
//Console.WriteLine(); MethodOperationInfo.SetStartTime(invocation);
} /// <summary>
/// 方法执行后
/// </summary>
/// <param name="invocation"></param>
private void PostProceed(IInvocation invocation)
{
//通用标识找到之前创建的那个对象,并记录结束时间 //ts = DateTime.Now - dt;
//Console.WriteLine($"执行完成,方法名称:{invocation.Method.Name}");
//Console.WriteLine($" 开始时间:{dt.ToString("yyyy-MM-dd HH:mm:ss fff")}");
//Console.WriteLine($" 结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}");
//Console.WriteLine($" 所用时间:{ts.TotalSeconds}");
//Console.WriteLine(); MethodOperationInfo.SetEndTime(invocation);
} /// <summary>
/// 拦截
/// </summary>
/// <param name="invocation"></param>
public void Intercept(IInvocation invocation)
{
this.PreProceed(invocation);
invocation.Proceed();//调用
this.PostProceed(invocation);
}
} /// <summary>
/// 扩展
/// </summary>
public static class IntExtension
{
public static string ToString(this object obj, int len, bool afterFill = true)
{
string name = obj.ToString();
//int count = len - name.Length;//不能用这个 汉字和英文占用的长度不同
int count = len - System.Text.Encoding.Default.GetBytes(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>
/// 操作日志
/// </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 string Name { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public bool IsEnd { get; set; }//标识这个对象是否完成计算耗时 /// <summary>
/// 构造函数
/// </summary>
/// <param name="invocation"></param>
public MethodOperationInfo(IInvocation invocation)
{
StartTime = DateTime.Now;
NameSpaceName = invocation.TargetType.Namespace;
ClassName = invocation.TargetType.Name;
MethodName = invocation.Method.Name;//方法名
ParameterTypes = GetParameterTypes(invocation);//参数类型
Parameters = GetParameter(invocation);//参数
Name = GetName(invocation);
IsEnd = false;
} /// <summary>
/// 操作完成
/// </summary>
/// <param name="invocation"></param>
public void OperationFinish(IInvocation invocation)
{
EndTime = DateTime.Now;
TotalMilliseconds = (EndTime - StartTime).TotalMilliseconds;
IsEnd = true;
} //存放 所有的 详细信息
public static List<MethodOperationInfo> operationInfos = new List<MethodOperationInfo>(); /// <summary>
/// 设置开始时间
/// </summary>
/// <param name="invocation"></param>
public static void SetStartTime(IInvocation invocation)
{
MethodOperationInfo model = new MethodOperationInfo(invocation);
operationInfos.Add(model);
} /// <summary>
/// 设置结束时间
/// </summary>
/// <param name="invocation"></param>
public static void SetEndTime(IInvocation invocation)
{
MethodOperationInfo model = GetModel(invocation);
model.OperationFinish(invocation);
} /// <summary>
/// 获取一个对象
/// </summary>
/// <param name="invocation"></param>
/// <returns></returns>
public static MethodOperationInfo GetModel(IInvocation invocation)
{
string name = GetName(invocation);
return operationInfos.Where(c => (c.Name == name) && !c.IsEnd).FirstOrDefault();
} /// <summary>
/// 获取 这个方法 唯一的编号,针对方法而已
/// </summary>
/// <param name="invocation"></param>
/// <returns></returns>
public static string GetName(IInvocation invocation)
{
string NameSpaceName = invocation.TargetType.Namespace;
string ClassName = invocation.TargetType.Name;
string MethodName = invocation.Method.Name;//方法名
string ParameterTypes = GetParameterTypes(invocation);
string Parameters = GetParameter(invocation);//参数 string strId = $"{NameSpaceName},{ClassName},{MethodName},{ParameterTypes}"; //MD5加密
byte[] result = Encoding.Default.GetBytes(strId);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
return BitConverter.ToString(output).Replace("-", "");
} /// <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="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_AllMethod = ; //去重
var operationInfos_NotRepeat = operationInfos.GroupBy(c => c.Name).Select(c => c.First()).ToList(); //分组统计
operationInfos.GroupBy(c => c.Name).Select(c => new
{
Name = c.Key,
ExecuteNumber = c.Count(),
TotalMilliseconds = c.Sum(a => a.TotalMilliseconds)
}).ToList().ForEach(c =>
{
var item = operationInfos_NotRepeat.Where(x => x.Name == c.Name).First();
totalMilliseconds_AllMethod += item.TotalMilliseconds; str += $"命名空间:{item.NameSpaceName.ToString(40)} ";
str += $"类名:{item.ClassName.ToString(30)} ";
str += $"方法:{item.MethodName.ToString(80)} ";
str += $"次数:{c.ExecuteNumber.ToString(10)} ";
str += $"耗时:{c.TotalMilliseconds.ToString(10, false) }毫秒 ";
str += $"\r\n";
});
sb.Append(str + "\r\n\r\n"); //方法-总耗时
str = "";
str += $"所有方法-耗时:{totalMilliseconds_AllMethod}毫秒 ";
str += $"{totalMilliseconds_AllMethod / 1000}秒 ";
str += $"{(totalMilliseconds_AllMethod / 1000 / 60).ToString("f2")}分钟 ";
str += $"当前时间:{DateTime.Now} ";
sb.Insert(, str + "\r\n\r\n"); #region 方法 每次耗时的 详情信息 if (ShowDetailRecord)
{
for (int i = ; i < operationInfos.Count; i++)
{
Console.WriteLine($"处理数据-当前行:{operationInfos.Count - i}");
var item = operationInfos[i]; //数据过滤
if (IsFilter && !list_Show_Method.Contains(item.MethodName))
{
continue;
} sb.Append($"命名空间:{item.NameSpaceName.ToString(40)} ");
sb.Append($"类名:{item.ClassName.ToString(30)} ");
sb.Append($"方法:{item.MethodName + item.ParameterTypes.ToString(80)} ");
sb.Append($"耗时:{item.TotalMilliseconds.ToString(10, false) }毫秒 ");
sb.Append($"参数:{item.Parameters.ToString(50)} ");
sb.Append($"\r\n");
}
}
#endregion //计算日志-耗时
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>
/// 显示日志 包含详情数据
/// </summary>
public static void ShowContainsDetail()
{
Show(true);
} /// <summary>
/// 显示日志 不包含详情数据
/// </summary>
public static void ShowNotContainsDetail()
{
Show(false);
}
} /// <summary>
/// 测试类1
/// </summary>
public class Class5_test1
{
public virtual void test1()
{
Console.WriteLine("主方法-test1-开始执行");
Console.WriteLine(); for (int i = ; i < ; i++)
{
test4();
}
for (int i = ; i < ; i++)
{
test3();
}
for (int i = ; i < ; i++)
{
test2();
} Console.WriteLine("主方法-test1-执行完成");
Console.WriteLine();
}
public virtual void test2()
{
System.Threading.Thread.Sleep();
}
public virtual void test3()
{
System.Threading.Thread.Sleep();
}
public virtual void test4()
{
System.Threading.Thread.Sleep();
}
} }

但是当要代理的那个的类,如果没有无参构造函数,这种方法就还是用不了,准备解决这个问题,解决了到时候再贴上解决方案。

本来是准备用到生产环境测试一下自己的代码的,结果,我去,有参构造函数,接口,多态,麻蛋都有啊,改好了,再继续贴代码

C# 监测每个方法的执行次数和占用时间(测试4)的更多相关文章

  1. C# 监测每个方法的执行次数和占用时间(测试3)

    原文:http://www.cnblogs.com/RicCC/archive/2010/03/15/castle-dynamic-proxy.html 在Nuget引用 Castle.Dynamic ...

  2. C# 监测每个方法的执行次数和占用时间(测试2)

    在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...

  3. C# 监测每个方法的执行次数和占用时间(测试1)

    在Nuget引用 Castle.DynamicProxy 和 Newtonsoft.Json 这个 原文:http://www.cnblogs.com/RicCC/archive/2010/03/15 ...

  4. C# 监测每个方法的执行次数和占用时间(测试5)

    又找到了一个bug 测试的类: public class Class11_1 { public virtual List<int> test2_1(List<tb_SensorRec ...

  5. 事件之onTouch方法的执行过程 及和 onClick执行发生冲突的解决办法

    转载:http://blog.csdn.net/jiangwei0910410003/article/details/17504315#quote 博主推荐: 风萧兮兮易水寒,“天真”一去兮不复还.如 ...

  6. Android中onTouch方法的执行过程以及和onClick执行发生冲突的解决办法

    $*********************************************************************************************$ 博主推荐 ...

  7. ORACLE查看SQL的执行次数/频率

    在ORACLE数据库应用调优中,一个SQL的执行次数/频率也是常常需要关注的,因为某个SQL执行太频繁,要么是由于应用设计有缺陷,需要在业务逻辑上做出优化处理,要么是业务特殊性所导致.如果执行频繁的S ...

  8. PLSQL_查询SQL的执行次数和频率(案例)

    2014-12-25 Created By BaoXinjian

  9. PLSQL_监控有些SQL的执行次数和频率

    原文:PLSQL_监控有些SQL的执行次数和频率 2014-12-25 Created By 鲍新建

随机推荐

  1. Django开发之登陆和登出

    使用django自带的验证模块 1.首先使用python manage.py startapp models为当前项目添加一个应用. 2.在setting.py中INSTALLED_APPS后面添加' ...

  2. 华为交换机配置Telnet步骤

    通过Telnet方式登录交换机进行设备查询管理为各网络管理员提供了便利,不需要成天拎着console线夹着笔记本蹲在机房里,只要在能连接到相应网络的电脑上Telnet连接即可. 前提条件是该交换机已经 ...

  3. IIS7 URL重写如何针对二级域名重写

    二级域名与站点主域名是绑在同一目录下,想实现访问二级域名重写至站点下的某个目录.  如:  访问so.abc.cn 实际访问的是站点根目录下的search目录下的文件 相当于so.abc.cn绑定至s ...

  4. MPAndroid 的学习

    1.MPAndroid 的github的地址: https://github.com/PhilJay/MPAndroidChart#documentation 2.使用步骤: 在build.gradl ...

  5. 2019 苏宁控股java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.苏宁等公司offer,岗位是Java后端开发,因为发展原因最终选择去了苏宁,入职一年时间了,也成为了面试官,之 ...

  6. 2019 新浪 java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.新浪等公司offer,岗位是Java后端开发,因为发展原因最终选择去了新浪,入职一年时间了,也成为了面试官,之 ...

  7. PHP基于TP5使用Websocket框架之GatewayWorker开发电商平台买家与卖家实时通讯

    前段时间公司提了一个新的需求,在商品的详情页要实现站内买家和商品卖家实时通讯的功能以方便沟通促成交易,要开发此功能当时首先考虑到的就是swoole和workerman了,从网上大概了解了一下关于这两款 ...

  8. 基于SVM的道路简单分割

    折腾了几天了,这个看似简单的东西,怎么做起来那么费劲啊? 任重而道远,光玩,光去幻想,是什么也做不出来的,要一点一点儿大量时间与精力的投入,才能出结果的. (点击下图,可选择原图观看,清晰的效果) 2 ...

  9. PYTHON 文件读写、坐标寻址、查找替换

    读文件 打开文件(文件需要存在) #打开文件 f = open("data.txt","r") #设置文件对象 print(f)#文件句柄 f.close() ...

  10. 通过公网ip访问虚拟机web服务

    工作中有需要进行通过外网ip访问虚拟机上的web服务,通过查阅资料,将配置过程整理如下: 思路:通过路由器的端口映射访问虚拟机上的web服务 1. 前提是在虚拟机上的web服务已经部署好,并且可以通过 ...