特定例程的绩效指标:任何最佳实践?

时间:2023-01-12 23:49:42

I'd like to gather metrics on specific routines of my code to see where I can best optimize. Let's take a simple example and say that I have a "Class" database with multiple "Students." Let's say the current code calls the database for every student instead of grabbing them all at once in a batch. I'd like to see how long each trip to the database takes for every student row.

我想收集有关我的代码的特定例程的指标,以了解我可以最佳地优化的位置。让我们举一个简单的例子,说我有一个带有多个“学生”的“班级”数据库。假设当前代码为每个学生调用数据库,而不是一次性地将它们全部抓取。我想看看每个学生行每次访问多长时间。

This is in C#, but I think it applies everywhere. Usually when I get curious as to a specific routine's performance, I'll create a DateTime object before it runs, run the routine, and then create another DateTime object after the call and take the milliseconds difference between the two to see how long it runs. Usually I just output this in the page's trace...so it's a bit lo-fi. Any best practices for this? I thought about being able to put the web app into some "diagnostic" mode and doing verbose logging/event log writing with whatever I'm after, but I wanted to see if the * hive mind has a better idea.

这是在C#中,但我认为它适用于所有地方。通常当我对特定例程的性能感到好奇时,我会在它运行之前创建一个DateTime对象,运行例程,然后在调用之后创建另一个DateTime对象并获取两者之间的毫秒差异以查看它运行了多长时间。通常我只是在页面的跟踪中输出它...所以它有点低保真。这方面的最佳做法是什么?我想过能够将Web应用程序置于某种“诊断”模式并使用我之后的任何内容进行详细的日志记录/事件日志写入,但我想看看* hive mind是否有更好的想法。

8 个解决方案

#1


1  

Some times approach you take will give you a best look at you application performance. One things I can recommend is to use System.Diagnostics.Stopwatch instead of DateTime , DateTime is accurate only up to 16 ms where Stopwatch is accurate up to the cpu tick.

有时你采取的方法会让你最好地看看你的应用程序性能。我可以推荐的一件事是使用System.Diagnostics.Stopwatch而不是DateTime,DateTime只能精确到16毫秒,其中秒表精确到cpu tick。

But I recommend to complement it with custom performance counters for production and running the app under profiler during development.

但我建议用自定义性能计数器来补充它,以便在开发期间在profiler下生成和运行应用程序。

#2


3  

For database queries, you have a two small problems. Cache: data cache and statement cache.

对于数据库查询,您有两个小问题。缓存:数据缓存和语句缓存。

If you run the query once, the statement is parsed, prepared, bound and executed. Data is fetched from files into cache.

如果运行一次查询,则会解析,准备,绑定和执行该语句。数据从文件中提取到缓存中。

When you execute the query a second time, the cache is used, and performance is often much, much better.

当您第二次执行查询时,将使用缓存,性能通常会好得多。

Which is the "real" performance number? First one or second one? Some folks say "worst case" is the real number, and we have to optimize that. Others say "typical case" and run the query twice, ignoring the first one. Others says "average" and run in 30 times, averaging them all. Other say "typical average", run the 31 times and average the last 30.

哪个是“真正的”性能数字?第一个还是第二个?有些人说“最坏情况”是实数,我们必须优化它。其他人说“典型案例”并运行查询两次,忽略第一个。其他人说“平均”并且运行30次,将它们平均化。其他说“典型的平均值”,运行31次,平均最后30次。

I suggest that the "last 30 of 31" is the most meaningful DB performance number. Don't sweat the things you can't control (parse, prepare, bind) times. Sweat the stuff you can control -- data structures, I/O loading, indexes, etc.

我建议“31的最后30个”是最有意义的数据库性能数字。不要为你无法控制(解析,准备,绑定)时间的东西而流汗。冒汗你可以控制的东西 - 数据结构,I / O加载,索引等。

#3


2  

I use this method on occasion and find it to be fairly accurate. The problem is that in large applications with a fairly hefty amount of debugging logs, it can be a pain to search through the logs for this information. So I use external tools (I program in Java primarily, and use JProbe) which allow me to see average and total times for my methods, how much time is spent exclusively by a particular method (as opposed to the cumulative time spent by the method and any method it calls), as well as memory and resource allocations.

我偶尔使用这种方法,并发现它相当准确。问题是,在具有相当大量调试日志的大型应用程序中,在日志中搜索此信息可能会很麻烦。所以我使用外部工具(我主要使用Java编程,并使用JProbe),它允许我查看我的方法的平均和总时间,特定方法花费的时间(与方法花费的累计时间相反)以及它调用的任何方法,以及内存和资源分配。

These tools can assist you in measuring the performance of entire applications, and if you are doing a significant amount of development in an area where performance is important, you may want to research the tools available and learn how to use one.

这些工具可以帮助您测量整个应用程序的性能,如果您在性能很重要的领域进行大量开发,您可能需要研究可用的工具并学习如何使用它们。

