Skip to content

Commit 9f75e37

Browse files
committed
Refactor code to print pgbench progress reports.
threadRun() function is very long and deeply-nested. Extract the code to print progress reports to a separate function, to make it slightly easier to read. Author: Fabien Coelho Discussion: https://www.postgresql.org/message-id/alpine.DEB.2.21.1903101225270.17271%40lancre
1 parent 5857be9 commit 9f75e37

File tree

1 file changed

+94
-83
lines changed

1 file changed

+94
-83
lines changed

src/bin/pgbench/pgbench.c

+94-83
Original file line numberDiff line numberDiff line change
@@ -4893,6 +4893,99 @@ addScript(ParsedScript script)
48934893
num_scripts++;
48944894
}
48954895

4896+
/*
4897+
* Print progress report.
4898+
*
4899+
* On entry, *last and *last_report contain the statistics and time of last
4900+
* progress report. On exit, they are updated with the new stats.
4901+
*/
4902+
static void
4903+
printProgressReport(TState *thread, int64 test_start, int64 now,
4904+
StatsData *last, int64 *last_report)
4905+
{
4906+
/* generate and show report */
4907+
int64 run = now - *last_report,
4908+
ntx;
4909+
double tps,
4910+
total_run,
4911+
latency,
4912+
sqlat,
4913+
lag,
4914+
stdev;
4915+
char tbuf[315];
4916+
StatsData cur;
4917+
4918+
/*
4919+
* Add up the statistics of all threads.
4920+
*
4921+
* XXX: No locking. There is no guarantee that we get an atomic snapshot
4922+
* of the transaction count and latencies, so these figures can well be
4923+
* off by a small amount. The progress report's purpose is to give a
4924+
* quick overview of how the test is going, so that shouldn't matter too
4925+
* much. (If a read from a 64-bit integer is not atomic, you might get a
4926+
* "torn" read and completely bogus latencies though!)
4927+
*/
4928+
initStats(&cur, 0);
4929+
for (int i = 0; i < nthreads; i++)
4930+
{
4931+
mergeSimpleStats(&cur.latency, &thread[i].stats.latency);
4932+
mergeSimpleStats(&cur.lag, &thread[i].stats.lag);
4933+
cur.cnt += thread[i].stats.cnt;
4934+
cur.skipped += thread[i].stats.skipped;
4935+
}
4936+
4937+
/* we count only actually executed transactions */
4938+
ntx = (cur.cnt - cur.skipped) - (last->cnt - last->skipped);
4939+
total_run = (now - test_start) / 1000000.0;
4940+
tps = 1000000.0 * ntx / run;
4941+
if (ntx > 0)
4942+
{
4943+
latency = 0.001 * (cur.latency.sum - last->latency.sum) / ntx;
4944+
sqlat = 1.0 * (cur.latency.sum2 - last->latency.sum2) / ntx;
4945+
stdev = 0.001 * sqrt(sqlat - 1000000.0 * latency * latency);
4946+
lag = 0.001 * (cur.lag.sum - last->lag.sum) / ntx;
4947+
}
4948+
else
4949+
{
4950+
latency = sqlat = stdev = lag = 0;
4951+
}
4952+
4953+
if (progress_timestamp)
4954+
{
4955+
/*
4956+
* On some platforms the current system timestamp is available in
4957+
* now_time, but rather than get entangled with that, we just eat the
4958+
* cost of an extra syscall in all cases.
4959+
*/
4960+
struct timeval tv;
4961+
4962+
gettimeofday(&tv, NULL);
4963+
snprintf(tbuf, sizeof(tbuf), "%ld.%03ld s",
4964+
(long) tv.tv_sec, (long) (tv.tv_usec / 1000));
4965+
}
4966+
else
4967+
{
4968+
/* round seconds are expected, but the thread may be late */
4969+
snprintf(tbuf, sizeof(tbuf), "%.1f s", total_run);
4970+
}
4971+
4972+
fprintf(stderr,
4973+
"progress: %s, %.1f tps, lat %.3f ms stddev %.3f",
4974+
tbuf, tps, latency, stdev);
4975+
4976+
if (throttle_delay)
4977+
{
4978+
fprintf(stderr, ", lag %.3f ms", lag);
4979+
if (latency_limit)
4980+
fprintf(stderr, ", " INT64_FORMAT " skipped",
4981+
cur.skipped - last->skipped);
4982+
}
4983+
fprintf(stderr, "\n");
4984+
4985+
*last = cur;
4986+
*last_report = now;
4987+
}
4988+
48964989
static void
48974990
printSimpleStats(const char *prefix, SimpleStats *ss)
48984991
{
@@ -6199,89 +6292,7 @@ threadRun(void *arg)
61996292
now = INSTR_TIME_GET_MICROSEC(now_time);
62006293
if (now >= next_report)
62016294
{
6202-
/* generate and show report */
6203-
StatsData cur;
6204-
int64 run = now - last_report,
6205-
ntx;
6206-
double tps,
6207-
total_run,
6208-
latency,
6209-
sqlat,
6210-
lag,
6211-
stdev;
6212-
char tbuf[315];
6213-
6214-
/*
6215-
* Add up the statistics of all threads.
6216-
*
6217-
* XXX: No locking. There is no guarantee that we get an
6218-
* atomic snapshot of the transaction count and latencies, so
6219-
* these figures can well be off by a small amount. The
6220-
* progress report's purpose is to give a quick overview of
6221-
* how the test is going, so that shouldn't matter too much.
6222-
* (If a read from a 64-bit integer is not atomic, you might
6223-
* get a "torn" read and completely bogus latencies though!)
6224-
*/
6225-
initStats(&cur, 0);
6226-
for (i = 0; i < nthreads; i++)
6227-
{
6228-
mergeSimpleStats(&cur.latency, &thread[i].stats.latency);
6229-
mergeSimpleStats(&cur.lag, &thread[i].stats.lag);
6230-
cur.cnt += thread[i].stats.cnt;
6231-
cur.skipped += thread[i].stats.skipped;
6232-
}
6233-
6234-
/* we count only actually executed transactions */
6235-
ntx = (cur.cnt - cur.skipped) - (last.cnt - last.skipped);
6236-
total_run = (now - thread_start) / 1000000.0;
6237-
tps = 1000000.0 * ntx / run;
6238-
if (ntx > 0)
6239-
{
6240-
latency = 0.001 * (cur.latency.sum - last.latency.sum) / ntx;
6241-
sqlat = 1.0 * (cur.latency.sum2 - last.latency.sum2) / ntx;
6242-
stdev = 0.001 * sqrt(sqlat - 1000000.0 * latency * latency);
6243-
lag = 0.001 * (cur.lag.sum - last.lag.sum) / ntx;
6244-
}
6245-
else
6246-
{
6247-
latency = sqlat = stdev = lag = 0;
6248-
}
6249-
6250-
if (progress_timestamp)
6251-
{
6252-
/*
6253-
* On some platforms the current system timestamp is
6254-
* available in now_time, but rather than get entangled
6255-
* with that, we just eat the cost of an extra syscall in
6256-
* all cases.
6257-
*/
6258-
struct timeval tv;
6259-
6260-
gettimeofday(&tv, NULL);
6261-
snprintf(tbuf, sizeof(tbuf), "%ld.%03ld s",
6262-
(long) tv.tv_sec, (long) (tv.tv_usec / 1000));
6263-
}
6264-
else
6265-
{
6266-
/* round seconds are expected, but the thread may be late */
6267-
snprintf(tbuf, sizeof(tbuf), "%.1f s", total_run);
6268-
}
6269-
6270-
fprintf(stderr,
6271-
"progress: %s, %.1f tps, lat %.3f ms stddev %.3f",
6272-
tbuf, tps, latency, stdev);
6273-
6274-
if (throttle_delay)
6275-
{
6276-
fprintf(stderr, ", lag %.3f ms", lag);
6277-
if (latency_limit)
6278-
fprintf(stderr, ", " INT64_FORMAT " skipped",
6279-
cur.skipped - last.skipped);
6280-
}
6281-
fprintf(stderr, "\n");
6282-
6283-
last = cur;
6284-
last_report = now;
6295+
printProgressReport(thread, thread_start, now, &last, &last_report);
62856296

62866297
/*
62876298
* Ensure that the next report is in the future, in case

0 commit comments

Comments
 (0)