C#应用程序分析给出了不同的结果

时间:2014-04-26 19:07:09

标签: c# performance profiling profiler performance-testing

我不熟悉资料分析。我试图分析连接到SQLite数据库并检索数据的C#应用​​程序。该数据库包含146856400行,select查询在执行后检索428800行。

在第一次执行时,主线程需要246686 ms

在第二次执行相同代码时,主线程仅需4296 ms

重新启动系统后

在第一次执行时,主线程需要244533 ms

在第二次执行相同代码时,主线程只需要4053 ms

问题:

1)为什么第一次执行时间和第二次执行时间之间存在很大差异

2)重新启动系统后,为什么我没有得到相同的结果。

请帮忙

1 个答案:

答案 0 :(得分:2)

您遇到查询的冷执行和热执行之间的区别。冷意味着第一次并加热db查询的所有后续调用。 一切都是“冷”的第一次

  • 操作系统文件系统缓存为空。
  • SQLLite缓存为空。
  • ORM动态查询编译尚未完成并缓存。
  • ORM Mapper缓存为空。
  • 垃圾收集器需要调整您的工作集
  • ....

当您第二次执行查询时,所有这些首次初始化(缓存)都已完成,只要有足够的内存可用于缓存大量请求的数据,您就可以测量不同缓存级别的影响。

4分钟到4分之间的表现差异令人印象深刻。这两个数字都有效。测量东西很容易。告诉别人你已经测量到了什么,以及如何通过改变这个或那个更难以改善性能。

表演游戏经常是这样的:

Customer: It is slow 
Dev:      I cannot repro your issue.
Customer: Here is my scenario .... 
Dev:      I still cannot repro it. Can you give me data set you use and the exact steps you did perform?
Customer: Sure. Here is the data and the test steps.
Dev:      Ahh I see. I can make it 10 times faster.
Customer: That is great. Can I have the fix?
Dev:      Sure here it is.
Customer: **Very Angry** It has become faster yes. But I cannot read my old data!
Dev:      Ups. We need to migrate all your old data to the new much more efficient format. 
          We need to  develop a a conversion tool which will take 3 weeks and your site will 
          have 3 days downtime while the conversion tool is running. 
          Or 
          We keep the old inefficient data format. But then we can make it only 9 times faster.
Customer: I want to access my data faster without data conversion!
Dev:      Here is the fix which is 10% slower with no schema changes. 
Customer: Finally. The fix does not break anything but it has not become faster?
Dev:      I have measured your use case. It is only slow for the first time. 
          All later data retrievals are 9 times faster than before. 
Customer: Did I mention that in my use case I read always different data?
Dev:      No you did not. 
Customer: Fix it!
Dev:      That is not really possible without a major rewrite of large portions of our software.
Customer: The data I want to access is stored in a list. I want to process it sequentially.
Dev:      In that case we can preload the data in the background while you are working the current data set. You will only experience a delay for the first data set on each working day.
Customer: Can I have the fix?
Dev:      Sure here it is.
Customer: Perfect. It works!

表现很难掌握,因为大部分时间你都会处理主观的感知表现。将其归结为定量测量是一个良好的开端,但您需要调整指标以反映实际的客户使用情况,否则您可能会在上述错误的位置进行优化。必须完全了解客户要求和用例。另一方面,您需要了解您的完整系统(将其描述为地狱),以便能够区分冷和热查询执行之间的区别以及您可以调整整个事物的位置。如果您一直查询不同的数据(不太可能),这些缓存将变得无用。也许您需要一个不同的索引来加速查询或购买SSD,或者将所有数据保存在内存中并在内存中进行所有后续查询....