From 044e29dd3472b6cbfca3915d445f484658effbb6 Mon Sep 17 00:00:00 2001 From: alonre24 Date: Sat, 2 Sep 2023 15:37:04 +0300 Subject: [PATCH] redis-benchmark - add the support for binary strings (#9414) Recently, the option of sending an argument from stdin using `-x` flag was added to redis-benchmark (this option is available in redis-cli as well). However, using the `-x` option for sending a blobs that contains null-characters doesn't work as expected - the argument is trimmed in the first occurrence of `\X00` (unlike in redis-cli). This PR aims to fix this issue and add the support for every binary string input, by sending arguments length to `redisFormatCommandArgv` when processing redis-benchmark command, so we won't treat the arguments as C-strings. Additionally, we add a simple test coverage for `-x` (without binary strings, and also remove an excessive server started in tests, and make sure to select db 0 so that `r` and the benchmark work on the same db. Co-authored-by: Oran Agra --- src/redis-benchmark.c | 7 ++++++- tests/integration/redis-benchmark.tcl | 12 ++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/redis-benchmark.c b/src/redis-benchmark.c index 1ee562f20..ed53dcd33 100644 --- a/src/redis-benchmark.c +++ b/src/redis-benchmark.c @@ -1888,8 +1888,12 @@ int main(int argc, char **argv) { sds_args[argc] = readArgFromStdin(); argc++; } + /* Setup argument length */ + size_t *argvlen = zmalloc(argc*sizeof(size_t)); + for (i = 0; i < argc; i++) + argvlen[i] = sdslen(sds_args[i]); do { - len = redisFormatCommandArgv(&cmd,argc,(const char**)sds_args,NULL); + len = redisFormatCommandArgv(&cmd,argc,(const char**)sds_args,argvlen); // adjust the datasize to the parsed command config.datasize = len; benchmark(title,cmd,len); @@ -1899,6 +1903,7 @@ int main(int argc, char **argv) { sdsfree(title); if (config.redis_config != NULL) freeRedisConfig(config.redis_config); + zfree(argvlen); return 0; } diff --git a/tests/integration/redis-benchmark.tcl b/tests/integration/redis-benchmark.tcl index 8035632c7..c3254408c 100644 --- a/tests/integration/redis-benchmark.tcl +++ b/tests/integration/redis-benchmark.tcl @@ -1,5 +1,5 @@ source tests/support/benchmark.tcl - +source tests/support/cli.tcl proc cmdstat {cmd} { return [cmdrstat $cmd r] @@ -25,10 +25,11 @@ proc default_set_get_checks {} { assert_match {} [cmdstat lrange] } -start_server {tags {"benchmark network external:skip logreqres:skip"}} { +tags {"benchmark network external:skip logreqres:skip"} { start_server {} { set master_host [srv 0 host] set master_port [srv 0 port] + r select 0 test {benchmark: set,get} { set cmd [redisbenchmark $master_host $master_port "-c 5 -n 10 -t set,get"] @@ -126,6 +127,13 @@ start_server {tags {"benchmark network external:skip logreqres:skip"}} { r config set maxclients $original_maxclients } + test {benchmark: read last argument from stdin} { + set base_cmd [redisbenchmark $master_host $master_port "-x -n 10 set key"] + set cmd "printf arg | $base_cmd" + common_bench_setup $cmd + r get key + } {arg} + # tls specific tests if {$::tls} { test {benchmark: specific tls-ciphers} {