C ++基准测试工具 [英] C++ Benchmark tool

查看:237
本文介绍了C ++基准测试工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些应用程序,这使数据库请求。我想这并不重要,我使用什么样的数据库,但是让我们说这是一个简单的 SQLite -driven数据库。 / p>

现在,此应用程序作为服务运行,每分钟执行一些请求(这个数字可能实际上很大)。



我愿意基准查询检索他们的数字,最大/最小/平均运行时间一段时间,我想为这个设计我自己的工具(显然,有一些,但我需要



所以 - 你能为这个任务提出一个方法吗?




我想有几种可能的情况:



1)我可以访问应用程序源代码。在这里,显然,我想做一些跨应用程序集成,可能使用管道。你能提出一些关于这应该如何做



2)我没有资源。因此,这是可能的从我的应用程序执行一些整洁的注入,以另一个基准?



非常感谢。

解决方案

我的回答只适用于case 1)。



在我的经验中,profiling是一个有趣的任务。使用专业工具可以是有效的,但它可能需要很多时间找到正确的一个,并学习如何正确使用它。我通常开始在一个非常简单的方式。我准备了两个非常简单的类。第一个ProfileHelper类填充了构造函数中的开始时间和析构函数中的结束时间。第二个类ProfileHelperStatistic是一个具有额外统计能力的容器(std :: multimap +几个方法来返回平均值,标准偏差和其他有趣的东西)。



ProfilerHelper对容器的引用以及在退出之前析构函数推送容器中的数据。您可以在main中声明ProfileHelperStatistic,如果您在特定函数的开头创建了一个特定函数的堆栈ProfilerHelper,则作业完成。 ProfileHelper的构造函数将存储开始时间,析构函数会将结果推送到ProfileHelperStatistic。



这是相当容易实现的,跨平台。创建和销毁对象的时间不会被记录,所以你不会污染结果。计算最终统计数据可能很昂贵,因此我建议您在结束时运行一次。



您还可以自定义要存储在ProfileHelperStatistic中的信息额外的信息(例如时间戳或内存使用)。



实现是相当容易,两个类不大于50行。只有两个提示:



1)在析构函数中捕获所有的内容:



2)



这是一个简单的工具,它可以帮助您以非常有效的方式分析您的应用程序。我的建议是从几个宏函数(5-7逻辑块)开始,然后增加粒度。记住80-20规则:20%的源代码使用80%的时间。



上一个关于数据库的注意事项:数据库动态地调整性能,查询几次结束时查询将比开始时更快(Oracle,我猜其他数据库也一样)。换句话说,如果你测试重点和​​人为的应用程序专注于只有几个特定的​​查询,你可以得到太乐观的结果。


I have some application, which makes database requests. I guess it doesn't actually matter, what kind of the database I am using, but let's say it's a simple SQLite-driven database.

Now, this application runs as a service and does some amount of requests per minute (this number might actually be huge).

I'm willing to benchmark the queries to retrieve their number, maximal / minimal / average running time for some period and I wish to design my own tool for this (obviously, there are some, but I need my own for some appropriate reasons :).

So - could you advice an approach for this task?


I guess there are several possible cases:

1) I have access to the application source code. Here, obviously, I want to make some sort of cross-application integration, probably using pipes. Could you advice something about how this should be done and (if there is one) any other possible solution?

2) I don't have sources. So, is this even possible to perform some neat injection from my application to benchmark the other one? I hope there is a way, maybe hacky, whatever.

Thanks a lot.

解决方案

My answer is valid just for the case 1).

In my experience profiling it is a fun a difficult task. Using professional tools can be effective but it can take a lot of time to find the right one and learn how to use it properly. I usually start in a very simple way. I have prepared two very simple classes. The first one ProfileHelper the class populate the start time in the constructor and the end time in the destructor. The second class ProfileHelperStatistic is a container with extra statistical capability (a std::multimap + few methods to return average, standard deviation and other funny stuff).

The ProfilerHelper has an reference to the container and before exit the destructor push the data in the container.You can declare the ProfileHelperStatistic in the main and if you create on the stack ProfilerHelper at the beginning of a specific function the job is done. The constructor of the ProfileHelper will store the starting time and the destructor will push the result on the ProfileHelperStatistic.

It is fairly easy to implement and with minor modification can be implemented as cross-platform. The time to create and destroy the object are not recorded, so you will not polluted the result. Calculating the final statistic can be expensive, so I suggest you to run it once at the end.

You can also customize the information that you are going to store in ProfileHelperStatistic adding extra information (like timestamp or memory usage for example).

The implementation is fairly easy, two class that are not bigger than 50 lines each. Just two hints:

1) catch all in the destructor!

2) consider to use collection that take constant time to insert if you are going to store a lot of data.

This is a simple tool and it can help you profiling your application in a very effective way. My suggestion is to start with few macro functions (5-7 logical block) and then increase the granularity. Remember the 80-20 rule: 20% of the source code use 80% of the time.

Last note about database: database tunes the performance dynamically, if you run a query several time at the end the query will be quicker than at the beginning (Oracle does, I guess other database as well). In other word, if you test heavily and artificially the application focusing on just few specific queries you can get too optimistic results.

这篇关于C ++基准测试工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