Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --sequential to valkey-benchmark (for populating entire keyspace) #1839

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions src/valkey-benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ static struct config {
long long previous_tick;
int keysize;
int datasize;
int randomkeys;
int randomkeys_keyspacelen;
int replacekeys;
int keyspacelen;
int sequential_replacement;
int keepalive;
int pipeline;
long long start;
Expand Down Expand Up @@ -393,18 +394,23 @@ static void resetClient(client c) {
c->pending = config.pipeline;
}

static void randomizeClientKey(client c) {
size_t i;

for (i = 0; i < c->randlen; i++) {
static void generateClientKey(client c) {
static _Atomic size_t seq_key = 0;
for (size_t i = 0; i < c->randlen; i++) {
char *p = c->randptr[i] + 11;
size_t r = 0;
if (config.randomkeys_keyspacelen != 0) r = random() % config.randomkeys_keyspacelen;
size_t j;
size_t key = 0;
if (config.keyspacelen != 0) {
if (config.sequential_replacement) {
key = atomic_fetch_add_explicit(&seq_key, 1, memory_order_relaxed);
} else {
key = random();
}
key %= config.keyspacelen;
}

for (j = 0; j < 12; j++) {
*p = '0' + r % 10;
r /= 10;
for (size_t j = 0; j < 12; j++) {
*p = '0' + key % 10;
key /= 10;
p--;
}
}
Expand Down Expand Up @@ -577,8 +583,8 @@ static void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
return;
}

/* Really initialize: randomize keys and set start time. */
if (config.randomkeys) randomizeClientKey(c);
/* Really initialize: replace keys and set start time. */
if (config.replacekeys) generateClientKey(c);
if (config.cluster_mode && c->staglen > 0) setClusterKeyHashTag(c);
c->slots_last_update = atomic_load_explicit(&config.slots_last_update, memory_order_relaxed);
c->start = ustime();
Expand Down Expand Up @@ -749,8 +755,8 @@ static client createClient(char *cmd, size_t len, client from, int thread_id) {
c->stagptr = NULL;
c->staglen = 0;

/* Find substrings in the output buffer that need to be randomized. */
if (config.randomkeys) {
/* Find substrings in the output buffer that need to be replaced. */
if (config.replacekeys) {
if (from) {
c->randlen = from->randlen;
c->randfree = 0;
Expand Down Expand Up @@ -1366,9 +1372,11 @@ int parseOptions(int argc, char **argv) {
p++;
if (*p < '0' || *p > '9') goto invalid;
}
config.randomkeys = 1;
config.randomkeys_keyspacelen = atoi(next);
if (config.randomkeys_keyspacelen < 0) config.randomkeys_keyspacelen = 0;
config.replacekeys = 1;
config.keyspacelen = atoi(next);
if (config.keyspacelen < 0) config.keyspacelen = 0;
} else if (!strcmp(argv[i], "--sequential")) {
config.sequential_replacement = 1;
} else if (!strcmp(argv[i], "-q")) {
config.quiet = 1;
} else if (!strcmp(argv[i], "--csv")) {
Expand Down Expand Up @@ -1541,14 +1549,15 @@ int parseOptions(int argc, char **argv) {
" -k <boolean> 1=keep alive 0=reconnect (default 1)\n"
" -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD,\n"
" random members and scores for ZADD.\n"
" Using this option the benchmark will expand the string\n"
" __rand_int__ inside an argument with a 12 digits number in\n"
" Using this option the benchmark will replace the string\n"
" __rand_int__ inside an argument with a random 12 digit number in\n"
" the specified range from 0 to keyspacelen-1. The\n"
" substitution changes every time a command is executed.\n"
" Default tests use this to hit random keys in the specified\n"
" range.\n"
" Note: If -r is omitted, all commands in a benchmark will\n"
" use the same key.\n"
" --sequential modifies -r argument to replace with sequential 12 digit numbers.\n"
" -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline).\n"
" -q Quiet. Just show query/sec values\n"
" --precision Number of decimal places to display in latency output (default 0)\n"
Expand Down Expand Up @@ -1703,8 +1712,9 @@ int main(int argc, char **argv) {
config.keepalive = 1;
config.datasize = 3;
config.pipeline = 1;
config.randomkeys = 0;
config.randomkeys_keyspacelen = 0;
config.replacekeys = 0;
config.keyspacelen = 0;
config.sequential_replacement = 0;
config.quiet = 0;
config.csv = 0;
config.loop = 0;
Expand Down Expand Up @@ -1948,7 +1958,7 @@ int main(int argc, char **argv) {

if (test_is_selected("zadd")) {
char *score = "0";
if (config.randomkeys) score = "__rand_int__";
if (config.replacekeys) score = "__rand_int__";
len = redisFormatCommand(&cmd, "ZADD myzset%s %s element:__rand_int__", tag, score);
benchmark("ZADD", cmd, len);
free(cmd);
Expand Down
Loading