Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54a2d5b

Browse files
committed
Simplify calculation of Poisson distributed delays in pgbench --rate mode.
The previous coding first generated a uniform random value between 0.0 and1.0, then converted that to an integer between 1 and 10000, and divided thatagain by 10000. Those conversions are unnecessary; we can use the doublevalue that pg_erand48() returns directly. While we're at it, put the logicinto a helper function, getPoissonRand().The largest delay generated by the old coding was about 9.2 times theaverage, because of the way the uniformly distributed value used for thecalculation was truncated to 1/10000 granularity. The new coding doesn'thave such clamping. With my laptop's DBL_MIN value, the maximum delay withthe new coding is about 700x the average. That seems acceptable - anyreasonable pgbench session should last long enough to average that out.Backpatch to 9.4.
1 parent02e3bcc commit54a2d5b

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

‎contrib/pgbench/pgbench.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,25 @@ getGaussianRand(TState *thread, int64 min, int64 max, double threshold)
552552
returnmin+ (int64)((max-min+1)*rand);
553553
}
554554

555+
/*
556+
* random number generator: generate a value, such that the series of values
557+
* will approximate a Poisson distribution centered on the given value.
558+
*/
559+
staticint64
560+
getPoissonRand(TState*thread,int64center)
561+
{
562+
/*
563+
* Use inverse transform sampling to generate a value > 0, such that the
564+
* expected (i.e. average) value is the given argument.
565+
*/
566+
doubleuniform;
567+
568+
/* erand in [0, 1), uniform in (0, 1] */
569+
uniform=1.0-pg_erand48(thread->random_state);
570+
571+
return (int64) (-log(uniform)* ((double)center)+0.5);
572+
}
573+
555574
/* call PQexec() and exit() on failure */
556575
staticvoid
557576
executeStatement(PGconn*con,constchar*sql)
@@ -1009,21 +1028,13 @@ doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile, AggVa
10091028
if (throttle_delay&& !st->is_throttled)
10101029
{
10111030
/*
1012-
* Use inverse transform sampling to randomly generate a delay, such
1013-
* that the series of delays will approximate a Poisson distribution
1014-
* centered on the throttle_delay time.
1015-
*
1016-
* 10000 implies a 9.2 (-log(1/10000)) to 0.0 (log 1) delay
1017-
* multiplier, and results in a 0.055 % target underestimation bias:
1018-
*
1019-
* SELECT 1.0/AVG(-LN(i/10000.0)) FROM generate_series(1,10000) AS i;
1020-
* = 1.000552717032611116335474
1031+
* Generate a delay such that the series of delays will approximate a
1032+
* Poisson distribution centered on the throttle_delay time.
10211033
*
10221034
* If transactions are too slow or a given wait is shorter than a
10231035
* transaction, the next transaction will start right away.
10241036
*/
1025-
int64wait= (int64) (throttle_delay*
1026-
1.00055271703*-log(getrand(thread,1,10000) /10000.0));
1037+
int64wait=getPoissonRand(thread,throttle_delay);
10271038

10281039
thread->throttle_trigger+=wait;
10291040

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp