测量使用预编译库的程序的总 CPU 时间(C++、Linux)

发布于 2024-09-28 11:16:08 字数 297 浏览 9 评论 0原文

我目前遇到了这个问题,我很想听听您的一些建议。

我有一个 C++ 程序,它使用预编译库对 PostgreSQL 数据库进行一些查询。现在的问题是我想找出执行程序源代码中描述的所有例程所需的总(组合)CPU 时间以及等待与数据库相关的活动所花费的时间。

我在Linux中使用了time命令,但它似乎没有测量程序在数据库上花费的时间。

在我的情况下,我不可能重新编译提供给我的库,所以我认为像 gprof 这样的东西不会起作用。

有什么建议吗?

谢谢。

I am currently stumbled into this problem and I'd love to hear some suggestions from you.

I have a C++ program that uses a precompiled library to make some queries to PostgreSQL database. Now the problem is I want to find out the total (combined) cpu time it takes to do all routines described in the source code of the program and also the time it spends waiting for the database-related activities.

I used the time command in Linux, but it seems that it didn't measure the time the program spent on the database.

And in my situation, it won't be possible for me to recompile the library provided to me, so I don't think things like gprof would work.

Any suggestions?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残龙傲雪 2024-10-05 11:16:08

尝试一下ctime中的时钟功能。

clock_t start, end;
double cpu_time_used

开始=时钟(); // 做事 结束=时钟(); cpu_time_used = ((double)(结束 - 开始)) / CLOCKS_PER_SEC;

Try the clock function in ctime.

clock_t start, end;
double cpu_time_used

start = clock(); // Do stuff end = clock(); cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

九命猫 2024-10-05 11:16:08

使用 POSIX 的 times,它测量进程的真实时间、用户时间和系统时间和它的孩子。

链接的 Opengroup 页面上有一个示例:“计时数据库查找”

Use POSIX's times, it measures real, user and system time of a process and its children.

There is an example on the linked Opengroup page: "Timing a Database Lookup"

沉默的熊 2024-10-05 11:16:08

当然,无论如何您都会获得挂钟时间,但大概您正在尝试获得 CPU 时间。

当涉及子流程(或不相关的流程)时,这很重要。但是,您可能希望尝试采用更全面的基准测试方法。

测量应用程序的延迟很容易(只需观察挂钟),但吞吐量通常比较困难。

要了解应用程序在负载下的行为方式,您需要以可重现的方式将其置于负载下(在生产级硬件上)。

这通常意味着同时执行大量任务,因为现代硬件往往能够同时执行多项任务。此外,如果您的应用程序中的任何内容曾经等待任何外部数据源(可能包括您自己计算机的硬盘驱动器),那么即使在单核上,您也可以通过同时处理多个请求来获得更好的吞吐量。

您可能需要查看像 oprofile 这样的工具,它是为分析而不是基准测试而设计的。

Of course you'll get the wall-clock time anyway, but presumably you're trying to get the CPU time.

This is nontrivial when you have subprocesses (or unrelated processes) involved. However, you may want to try to have a more holistic approach to benchmarking.

Measuring the latency of an application is easy enough (just watch the wall-clock) but throughput is generally harder.

To get an idea of how an application behaves under load, you need to put it under load (on production-grade hardware), in a reproducible way.

This normally means hitting it with lots of tasks concurrently, as modern hardware tends to be able to do several things at once. Moreover, if anything in your app ever waits for any external data source (including the hard drive of your own machine potentially), you can get better throughput even on a single core by having multiple requests being served at once.

You may want to look at tools like oprofile, which is designed for profiling, not benchmarking.

叹倦 2024-10-05 11:16:08

您可以打开 log_statementlog_duration 并在 postgresql.conf 中设置 log_min_duration_statement=0,运行您的程序,然后然后使用 PQA 等分析 Postgres 日志。

You can turn on log_statement and log_duration and set log_min_duration_statement=0 in postgresql.conf, run your program, and then analyze Postgres logs using for example PQA.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文