Commit 54a2d5b3 authored by Heikki Linnakangas's avatar Heikki Linnakangas

Simplify calculation of Poisson distributed delays in pgbench --rate mode.

The previous coding first generated a uniform random value between 0.0 and
1.0, then converted that to an integer between 1 and 10000, and divided that
again by 10000. Those conversions are unnecessary; we can use the double
value that pg_erand48() returns directly. While we're at it, put the logic
into a helper function, getPoissonRand().

The largest delay generated by the old coding was about 9.2 times the
average, because of the way the uniformly distributed value used for the
calculation was truncated to 1/10000 granularity. The new coding doesn't
have such clamping. With my laptop's DBL_MIN value, the maximum delay with
the new coding is about 700x the average. That seems acceptable - any
reasonable pgbench session should last long enough to average that out.

Backpatch to 9.4.
parent 02e3bcc6
...@@ -552,6 +552,25 @@ getGaussianRand(TState *thread, int64 min, int64 max, double threshold) ...@@ -552,6 +552,25 @@ getGaussianRand(TState *thread, int64 min, int64 max, double threshold)
return min + (int64)((max - min + 1) * rand); return min + (int64)((max - min + 1) * rand);
} }
/*
* random number generator: generate a value, such that the series of values
* will approximate a Poisson distribution centered on the given value.
*/
static int64
getPoissonRand(TState *thread, int64 center)
{
/*
* Use inverse transform sampling to generate a value > 0, such that the
* expected (i.e. average) value is the given argument.
*/
double uniform;
/* erand in [0, 1), uniform in (0, 1] */
uniform = 1.0 - pg_erand48(thread->random_state);
return (int64) (-log(uniform) * ((double) center) + 0.5);
}
/* call PQexec() and exit() on failure */ /* call PQexec() and exit() on failure */
static void static void
executeStatement(PGconn *con, const char *sql) executeStatement(PGconn *con, const char *sql)
...@@ -1009,21 +1028,13 @@ top: ...@@ -1009,21 +1028,13 @@ top:
if (throttle_delay && !st->is_throttled) if (throttle_delay && !st->is_throttled)
{ {
/* /*
* Use inverse transform sampling to randomly generate a delay, such * Generate a delay such that the series of delays will approximate a
* that the series of delays will approximate a Poisson distribution * Poisson distribution centered on the throttle_delay time.
* centered on the throttle_delay time.
*
* 10000 implies a 9.2 (-log(1/10000)) to 0.0 (log 1) delay
* multiplier, and results in a 0.055 % target underestimation bias:
*
* SELECT 1.0/AVG(-LN(i/10000.0)) FROM generate_series(1,10000) AS i;
* = 1.000552717032611116335474
* *
* If transactions are too slow or a given wait is shorter than a * If transactions are too slow or a given wait is shorter than a
* transaction, the next transaction will start right away. * transaction, the next transaction will start right away.
*/ */
int64 wait = (int64) (throttle_delay * int64 wait = getPoissonRand(thread, throttle_delay);
1.00055271703 * -log(getrand(thread, 1, 10000) / 10000.0));
thread->throttle_trigger += wait; thread->throttle_trigger += wait;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment