Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dTNyn-0000MK-2u for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Jul 2017 07:47:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dTNym-0002VP-Li for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Jul 2017 07:47:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dTNxE-0008Eq-P0 for pgsql-hackers@postgresql.org; Fri, 07 Jul 2017 07:45:32 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dTNxB-0004Ds-0s for pgsql-hackers@postgresql.org; Fri, 07 Jul 2017 07:45:32 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 6776621C055B for ; Fri, 7 Jul 2017 10:45:28 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from [10.242.26.185] (unknown [217.23.185.36]) by mail.postgrespro.ru (Postfix) with ESMTPSA id 344E821C03E3 for ; Fri, 7 Jul 2017 10:45:28 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1499413528; bh=tcXa/SPlTul37Zf6cOONLvp43BjXkoIIr3QZddr9uiA=; h=From:Subject:Date:To; b=K/zKByEaebIwF1qx5s1dWB2bG6kHnT5hkA8JxSzen3gNr+pwBaDLd6H8inta2aRgl kbXwp7HLz44/IkBLR3ckorf4wycfvJEnsj6fNHYPUR8Juh3zD5xsih4FZWn7l/zBFr oeFoBNJa68jmM18OTOzDyOETY5qKsCRUTFgX93m0= From: Alik Khilazhev Content-Type: multipart/mixed; boundary="Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC" Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: [WIP] Zipfian distribution in pgbench Message-Id: Date: Fri, 7 Jul 2017 10:45:29 +0300 To: pgsql-hackers@postgresql.org X-Mailer: Apple Mail (2.3273) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hello! PostgreSQL shows very bad results in YCSB Workload A (50% SELECT and 50% UP= DATE of random row by PK) on benchmarking with big number of clients using = Zipfian distribution. MySQL also has decline but it is not significant as i= t is in PostgreSQL. MongoDB does not have decline at all. And if pgbench wo= uld have Zipfian distribution random number generator, everyone will be abl= e to make research on this topic without using YCSB.=20 =20 This is the reason why I am currently working on random_zipfian function. The bottleneck of algorithm that I use is that it calculates zeta function = (it has linear complexity - https://en.wikipedia.org/wiki/Riemann_zeta_func= tion). It my cause problems on generating huge amount of big numbers.=20 That=E2=80=99s why I added caching for zeta value. And it works good for ca= ses when random_zipfian called with same parameters in script. For example: =E2=80=A6=20 \set a random_zipfian(1, 100, 1.2) \set b random_zipfian(1, 100, 1.2) =E2=80=A6 In other case, second call will override cache of first and caching does no= t make any sense: =E2=80=A6 \set a random_zipfian(1, 100, 1.2) \set b random_zipfian(1, 200, 1.4) =E2=80=A6=20 That=E2=80=99s why I have a question: should I implement support of caching= zeta values for calls with different parameters, or not?=20 P.S. I attaching patch and script - analogue of YCSB Workload A. Run benchmark with command: $ pgbench -f ycsb_read_zipf.sql -f ycsb_update_zipf.sql On scale =3D 10(1 million rows) it gives following results on machine with = 144 cores(with synchronous_commit=3Doff): nclients tps 1 8842.401870 2 18358.140869 4 45999.378785 8 88713.743199 16 170166.998212 32 290069.221493 64 178128.030553 128 88712.825602 256 38364.937573 512 13512.765878 1000 6188.136736 --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Disposition: attachment; filename=ycsb_read_zipf.sql Content-Type: application/sql; x-unix-mode=0644; name="ycsb_read_zipf.sql" Content-Transfer-Encoding: 7bit \set i random_zipfian(1, 100000 * :scale, 1.01) SELECT abalance FROM pgbench_accounts WHERE aid = :i; --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Disposition: attachment; filename=pgbench-zipf-01v.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="pgbench-zipf-01v.patch" Content-Transfer-Encoding: 7bit diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml index 64b043b48a..1dea6e4b17 100644 --- a/doc/src/sgml/ref/pgbench.sgml +++ b/doc/src/sgml/ref/pgbench.sgml @@ -1014,6 +1014,14 @@ pgbench options dbname an integer between 1 and 10 + random_zipfian(lb, ub, parameter) + integer + Zipfian-distributed random integer in [lb, ub], + see below + random_zipfian(1, 10, 1.2) + an integer between 1 and 10 + + sqrt(x) double square root @@ -1094,6 +1102,24 @@ f(x) = PHI(2.0 * parameter * (x - mu) / (max - min + 1)) / of the Box-Muller transform. + + + + For Zipfian distribution, parameter + defines how skewed the distribution. A physical sense of this parameter + is following: 1 generated N times, + 2 generated 2 ^ parameter less, + 3 generated 3 ^ parameter less, ... + X generated X ^ parameter less times. + + + Intuitively, the larger the parameter, the more + frequently value values to the beginning of the interval are drawn. + The closer to 0 parameter is, + the flatter (more uniform) the access distribution. + + + diff --git a/src/bin/pgbench/exprparse.y b/src/bin/pgbench/exprparse.y index b3a2d9bfd3..25d5ad48e5 100644 --- a/src/bin/pgbench/exprparse.y +++ b/src/bin/pgbench/exprparse.y @@ -191,6 +191,9 @@ static const struct { "random_exponential", 3, PGBENCH_RANDOM_EXPONENTIAL }, + { + "random_zipfian", 3, PGBENCH_RANDOM_ZIPFIAN + }, /* keep as last array element */ { NULL, 0, 0 diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 4d364a1db4..9e515abe9b 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -94,6 +94,7 @@ static int pthread_join(pthread_t th, void **thread_return); #define DEFAULT_NXACTS 10 /* default nxacts */ #define MIN_GAUSSIAN_PARAM 2.0 /* minimum parameter for gauss */ +#define MIN_ZIPFIAN_PARAM 1.0000001 /* minimum parameter for zipfian */ int nxacts = 0; /* number of transactions per client */ int duration = 0; /* duration in seconds */ @@ -184,6 +185,14 @@ char *dbName; char *logfile_prefix = NULL; const char *progname; +/* Variables used in zipfian_random */ +double zipf_zetan; /* zeta(n) */ +double zipf_prev_theta; /* theta parameter of previous execution */ +double zipf_prev_nitems; /* n(ub - lb + 1) parameter of previous execution */ +double zipf_alpha; +double zipf_beta; +double zipf_eta; + #define WSEP '@' /* weight separator */ volatile bool timer_exceeded = false; /* flag from signal handler */ @@ -737,6 +746,58 @@ getPoissonRand(TState *thread, int64 center) return (int64) (-log(uniform) * ((double) center) + 0.5); } +/* helper function for getZipfianRand */ +static double +zeta(int64 n, double theta) +{ + int i; + double ans = 0.0; + + for (i = 1; i <= n; i++) + ans += pow(1. / (double)i, theta); + return (ans); +} + +/* helper function for getZipfianRand. Computes random variable in range 1..n */ +static int64 +zipfn(TState *thread, int64 n, double theta) +{ + double u = pg_erand48(thread->random_state); + double uz = u * zipf_zetan; + + if (uz < 1.) + return 1; + if (uz < 1 + zipf_beta) + return 2; + return 1 + (int64)(n * pow(zipf_eta * u - zipf_eta + 1., zipf_alpha)); +} + +/* random number generator: zipfian distribution from min to max inclusive */ +static int64 +getZipfianRand(TState *thread, int64 min, int64 max, double theta) +{ + Assert(theta > MIN_ZIPFIAN_PARAM); + + int64 n = max - min + 1; + double zipf_zeta2; + + /* update cached variables if current parameters are different from previous */ + if (n != zipf_prev_nitems || theta != zipf_prev_theta) + { + zipf_prev_theta = theta; + zipf_prev_nitems = n; + + zipf_zeta2 = zeta(2, theta); + zipf_zetan = zeta(n, theta); + + zipf_alpha = 1. / (1 - theta); + zipf_beta = pow(0.5, theta); + zipf_eta = (1. - pow(2. / n, 1 - theta)) / (1. - zipf_zeta2 / zipf_zetan); + } + + return min + zipfn(thread, n, theta) - 1; +} + /* * Initialize the given SimpleStats struct to all zeroes */ @@ -1567,6 +1628,7 @@ evalFunc(TState *thread, CState *st, case PGBENCH_RANDOM: case PGBENCH_RANDOM_EXPONENTIAL: case PGBENCH_RANDOM_GAUSSIAN: + case PGBENCH_RANDOM_ZIPFIAN: { int64 imin, imax; @@ -1617,6 +1679,18 @@ evalFunc(TState *thread, CState *st, setIntValue(retval, getGaussianRand(thread, imin, imax, param)); } + else if (func == PGBENCH_RANDOM_ZIPFIAN) + { + if (param <= MIN_ZIPFIAN_PARAM) + { + fprintf(stderr, + "zipfian parameter must be greater than %f " + "(not %f)\n", MIN_ZIPFIAN_PARAM, param); + return false; + } + setIntValue(retval, + getZipfianRand(thread, imin, imax, param)); + } else /* exponential */ { if (param <= 0.0) diff --git a/src/bin/pgbench/pgbench.h b/src/bin/pgbench/pgbench.h index abc13e9463..1a29f1260c 100644 --- a/src/bin/pgbench/pgbench.h +++ b/src/bin/pgbench/pgbench.h @@ -75,7 +75,8 @@ typedef enum PgBenchFunction PGBENCH_SQRT, PGBENCH_RANDOM, PGBENCH_RANDOM_GAUSSIAN, - PGBENCH_RANDOM_EXPONENTIAL + PGBENCH_RANDOM_EXPONENTIAL, + PGBENCH_RANDOM_ZIPFIAN } PgBenchFunction; typedef struct PgBenchExpr PgBenchExpr; --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Disposition: attachment; filename=ycsb_update_zipf.sql Content-Type: application/sql; x-unix-mode=0644; name="ycsb_update_zipf.sql" Content-Transfer-Encoding: 7bit \set i random_zipfian(1, 100000 * :scale, 1.01) UPDATE pgbench_accounts SET abalance = 1 WHERE aid = :i; --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 =E2=80=94 Thanks and Regards, Alik Khilazhev Postgres Professional: http://www.postgrespro.com The Russian Postgres Company= --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --Apple-Mail=_4CD750A0-15A9-4850-97AB-B91F1CA14FBC--