#4


1  

There are some Profilers available but, frankly, I think your approach is better. The profiler approach is overkill. Maybe the use of profilers is worth the trouble if you absolutely have no clue where the bottleneck is. I would rather spend a little time analyzing the problem up front and putting a few strategic print statements than figure out how to instrument your app for profiling then pour over gargantuan reports where every executable line of code is timed.

有一些Profilers可用,但坦率地说,我认为你的方法更好。剖析器方法过度。如果您完全不知道瓶颈在哪里,也许使用分析器是值得的。我宁愿花一点时间预先分析问题并提出一些战略性的打印陈述,而不是弄清楚如何检测你的应用程序进行分析,然后倒入庞大的报告,其中每个可执行的代码行都是定时的。

#5


1  

If you're working with .NET, then I'd recommend checking out the Stopwatch class. The times you get back from that are going to be much more accurate than an equivalent sample using DateTime.

如果您正在使用.NET,那么我建议您查看Stopwatch类。您从中获得的时间比使用DateTime的等效样本准确得多。

I'd also recommend checking out ANTS Profiler for scenarios in which performance is exceptionally important.

我还建议您查看ANTS Profiler,了解性能非常重要的场景。

#6


1  

It is worth considering investing in a good commercial profiler, particularly if you ever expect to have to do this a second time.

值得考虑投资一个好的商业分析师,特别是如果你曾经期望必须第二次这样做。

The one I use, JProfiler, works in the Java world and can attach to an already-running application, so no special instrumentation is required (at least with the more recent JVMs).

我使用的那个,JProfiler,在Java世界中工作,可以连接到已经运行的应用程序,因此不需要特殊的工具(至少使用更新的JVM)。

It very rapidly builds a sorted list of hotspots in your code, showing which methods your code is spending most of its time inside. It filters pretty intelligently by default, and allows you to tune the filtering further if required, meaning that you can ignore the detail of third party libraries, while picking out those of your methods which are taking all the time.

它可以非常快速地在代码中构建一个排序的热点列表,显示代码在大部分时间内花费的方法。默认情况下,它会非常智能地过滤,并允许您根据需要进一步调整过滤,这意味着您可以忽略第三方库的详细信息,同时选择那些一直在使用的方法。

In addition, you get lots of other useful reports on what your code is doing. It paid for the cost of the licence in the time I saved the first time I used it; I didn't have to add in lots of logging statements and construct a mechanism to anayse the output: the developers of the profiler had already done all of that for me.

此外,您还可以获得许多有关代码运行情况的有用报告。它在我第一次使用它的时候支付了许可证的费用;我没有必要添加大量的日志记录语句并构建一个机制来分析输出:探查器的开发人员已经为我做了所有这些。

I'm not associated with ej-technologies in any way other than being a very happy customer.

除了作为一个非常幸福的客户之外,我不会以任何方式与ej技术联系起来。

#7


0  

I use this method and I think it's very accurate.

我使用这种方法,我认为它非常准确。

#8


0  

I think you have a good approach. I recommend that you produce "machine friendly" records in the log file(s) so that you can parse them more easily. Something like CSV or other-delimited records that are consistently structured.

我认为你有一个很好的方法。我建议您在日志文件中生成“机器友好”记录,以便您可以更轻松地解析它们。像CSV或其他分隔的记录一样结构化的东西。

#1


1  

Some times approach you take will give you a best look at you application performance. One things I can recommend is to use System.Diagnostics.Stopwatch instead of DateTime , DateTime is accurate only up to 16 ms where Stopwatch is accurate up to the cpu tick.

有时你采取的方法会让你最好地看看你的应用程序性能。我可以推荐的一件事是使用System.Diagnostics.Stopwatch而不是DateTime,DateTime只能精确到16毫秒,其中秒表精确到cpu tick。

But I recommend to complement it with custom performance counters for production and running the app under profiler during development.

但我建议用自定义性能计数器来补充它,以便在开发期间在profiler下生成和运行应用程序。

#2


3  

For database queries, you have a two small problems. Cache: data cache and statement cache.

对于数据库查询,您有两个小问题。缓存:数据缓存和语句缓存。

If you run the query once, the statement is parsed, prepared, bound and executed. Data is fetched from files into cache.

如果运行一次查询,则会解析,准备,绑定和执行该语句。数据从文件中提取到缓存中。

When you execute the query a second time, the cache is used, and performance is often much, much better.

当您第二次执行查询时,将使用缓存,性能通常会好得多。

Which is the "real" performance number? First one or second one? Some folks say "worst case" is the real number, and we have to optimize that. Others say "typical case" and run the query twice, ignoring the first one. Others says "average" and run in 30 times, averaging them all. Other say "typical average", run the 31 times and average the last 30.

哪个是“真正的”性能数字?第一个还是第二个?有些人说“最坏情况”是实数,我们必须优化它。其他人说“典型案例”并运行查询两次,忽略第一个。其他人说“平均”并且运行30次,将它们平均化。其他说“典型的平均值”,运行31次,平均最后30次。

I suggest that the "last 30 of 31" is the most meaningful DB performance number. Don't sweat the things you can't control (parse, prepare, bind) times. Sweat the stuff you can control -- data structures, I/O loading, indexes, etc.

我建议“31的最后30个”是最有意义的数据库性能数字。不要为你无法控制(解析,准备,绑定)时间的东西而流汗。冒汗你可以控制的东西 - 数据结构,I / O加载,索引等。

#3


2  

I use this method on occasion and find it to be fairly accurate. The problem is that in large applications with a fairly hefty amount of debugging logs, it can be a pain to search through the logs for this information. So I use external tools (I program in Java primarily, and use JProbe) which allow me to see average and total times for my methods, how much time is spent exclusively by a particular method (as opposed to the cumulative time spent by the method and any method it calls), as well as memory and resource allocations.

我偶尔使用这种方法,并发现它相当准确。问题是,在具有相当大量调试日志的大型应用程序中,在日志中搜索此信息可能会很麻烦。所以我使用外部工具(我主要使用Java编程,并使用JProbe),它允许我查看我的方法的平均和总时间,特定方法花费的时间(与方法花费的累计时间相反)以及它调用的任何方法,以及内存和资源分配。

These tools can assist you in measuring the performance of entire applications, and if you are doing a significant amount of development in an area where performance is important, you may want to research the tools available and learn how to use one.

这些工具可以帮助您测量整个应用程序的性能,如果您在性能很重要的领域进行大量开发,您可能需要研究可用的工具并学习如何使用它们。

#4


1  

There are some Profilers available but, frankly, I think your approach is better. The profiler approach is overkill. Maybe the use of profilers is worth the trouble if you absolutely have no clue where the bottleneck is. I would rather spend a little time analyzing the problem up front and putting a few strategic print statements than figure out how to instrument your app for profiling then pour over gargantuan reports where every executable line of code is timed.

有一些Profilers可用,但坦率地说,我认为你的方法更好。剖析器方法过度。如果您完全不知道瓶颈在哪里,也许使用分析器是值得的。我宁愿花一点时间预先分析问题并提出一些战略性的打印陈述,而不是弄清楚如何检测你的应用程序进行分析,然后倒入庞大的报告,其中每个可执行的代码行都是定时的。

#5


1  

If you're working with .NET, then I'd recommend checking out the Stopwatch class. The times you get back from that are going to be much more accurate than an equivalent sample using DateTime.

如果您正在使用.NET,那么我建议您查看Stopwatch类。您从中获得的时间比使用DateTime的等效样本准确得多。

I'd also recommend checking out ANTS Profiler for scenarios in which performance is exceptionally important.

我还建议您查看ANTS Profiler,了解性能非常重要的场景。

#6


1  

It is worth considering investing in a good commercial profiler, particularly if you ever expect to have to do this a second time.

值得考虑投资一个好的商业分析师,特别是如果你曾经期望必须第二次这样做。

The one I use, JProfiler, works in the Java world and can attach to an already-running application, so no special instrumentation is required (at least with the more recent JVMs).

我使用的那个,JProfiler,在Java世界中工作,可以连接到已经运行的应用程序,因此不需要特殊的工具(至少使用更新的JVM)。

It very rapidly builds a sorted list of hotspots in your code, showing which methods your code is spending most of its time inside. It filters pretty intelligently by default, and allows you to tune the filtering further if required, meaning that you can ignore the detail of third party libraries, while picking out those of your methods which are taking all the time.

它可以非常快速地在代码中构建一个排序的热点列表,显示代码在大部分时间内花费的方法。默认情况下,它会非常智能地过滤,并允许您根据需要进一步调整过滤,这意味着您可以忽略第三方库的详细信息,同时选择那些一直在使用的方法。

In addition, you get lots of other useful reports on what your code is doing. It paid for the cost of the licence in the time I saved the first time I used it; I didn't have to add in lots of logging statements and construct a mechanism to anayse the output: the developers of the profiler had already done all of that for me.

此外,您还可以获得许多有关代码运行情况的有用报告。它在我第一次使用它的时候支付了许可证的费用;我没有必要添加大量的日志记录语句并构建一个机制来分析输出:探查器的开发人员已经为我做了所有这些。

I'm not associated with ej-technologies in any way other than being a very happy customer.

除了作为一个非常幸福的客户之外,我不会以任何方式与ej技术联系起来。

#7


0  

I use this method and I think it's very accurate.

我使用这种方法,我认为它非常准确。

#8


0  

I think you have a good approach. I recommend that you produce "machine friendly" records in the log file(s) so that you can parse them more easily. Something like CSV or other-delimited records that are consistently structured.

我认为你有一个很好的方法。我建议您在日志文件中生成“机器友好”记录,以便您可以更轻松地解析它们。像CSV或其他分隔的记录一样结构化的东西。