Merge lp:~vadim-tk/sysbench/zipf-distribution into lp:sysbench

Proposed by Alexey Kopytov on 2012-05-04
Status: Needs review
Proposed branch: lp:~vadim-tk/sysbench/zipf-distribution
Merge into: lp:sysbench
Diff against target: 158 lines (+94/-2)
2 files modified
sysbench/sysbench.c (+91/-2)
sysbench/sysbench.h (+3/-0)
To merge this branch: bzr merge lp:~vadim-tk/sysbench/zipf-distribution
Reviewer Review Type Date Requested Status
Alexey Kopytov Pending
Review via email: mp+104775@code.launchpad.net
To post a comment you must log in.
110. By Vadim Tkachenko on 2012-05-11

Added self-similar distribution

111. By Vadim Tkachenko on 2012-05-11

Renamed self-similar into Pareto

Unmerged revisions

111. By Vadim Tkachenko on 2012-05-11

Renamed self-similar into Pareto

110. By Vadim Tkachenko on 2012-05-11

Added self-similar distribution

109. By root <email address hidden> on 2012-04-30

change default theta for zipf

108. By root <email address hidden> on 2012-04-30

Initial naive implementation of Zipf distribution

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'sysbench/sysbench.c'
--- sysbench/sysbench.c 2012-03-21 08:20:02 +0000
+++ sysbench/sysbench.c 2012-05-11 19:27:18 +0000
@@ -81,7 +81,9 @@
81{81{
82 DIST_TYPE_UNIFORM,82 DIST_TYPE_UNIFORM,
83 DIST_TYPE_GAUSSIAN,83 DIST_TYPE_GAUSSIAN,
84 DIST_TYPE_SPECIAL84 DIST_TYPE_SPECIAL,
85 DIST_TYPE_ZIPF,
86 DIST_TYPE_PARETO
85} rand_dist_t;87} rand_dist_t;
8688
87/* Event queue data type for the tx-rate mode */89/* Event queue data type for the tx-rate mode */
@@ -99,6 +101,16 @@
99static unsigned int rand_res;101static unsigned int rand_res;
100static int rand_seed; /* optional seed set on the command line */102static int rand_seed; /* optional seed set on the command line */
101103
104/* parameters for zipf distribution */
105static double zipf_theta; /* parameter theta */
106static unsigned int zipf_nitems = 0; /* number of items to choose from */
107static double zipf_zetan; /* precalculated ZetaN, based on nitems */
108static double zipf_zeta2; /* precalculated Zeta2, based on theta */
109
110/* parameters for Pareto distribution */
111static double pareto_h; /* parameter h */
112static double pareto_power; /* parameter pre-calculated by h */
113
102/* Random seed used to generate unique random numbers */114/* Random seed used to generate unique random numbers */
103static unsigned long long rnd_seed;115static unsigned long long rnd_seed;
104/* Mutex to protect random seed */116/* Mutex to protect random seed */
@@ -131,14 +143,16 @@
131 {"help", "print help and exit", SB_ARG_TYPE_FLAG, NULL},143 {"help", "print help and exit", SB_ARG_TYPE_FLAG, NULL},
132 {"version", "print version and exit", SB_ARG_TYPE_FLAG, "off"},144 {"version", "print version and exit", SB_ARG_TYPE_FLAG, "off"},
133 {"rand-init", "initialize random number generator", SB_ARG_TYPE_FLAG, "off"},145 {"rand-init", "initialize random number generator", SB_ARG_TYPE_FLAG, "off"},
134 {"rand-type", "random numbers distribution {uniform,gaussian,special}", SB_ARG_TYPE_STRING,146 {"rand-type", "random numbers distribution {uniform,gaussian,special,zipf,pareto}", SB_ARG_TYPE_STRING,
135 "special"},147 "special"},
136 {"rand-spec-iter", "number of iterations used for numbers generation", SB_ARG_TYPE_INT, "12"},148 {"rand-spec-iter", "number of iterations used for numbers generation", SB_ARG_TYPE_INT, "12"},
137 {"rand-spec-pct", "percentage of values to be treated as 'special' (for special distribution)",149 {"rand-spec-pct", "percentage of values to be treated as 'special' (for special distribution)",
138 SB_ARG_TYPE_INT, "1"},150 SB_ARG_TYPE_INT, "1"},
139 {"rand-spec-res", "percentage of 'special' values to use (for special distribution)",151 {"rand-spec-res", "percentage of 'special' values to use (for special distribution)",
140 SB_ARG_TYPE_INT, "75"},152 SB_ARG_TYPE_INT, "75"},
153 {"rand-zipf-t", "parameter theta for zipf distibution", SB_ARG_TYPE_FLOAT, "1.16"},
141 {"rand-seed", "seed for random number generator, ignored when 0", SB_ARG_TYPE_INT, "0"},154 {"rand-seed", "seed for random number generator, ignored when 0", SB_ARG_TYPE_INT, "0"},
155 {"rand-pareto-h", "parameter h for pareto distibution", SB_ARG_TYPE_FLOAT, "0.2"},
142 {NULL, NULL, SB_ARG_TYPE_NULL, NULL}156 {NULL, NULL, SB_ARG_TYPE_NULL, NULL}
143};157};
144158
@@ -1020,6 +1034,16 @@
1020 rand_type = DIST_TYPE_SPECIAL;1034 rand_type = DIST_TYPE_SPECIAL;
1021 rand_func = &sb_rand_special;1035 rand_func = &sb_rand_special;
1022 }1036 }
1037 else if (!strcmp(s, "zipf"))
1038 {
1039 rand_type = DIST_TYPE_ZIPF;
1040 rand_func = &sb_rand_zipf;
1041 }
1042 else if (!strcmp(s, "pareto"))
1043 {
1044 rand_type = DIST_TYPE_PARETO;
1045 rand_func = &sb_rand_pareto;
1046 }
1023 else1047 else
1024 {1048 {
1025 log_text(LOG_FATAL, "Invalid random numbers distribution: %s.", s);1049 log_text(LOG_FATAL, "Invalid random numbers distribution: %s.", s);
@@ -1030,6 +1054,12 @@
1030 rand_pct = sb_get_value_int("rand-spec-pct");1054 rand_pct = sb_get_value_int("rand-spec-pct");
1031 rand_res = sb_get_value_int("rand-spec-res");1055 rand_res = sb_get_value_int("rand-spec-res");
10321056
1057 zipf_theta = sb_get_value_float("rand-zipf-t");
1058 zipf_zeta2 = sb_rand_zeta(2., zipf_theta);
1059
1060 pareto_h = sb_get_value_float("rand-pareto-h");
1061 pareto_power = log(pareto_h)/log(1.0-pareto_h);
1062
1033 sb_globals.tx_rate = sb_get_value_int("tx-rate");1063 sb_globals.tx_rate = sb_get_value_int("tx-rate");
1034 sb_globals.report_interval = sb_get_value_int("report-interval");1064 sb_globals.report_interval = sb_get_value_int("report-interval");
10351065
@@ -1226,6 +1256,65 @@
1226 return a + sum / rand_iter;1256 return a + sum / rand_iter;
1227}1257}
12281258
1259/* Pareto distribution */
1260
1261int sb_rand_pareto(int a, int b)
1262{
1263 double randf;
1264 randf = (double) sb_rnd() / (double) SB_MAX_RND;
1265
1266 return a + (int)(b - a + 1) * pow(randf, pareto_power);
1267}
1268
1269
1270/* zipf distribution */
1271
1272/* aux function to calculate zeta */
1273double sb_rand_zeta(int n, double theta)
1274{
1275 int i;
1276 double ans=0.0;
1277
1278 for (i=1; i <= n; i++)
1279 ans += pow(1./(double)i, theta);
1280 return(ans);
1281}
1282
1283
1284int sb_rand_zipf(int a, int b)
1285{
1286
1287 double alpha, eta, rand_uni, rand_z;
1288 unsigned int n;
1289 unsigned int val;
1290
1291 n = b - a + 1;
1292
1293 /* we pre-cache zipf_zetan, as calculation is slow */
1294 if (n != zipf_nitems)
1295 {
1296 zipf_zetan = sb_rand_zeta(n, zipf_theta);
1297 zipf_nitems = n;
1298 }
1299
1300 alpha = 1. / (1. - zipf_theta);
1301 eta = (1. - pow(2./n, 1. - zipf_theta)) / (1. - zipf_zeta2/zipf_zetan);
1302
1303 rand_uni = (double) sb_rnd() / (double) SB_MAX_RND;
1304 rand_z = rand_uni * zipf_zetan;
1305
1306 if (rand_z < 1.)
1307 {
1308 val = 1;
1309 } else if (rand_z < (1. + pow(0.5, zipf_theta))) {
1310 val = 2;
1311 } else {
1312 val = 1 + (unsigned int)(n * pow(eta*rand_uni - eta + 1., alpha));
1313 }
1314
1315 return a + val - 1;
1316}
1317
1229/* 'special' distribution */1318/* 'special' distribution */
12301319
1231int sb_rand_special(int a, int b)1320int sb_rand_special(int a, int b)
12321321
=== modified file 'sysbench/sysbench.h'
--- sysbench/sysbench.h 2012-03-18 22:35:16 +0000
+++ sysbench/sysbench.h 2012-05-11 19:27:18 +0000
@@ -227,6 +227,9 @@
227int sb_rand_uniform(int, int);227int sb_rand_uniform(int, int);
228int sb_rand_gaussian(int, int);228int sb_rand_gaussian(int, int);
229int sb_rand_special(int, int);229int sb_rand_special(int, int);
230int sb_rand_zipf(int, int);
231int sb_rand_pareto(int, int);
232double sb_rand_zeta(int n, double theta);
230int sb_rand_uniq(int a, int b);233int sb_rand_uniq(int a, int b);
231void sb_rand_str(const char *, char *);234void sb_rand_str(const char *, char *);
232235

Subscribers

People subscribed via source and target branches

to status/vote changes: