From 5b7e036707ccd93506731da82a56b07023d13e30 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 25 Sep 2018 11:09:18 -0400 Subject: [PATCH] Avoid unnecessary precision loss for pgbench's --rate target. It's fairly silly to truncate the throttle_delay to integer when the only math we ever do with it requires converting back to double. Furthermore, given that people are starting to complain about restrictions like only supporting 1K client connections, I don't think we're very far away from situations where the precision loss matters. As the code stood, for example, there's no difference between --rate 100001 and --rate 111111; both get converted to throttle_delay = 9. Somebody trying to run 100 threads and have each one dispatch around 1K TPS would find this lack of precision rather surprising, especially since the required per-thread delays are around 1ms, well within the timing precision of modern systems. --- src/bin/pgbench/pgbench.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index ae81abab7f..7576e4cfae 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -169,7 +169,7 @@ double sample_rate = 0.0; * When threads are throttled to a given rate limit, this is the target delay * to reach that rate in usec. 0 is the default and means no throttling. */ -int64 throttle_delay = 0; +double throttle_delay = 0; /* * Transactions which take longer than this limit (in usec) are counted as @@ -826,9 +826,12 @@ getGaussianRand(TState *thread, int64 min, int64 max, double parameter) /* * random number generator: generate a value, such that the series of values * will approximate a Poisson distribution centered on the given value. + * + * Individual results are rounded to integers, though the center value need + * not be one. */ static int64 -getPoissonRand(TState *thread, int64 center) +getPoissonRand(TState *thread, double center) { /* * Use inverse transform sampling to generate a value > 0, such that the @@ -839,7 +842,7 @@ getPoissonRand(TState *thread, int64 center) /* erand in [0, 1), uniform in (0, 1] */ uniform = 1.0 - pg_erand48(thread->random_state); - return (int64) (-log(uniform) * ((double) center) + 0.5); + return (int64) (-log(uniform) * center + 0.5); } /* helper function for getZipfianRand */ @@ -5114,8 +5117,8 @@ main(int argc, char **argv) fprintf(stderr, "invalid rate limit: \"%s\"\n", optarg); exit(1); } - /* Invert rate limit into a time offset */ - throttle_delay = (int64) (1000000.0 / throttle_value); + /* Invert rate limit into per-transaction delay in usec */ + throttle_delay = 1000000.0 / throttle_value; } break; case 'L': @@ -5239,7 +5242,11 @@ main(int argc, char **argv) if (nthreads > nclients) nthreads = nclients; - /* compute a per thread delay */ + /* + * Convert throttle_delay to a per-thread delay time. Note that this + * might be a fractional number of usec, but that's OK, since it's just + * the center of a Poisson distribution of delays. + */ throttle_delay *= nthreads; if (argc > optind)