Measuring total CPU time of a program that uses precompiled libraries (C++, Linux)
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.
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;
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"
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.
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.
精彩评论