Skip to content

Commit 5b7e036

Browse files
committed
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.
1 parent 64171b3 commit 5b7e036

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/bin/pgbench/pgbench.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ double sample_rate = 0.0;
169169
* When threads are throttled to a given rate limit, this is the target delay
170170
* to reach that rate in usec. 0 is the default and means no throttling.
171171
*/
172-
int64 throttle_delay = 0;
172+
double throttle_delay = 0;
173173

174174
/*
175175
* 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)
826826
/*
827827
* random number generator: generate a value, such that the series of values
828828
* will approximate a Poisson distribution centered on the given value.
829+
*
830+
* Individual results are rounded to integers, though the center value need
831+
* not be one.
829832
*/
830833
static int64
831-
getPoissonRand(TState *thread, int64 center)
834+
getPoissonRand(TState *thread, double center)
832835
{
833836
/*
834837
* Use inverse transform sampling to generate a value > 0, such that the
@@ -839,7 +842,7 @@ getPoissonRand(TState *thread, int64 center)
839842
/* erand in [0, 1), uniform in (0, 1] */
840843
uniform = 1.0 - pg_erand48(thread->random_state);
841844

842-
return (int64) (-log(uniform) * ((double) center) + 0.5);
845+
return (int64) (-log(uniform) * center + 0.5);
843846
}
844847

845848
/* helper function for getZipfianRand */
@@ -5114,8 +5117,8 @@ main(int argc, char **argv)
51145117
fprintf(stderr, "invalid rate limit: \"%s\"\n", optarg);
51155118
exit(1);
51165119
}
5117-
/* Invert rate limit into a time offset */
5118-
throttle_delay = (int64) (1000000.0 / throttle_value);
5120+
/* Invert rate limit into per-transaction delay in usec */
5121+
throttle_delay = 1000000.0 / throttle_value;
51195122
}
51205123
break;
51215124
case 'L':
@@ -5239,7 +5242,11 @@ main(int argc, char **argv)
52395242
if (nthreads > nclients)
52405243
nthreads = nclients;
52415244

5242-
/* compute a per thread delay */
5245+
/*
5246+
* Convert throttle_delay to a per-thread delay time. Note that this
5247+
* might be a fractional number of usec, but that's OK, since it's just
5248+
* the center of a Poisson distribution of delays.
5249+
*/
52435250
throttle_delay *= nthreads;
52445251

52455252
if (argc > optind)

0 commit comments

Comments
 (0)