-
Notifications
You must be signed in to change notification settings - Fork 217
/
memtier_benchmark.cpp
executable file
·1751 lines (1606 loc) · 71.7 KB
/
memtier_benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011-2019 Redis Labs Ltd.
*
* This file is part of memtier_benchmark.
*
* memtier_benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* memtier_benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with memtier_benchmark. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#include <getopt.h>
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef USE_TLS
#include <openssl/crypto.h>
#include <openssl/conf.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#define REDIS_TLS_PROTO_TLSv1 (1<<0)
#define REDIS_TLS_PROTO_TLSv1_1 (1<<1)
#define REDIS_TLS_PROTO_TLSv1_2 (1<<2)
#define REDIS_TLS_PROTO_TLSv1_3 (1<<3)
/* Use safe defaults */
#ifdef TLS1_3_VERSION
#define REDIS_TLS_PROTO_DEFAULT (REDIS_TLS_PROTO_TLSv1_2|REDIS_TLS_PROTO_TLSv1_3)
#else
#define REDIS_TLS_PROTO_DEFAULT (REDIS_TLS_PROTO_TLSv1_2)
#endif
#endif
#include <cstring>
#include <stdexcept>
#include "client.h"
#include "JSON_handler.h"
#include "obj_gen.h"
#include "memtier_benchmark.h"
static int log_level = 0;
void benchmark_log_file_line(int level, const char *filename, unsigned int line, const char *fmt, ...)
{
if (level > log_level)
return;
va_list args;
char fmtbuf[1024];
snprintf(fmtbuf, sizeof(fmtbuf)-1, "%s:%u: ", filename, line);
strcat(fmtbuf, fmt);
va_start(args, fmt);
vfprintf(stderr, fmtbuf, args);
va_end(args);
}
void benchmark_log(int level, const char *fmt, ...)
{
if (level > log_level)
return;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
bool is_redis_protocol(enum PROTOCOL_TYPE type) {
return (type == PROTOCOL_REDIS_DEFAULT || type == PROTOCOL_RESP2 || type == PROTOCOL_RESP3);
}
static const char * get_protocol_name(enum PROTOCOL_TYPE type) {
if (type == PROTOCOL_REDIS_DEFAULT) return "redis";
else if (type == PROTOCOL_RESP2) return "resp2";
else if (type == PROTOCOL_RESP3) return "resp3";
else if (type == PROTOCOL_MEMCACHE_TEXT) return "memcache_text";
else if (type == PROTOCOL_MEMCACHE_BINARY) return "memcache_binary";
else return "none";
}
static void config_print(FILE *file, struct benchmark_config *cfg)
{
char tmpbuf[512];
fprintf(file,
"server = %s\n"
"port = %u\n"
"unix socket = %s\n"
"address family = %s\n"
"protocol = %s\n"
#ifdef USE_TLS
"tls = %s\n"
"cert = %s\n"
"key = %s\n"
"cacert = %s\n"
"tls_skip_verify = %s\n"
"sni = %s\n"
#endif
"out_file = %s\n"
"client_stats = %s\n"
"run_count = %u\n"
"debug = %u\n"
"requests = %llu\n"
"rate_limit = %u\n"
"clients = %u\n"
"threads = %u\n"
"test_time = %u\n"
"ratio = %u:%u\n"
"pipeline = %u\n"
"data_size = %u\n"
"data_offset = %u\n"
"random_data = %s\n"
"data_size_range = %u-%u\n"
"data_size_list = %s\n"
"data_size_pattern = %s\n"
"expiry_range = %u-%u\n"
"data_import = %s\n"
"data_verify = %s\n"
"verify_only = %s\n"
"generate_keys = %s\n"
"key_prefix = %s\n"
"key_minimum = %llu\n"
"key_maximum = %llu\n"
"key_pattern = %s\n"
"key_stddev = %f\n"
"key_median = %f\n"
"reconnect_interval = %u\n"
"multi_key_get = %u\n"
"authenticate = %s\n"
"select-db = %d\n"
"no-expiry = %s\n"
"wait-ratio = %u:%u\n"
"num-slaves = %u-%u\n"
"wait-timeout = %u-%u\n"
"json-out-file = %s\n",
cfg->server,
cfg->port,
cfg->unix_socket,
cfg->resolution == AF_UNSPEC ? "Unspecified" : cfg->resolution == AF_INET ? "AF_INET" : "AF_INET6",
get_protocol_name(cfg->protocol),
#ifdef USE_TLS
cfg->tls ? "yes" : "no",
cfg->tls_cert,
cfg->tls_key,
cfg->tls_cacert,
cfg->tls_skip_verify ? "yes" : "no",
cfg->tls_sni,
#endif
cfg->out_file,
cfg->client_stats,
cfg->run_count,
cfg->debug,
cfg->requests,
cfg->request_rate,
cfg->clients,
cfg->threads,
cfg->test_time,
cfg->ratio.a, cfg->ratio.b,
cfg->pipeline,
cfg->data_size,
cfg->data_offset,
cfg->random_data ? "yes" : "no",
cfg->data_size_range.min, cfg->data_size_range.max,
cfg->data_size_list.print(tmpbuf, sizeof(tmpbuf)-1),
cfg->data_size_pattern,
cfg->expiry_range.min, cfg->expiry_range.max,
cfg->data_import,
cfg->data_verify ? "yes" : "no",
cfg->verify_only ? "yes" : "no",
cfg->generate_keys ? "yes" : "no",
cfg->key_prefix,
cfg->key_minimum,
cfg->key_maximum,
cfg->key_pattern,
cfg->key_stddev,
cfg->key_median,
cfg->reconnect_interval,
cfg->multi_key_get,
cfg->authenticate ? cfg->authenticate : "",
cfg->select_db,
cfg->no_expiry ? "yes" : "no",
cfg->wait_ratio.a, cfg->wait_ratio.b,
cfg->num_slaves.min, cfg->num_slaves.max,
cfg->wait_timeout.min, cfg->wait_timeout.max,
cfg->json_out_file);
}
static void config_print_to_json(json_handler * jsonhandler, struct benchmark_config *cfg)
{
char tmpbuf[512];
jsonhandler->open_nesting("configuration");
jsonhandler->write_obj("server" ,"\"%s\"", cfg->server);
jsonhandler->write_obj("port" ,"%u", cfg->port);
jsonhandler->write_obj("unix socket" ,"\"%s\"", cfg->unix_socket);
jsonhandler->write_obj("address family" ,"\"%s\"", cfg->resolution == AF_UNSPEC ? "Unspecified" : cfg->resolution == AF_INET ? "AF_INET" : "AF_INET6");
jsonhandler->write_obj("protocol" ,"\"%s\"", get_protocol_name(cfg->protocol));
jsonhandler->write_obj("out_file" ,"\"%s\"", cfg->out_file);
#ifdef USE_TLS
jsonhandler->write_obj("tls" ,"\"%s\"", cfg->tls ? "true" : "false");
jsonhandler->write_obj("cert" ,"\"%s\"", cfg->tls_cert);
jsonhandler->write_obj("key" ,"\"%s\"", cfg->tls_key);
jsonhandler->write_obj("cacert" ,"\"%s\"", cfg->tls_cacert);
jsonhandler->write_obj("tls_skip_verify" ,"\"%s\"", cfg->tls_skip_verify ? "true" : "false");
jsonhandler->write_obj("sni" ,"\"%s\"", cfg->tls_sni);
#endif
jsonhandler->write_obj("client_stats" ,"\"%s\"", cfg->client_stats);
jsonhandler->write_obj("run_count" ,"%u", cfg->run_count);
jsonhandler->write_obj("debug" ,"%u", cfg->debug);
jsonhandler->write_obj("requests" ,"%llu", cfg->requests);
jsonhandler->write_obj("rate_limit" ,"%u", cfg->request_rate);
jsonhandler->write_obj("clients" ,"%u", cfg->clients);
jsonhandler->write_obj("threads" ,"%u", cfg->threads);
jsonhandler->write_obj("test_time" ,"%u", cfg->test_time);
jsonhandler->write_obj("ratio" ,"\"%u:%u\"", cfg->ratio.a, cfg->ratio.b);
jsonhandler->write_obj("pipeline" ,"%u", cfg->pipeline);
jsonhandler->write_obj("data_size" ,"%u", cfg->data_size);
jsonhandler->write_obj("data_offset" ,"%u", cfg->data_offset);
jsonhandler->write_obj("random_data" ,"\"%s\"", cfg->random_data ? "true" : "false");
jsonhandler->write_obj("data_size_range" ,"\"%u:%u\"", cfg->data_size_range.min, cfg->data_size_range.max);
jsonhandler->write_obj("data_size_list" ,"\"%s\"", cfg->data_size_list.print(tmpbuf, sizeof(tmpbuf)-1));
jsonhandler->write_obj("data_size_pattern" ,"\"%s\"", cfg->data_size_pattern);
jsonhandler->write_obj("expiry_range" ,"\"%u:%u\"", cfg->expiry_range.min, cfg->expiry_range.max);
jsonhandler->write_obj("data_import" ,"\"%s\"", cfg->data_import);
jsonhandler->write_obj("data_verify" ,"\"%s\"", cfg->data_verify ? "true" : "false");
jsonhandler->write_obj("verify_only" ,"\"%s\"", cfg->verify_only ? "true" : "false");
jsonhandler->write_obj("generate_keys" ,"\"%s\"", cfg->generate_keys ? "true" : "false");
jsonhandler->write_obj("key_prefix" ,"\"%s\"", cfg->key_prefix);
jsonhandler->write_obj("key_minimum" ,"%11u", cfg->key_minimum);
jsonhandler->write_obj("key_maximum" ,"%11u", cfg->key_maximum);
jsonhandler->write_obj("key_pattern" ,"\"%s\"", cfg->key_pattern);
jsonhandler->write_obj("key_stddev" ,"%f", cfg->key_stddev);
jsonhandler->write_obj("key_median" ,"%f", cfg->key_median);
jsonhandler->write_obj("reconnect_interval","%u", cfg->reconnect_interval);
jsonhandler->write_obj("multi_key_get" ,"%u", cfg->multi_key_get);
jsonhandler->write_obj("authenticate" ,"\"%s\"", cfg->authenticate ? cfg->authenticate : "");
jsonhandler->write_obj("select-db" ,"%d", cfg->select_db);
jsonhandler->write_obj("no-expiry" ,"\"%s\"", cfg->no_expiry ? "true" : "false");
jsonhandler->write_obj("wait-ratio" ,"\"%u:%u\"", cfg->wait_ratio.a, cfg->wait_ratio.b);
jsonhandler->write_obj("num-slaves" ,"\"%u:%u\"", cfg->num_slaves.min, cfg->num_slaves.max);
jsonhandler->write_obj("wait-timeout" ,"\"%u-%u\"", cfg->wait_timeout.min, cfg->wait_timeout.max);
jsonhandler->close_nesting();
}
static void config_init_defaults(struct benchmark_config *cfg)
{
if (!cfg->server && !cfg->unix_socket)
cfg->server = "localhost";
if (!cfg->port && !cfg->unix_socket)
cfg->port = 6379;
if (!cfg->resolution)
cfg->resolution = AF_UNSPEC;
if (!cfg->run_count)
cfg->run_count = 1;
if (!cfg->clients)
cfg->clients = 50;
if (!cfg->threads)
cfg->threads = 4;
if (!cfg->ratio.is_defined())
cfg->ratio = config_ratio("1:10");
if (!cfg->pipeline)
cfg->pipeline = 1;
if (!cfg->data_size && !cfg->data_size_list.is_defined() && !cfg->data_size_range.is_defined() && !cfg->data_import)
cfg->data_size = 32;
if (cfg->generate_keys || !cfg->data_import) {
if (!cfg->key_prefix)
cfg->key_prefix = "memtier-";
if (!cfg->key_maximum)
cfg->key_maximum = 10000000;
}
if (!cfg->key_pattern)
cfg->key_pattern = "R:R";
if (!cfg->data_size_pattern)
cfg->data_size_pattern = "R";
if (cfg->requests == (unsigned long long)-1) {
cfg->requests = cfg->key_maximum - cfg->key_minimum;
if (strcmp(cfg->key_pattern, "P:P")==0)
cfg->requests = cfg->requests / (cfg->clients * cfg->threads) + 1;
printf("setting requests to %llu\n", cfg->requests);
}
if (!cfg->requests && !cfg->test_time)
cfg->requests = 10000;
if (!cfg->hdr_prefix)
cfg->hdr_prefix = "";
if (!cfg->print_percentiles.is_defined())
cfg->print_percentiles = config_quantiles("50,99,99.9");
#ifdef USE_TLS
if (!cfg->tls_protocols)
cfg->tls_protocols = REDIS_TLS_PROTO_DEFAULT;
#endif
}
static int generate_random_seed()
{
int R = 0;
FILE* f = fopen("/dev/random", "r");
if (f)
{
size_t ignore = fread(&R, sizeof(R), 1, f);
fclose(f);
ignore++;//ignore warning
}
return (int)time(NULL)^getpid()^R;
}
static bool verify_cluster_option(struct benchmark_config *cfg) {
if (cfg->reconnect_interval) {
fprintf(stderr, "error: cluster mode dose not support reconnect-interval option.\n");
return false;
} else if (cfg->multi_key_get) {
fprintf(stderr, "error: cluster mode dose not support multi-key-get option.\n");
return false;
} else if (cfg->wait_ratio.is_defined()) {
fprintf(stderr, "error: cluster mode dose not support wait-ratio option.\n");
return false;
} else if (!is_redis_protocol(cfg->protocol)) {
fprintf(stderr, "error: cluster mode supported only in redis protocol.\n");
return false;
} else if (cfg->unix_socket) {
fprintf(stderr, "error: cluster mode dose not support unix-socket option.\n");
return false;
}
return true;
}
static bool verify_arbitrary_command_option(struct benchmark_config *cfg) {
if (cfg->key_pattern) {
fprintf(stderr, "error: when using arbitrary command, key pattern is configured with --command-key-pattern option.\n");
return false;
} else if (cfg->ratio.is_defined()) {
fprintf(stderr, "error: when using arbitrary command, ratio is configured with --command-ratio option.\n");
return false;
}
// verify that when using Parallel key pattern, it's configured to all commands
size_t parallel_count = 0;
for (size_t i = 0; i<cfg->arbitrary_commands->size(); i++) {
arbitrary_command& cmd = cfg->arbitrary_commands->at(i);
if (cmd.key_pattern == 'P') {
parallel_count++;
}
}
if (parallel_count > 0 && parallel_count != cfg->arbitrary_commands->size()) {
fprintf(stderr, "error: parallel key-pattern must be configured to all commands.\n");
return false;
}
return true;
}
static int config_parse_args(int argc, char *argv[], struct benchmark_config *cfg)
{
enum extended_options {
o_test_time = 128,
o_ratio,
o_pipeline,
o_data_size_range,
o_data_size_list,
o_data_size_pattern,
o_data_offset,
o_expiry_range,
o_data_import,
o_data_verify,
o_verify_only,
o_key_prefix,
o_key_minimum,
o_key_maximum,
o_key_pattern,
o_key_stddev,
o_key_median,
o_show_config,
o_hide_histogram,
o_print_percentiles,
o_distinct_client_seed,
o_randomize,
o_client_stats,
o_reconnect_interval,
o_generate_keys,
o_multi_key_get,
o_select_db,
o_no_expiry,
o_wait_ratio,
o_num_slaves,
o_wait_timeout,
o_json_out_file,
o_cluster_mode,
o_command,
o_command_key_pattern,
o_command_ratio,
o_tls,
o_tls_cert,
o_tls_key,
o_tls_cacert,
o_tls_skip_verify,
o_tls_sni,
o_tls_protocols,
o_hdr_file_prefix,
o_rate_limiting,
o_help
};
static struct option long_options[] = {
{ "server", 1, 0, 's' },
{ "host", 1, 0, 'h' },
{ "port", 1, 0, 'p' },
{ "unix-socket", 1, 0, 'S' },
{ "ipv4", 0, 0, '4' },
{ "ipv6", 0, 0, '6' },
{ "protocol", 1, 0, 'P' },
#ifdef USE_TLS
{ "tls", 0, 0, o_tls },
{ "cert", 1, 0, o_tls_cert },
{ "key", 1, 0, o_tls_key },
{ "cacert", 1, 0, o_tls_cacert },
{ "tls-skip-verify", 0, 0, o_tls_skip_verify },
{ "sni", 1, 0, o_tls_sni },
{ "tls-protocols", 1, 0, o_tls_protocols },
#endif
{ "out-file", 1, 0, 'o' },
{ "hdr-file-prefix", 1, 0, o_hdr_file_prefix },
{ "client-stats", 1, 0, o_client_stats },
{ "run-count", 1, 0, 'x' },
{ "debug", 0, 0, 'D' },
{ "show-config", 0, 0, o_show_config },
{ "hide-histogram", 0, 0, o_hide_histogram },
{ "print-percentiles", 1, 0, o_print_percentiles },
{ "distinct-client-seed", 0, 0, o_distinct_client_seed },
{ "randomize", 0, 0, o_randomize },
{ "requests", 1, 0, 'n' },
{ "clients", 1, 0, 'c' },
{ "threads", 1, 0, 't' },
{ "test-time", 1, 0, o_test_time },
{ "ratio", 1, 0, o_ratio },
{ "pipeline", 1, 0, o_pipeline },
{ "data-size", 1, 0, 'd' },
{ "data-offset", 1, 0, o_data_offset },
{ "random-data", 0, 0, 'R' },
{ "data-size-range", 1, 0, o_data_size_range },
{ "data-size-list", 1, 0, o_data_size_list },
{ "data-size-pattern", 1, 0, o_data_size_pattern },
{ "expiry-range", 1, 0, o_expiry_range },
{ "data-import", 1, 0, o_data_import },
{ "data-verify", 0, 0, o_data_verify },
{ "verify-only", 0, 0, o_verify_only },
{ "generate-keys", 0, 0, o_generate_keys },
{ "key-prefix", 1, 0, o_key_prefix },
{ "key-minimum", 1, 0, o_key_minimum },
{ "key-maximum", 1, 0, o_key_maximum },
{ "key-pattern", 1, 0, o_key_pattern },
{ "key-stddev", 1, 0, o_key_stddev },
{ "key-median", 1, 0, o_key_median },
{ "reconnect-interval", 1, 0, o_reconnect_interval },
{ "multi-key-get", 1, 0, o_multi_key_get },
{ "authenticate", 1, 0, 'a' },
{ "select-db", 1, 0, o_select_db },
{ "no-expiry", 0, 0, o_no_expiry },
{ "wait-ratio", 1, 0, o_wait_ratio },
{ "num-slaves", 1, 0, o_num_slaves },
{ "wait-timeout", 1, 0, o_wait_timeout },
{ "json-out-file", 1, 0, o_json_out_file },
{ "cluster-mode", 0, 0, o_cluster_mode },
{ "help", 0, 0, o_help },
{ "version", 0, 0, 'v' },
{ "command", 1, 0, o_command },
{ "command-key-pattern", 1, 0, o_command_key_pattern },
{ "command-ratio", 1, 0, o_command_ratio },
{ "rate-limiting", 1, 0, o_rate_limiting },
{ NULL, 0, 0, 0 }
};
int option_index;
int c;
char *endptr;
while ((c = getopt_long(argc, argv,
"vs:S:p:P:o:x:DRn:c:t:d:a:h:46", long_options, &option_index)) != -1)
{
switch (c) {
case o_help:
return -1;
break;
case 'v':
puts(PACKAGE_STRING);
puts("Copyright (C) 2011-2024 Redis Ltd.");
puts("This is free software. You may redistribute copies of it under the terms of");
puts("the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.");
puts("There is NO WARRANTY, to the extent permitted by law.");
exit(0);
case 's':
case 'h':
cfg->server = optarg;
break;
case 'S':
cfg->unix_socket = optarg;
break;
case 'p':
endptr = NULL;
cfg->port = (unsigned short) strtoul(optarg, &endptr, 10);
if (!cfg->port || cfg->port > 65535 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: port must be a number in the range [1-65535].\n");
return -1;
}
break;
case '4':
cfg->resolution = AF_INET;
break;
case '6':
cfg->resolution = AF_INET6;
break;
case 'P':
if (strcmp(optarg, "redis") == 0) {
cfg->protocol = PROTOCOL_REDIS_DEFAULT;
} else if (strcmp(optarg, "resp2") == 0) {
cfg->protocol = PROTOCOL_RESP2;
} else if (strcmp(optarg, "resp3") == 0) {
cfg->protocol = PROTOCOL_RESP3;
} else if (strcmp(optarg, "memcache_text") == 0) {
cfg->protocol = PROTOCOL_MEMCACHE_TEXT;
} else if (strcmp(optarg, "memcache_binary") == 0) {
cfg->protocol = PROTOCOL_MEMCACHE_BINARY;
} else {
fprintf(stderr, "error: supported protocols are 'memcache_text', 'memcache_binary', 'redis', 'resp2' and resp3'.\n");
return -1;
}
break;
case 'o':
cfg->out_file = optarg;
break;
case o_hdr_file_prefix:
cfg->hdr_prefix = optarg;
break;
case o_client_stats:
cfg->client_stats = optarg;
break;
case 'x':
endptr = NULL;
cfg->run_count = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->run_count || !endptr || *endptr != '\0') {
fprintf(stderr, "error: run count must be greater than zero.\n");
return -1;
}
break;
case 'D':
cfg->debug++;
break;
case o_show_config:
cfg->show_config++;
break;
case o_hide_histogram:
cfg->hide_histogram++;
break;
case o_print_percentiles:
cfg->print_percentiles = config_quantiles(optarg);
if (!cfg->print_percentiles.is_defined()) {
fprintf(stderr, "error: quantiles must be expressed as [0.0-100.0],[0.0-100.0](,...) .\n");
return -1;
}
break;
case o_distinct_client_seed:
cfg->distinct_client_seed++;
break;
case o_randomize:
srandom(generate_random_seed());
cfg->randomize = random();
break;
case 'n':
endptr = NULL;
if (strcmp(optarg, "allkeys")==0)
cfg->requests = -1;
else {
cfg->requests = (unsigned long long) strtoull(optarg, &endptr, 10);
if (!cfg->requests || !endptr || *endptr != '\0') {
fprintf(stderr, "error: requests must be greater than zero.\n");
return -1;
}
if (cfg->test_time) {
fprintf(stderr, "error: --test-time and --requests are mutually exclusive.\n");
return -1;
}
}
break;
case 'c':
endptr = NULL;
cfg->clients = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->clients || !endptr || *endptr != '\0') {
fprintf(stderr, "error: clients must be greater than zero.\n");
return -1;
}
break;
case 't':
endptr = NULL;
cfg->threads = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->threads || !endptr || *endptr != '\0') {
fprintf(stderr, "error: threads must be greater than zero.\n");
return -1;
}
break;
case o_test_time:
endptr = NULL;
cfg->test_time = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->test_time || !endptr || *endptr != '\0') {
fprintf(stderr, "error: test time must be greater than zero.\n");
return -1;
}
if (cfg->requests) {
fprintf(stderr, "error: --test-time and --requests are mutually exclusive.\n");
return -1;
}
break;
case o_ratio:
cfg->ratio = config_ratio(optarg);
if (!cfg->ratio.is_defined()) {
fprintf(stderr, "error: ratio must be expressed as [0-n]:[0-n].\n");
return -1;
}
break;
case o_pipeline:
endptr = NULL;
cfg->pipeline = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->pipeline || !endptr || *endptr != '\0') {
fprintf(stderr, "error: pipeline must be greater than zero.\n");
return -1;
}
break;
case 'd':
endptr = NULL;
cfg->data_size = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->data_size || !endptr || *endptr != '\0') {
fprintf(stderr, "error: data-size must be greater than zero.\n");
return -1;
}
break;
case 'R':
cfg->random_data = true;
break;
case o_data_offset:
endptr = NULL;
cfg->data_offset = (unsigned int) strtoul(optarg, &endptr, 10);
if (!endptr || *endptr != '\0') {
fprintf(stderr, "error: data-offset must be greater than or equal to zero.\n");
return -1;
}
break;
case o_data_size_range:
cfg->data_size_range = config_range(optarg);
if (!cfg->data_size_range.is_defined() || cfg->data_size_range.min < 1) {
fprintf(stderr, "error: data-size-range must be expressed as [1-n]-[1-n].\n");
return -1;
}
break;
case o_data_size_list:
cfg->data_size_list = config_weight_list(optarg);
if (!cfg->data_size_list.is_defined()) {
fprintf(stderr, "error: data-size-list must be expressed as [size1:weight1],...[sizeN:weightN].\n");
return -1;
}
break;
case o_expiry_range:
cfg->expiry_range = config_range(optarg);
if (!cfg->expiry_range.is_defined()) {
fprintf(stderr, "error: expiry-range must be expressed as [0-n]-[1-n].\n");
return -1;
}
break;
case o_data_size_pattern:
cfg->data_size_pattern = optarg;
if (strlen(cfg->data_size_pattern) != 1 ||
(cfg->data_size_pattern[0] != 'R' && cfg->data_size_pattern[0] != 'S')) {
fprintf(stderr, "error: data-size-pattern must be either R or S.\n");
return -1;
}
break;
case o_data_import:
cfg->data_import = optarg;
break;
case o_data_verify:
cfg->data_verify = 1;
break;
case o_verify_only:
cfg->verify_only = 1;
cfg->data_verify = 1; // Implied
break;
case o_key_prefix:
cfg->key_prefix = optarg;
break;
case o_key_minimum:
endptr = NULL;
cfg->key_minimum = strtoull(optarg, &endptr, 10);
if (cfg->key_minimum < 1 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: key-minimum must be greater than zero.\n");
return -1;
}
break;
case o_key_maximum:
endptr = NULL;
cfg->key_maximum = strtoull(optarg, &endptr, 10);
if (cfg->key_maximum< 1 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: key-maximum must be greater than zero.\n");
return -1;
}
break;
case o_key_stddev:
endptr = NULL;
cfg->key_stddev = strtod(optarg, &endptr);
if (cfg->key_stddev<= 0 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: key-stddev must be greater than zero.\n");
return -1;
}
break;
case o_key_median:
endptr = NULL;
cfg->key_median = strtod(optarg, &endptr);
if (cfg->key_median<= 0 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: key-median must be greater than zero.\n");
return -1;
}
break;
case o_key_pattern:
cfg->key_pattern = optarg;
if (strlen(cfg->key_pattern) != 3 || cfg->key_pattern[key_pattern_delimiter] != ':' ||
(cfg->key_pattern[key_pattern_set] != 'R' &&
cfg->key_pattern[key_pattern_set] != 'S' &&
cfg->key_pattern[key_pattern_set] != 'G' &&
cfg->key_pattern[key_pattern_set] != 'P') ||
(cfg->key_pattern[key_pattern_get] != 'R' &&
cfg->key_pattern[key_pattern_get] != 'S' &&
cfg->key_pattern[key_pattern_get] != 'G' &&
cfg->key_pattern[key_pattern_get] != 'P')) {
fprintf(stderr, "error: key-pattern must be in the format of [S/R/G/P]:[S/R/G/P].\n");
return -1;
}
if ((cfg->key_pattern[key_pattern_set] == 'P' ||
cfg->key_pattern[key_pattern_get] == 'P') &&
(cfg->key_pattern[key_pattern_set] != cfg->key_pattern[key_pattern_get])) {
fprintf(stderr, "error: parallel key-pattern must be configured for both SET and GET commands.\n");
return -1;
}
break;
case o_reconnect_interval:
endptr = NULL;
cfg->reconnect_interval = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->reconnect_interval || !endptr || *endptr != '\0') {
fprintf(stderr, "error: reconnect-interval must be greater than zero.\n");
return -1;
}
break;
case o_generate_keys:
cfg->generate_keys = 1;
break;
case o_multi_key_get:
endptr = NULL;
cfg->multi_key_get = (unsigned int) strtoul(optarg, &endptr, 10);
if (cfg->multi_key_get <= 0 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: multi-key-get must be greater than zero.\n");
return -1;
}
break;
case 'a':
cfg->authenticate = optarg;
break;
case o_select_db:
cfg->select_db = (int) strtoul(optarg, &endptr, 10);
if (cfg->select_db < 0 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: select-db must be greater or equal zero.\n");
return -1;
}
break;
case o_no_expiry:
cfg->no_expiry = true;
break;
case o_wait_ratio:
cfg->wait_ratio = config_ratio(optarg);
if (!cfg->wait_ratio.is_defined()) {
fprintf(stderr, "error: wait-ratio must be expressed as [0-n]:[0-n].\n");
return -1;
}
break;
case o_num_slaves:
cfg->num_slaves = config_range(optarg);
if (!cfg->num_slaves.is_defined()) {
fprintf(stderr, "error: num-slaves must be expressed as [0-n]-[1-n].\n");
return -1;
}
break;
case o_wait_timeout:
cfg->wait_timeout = config_range(optarg);
if (!cfg->wait_timeout.is_defined()) {
fprintf(stderr, "error: wait-timeout must be expressed as [0-n]-[1-n].\n");
return -1;
}
break;
case o_json_out_file:
cfg->json_out_file = optarg;
break;
case o_cluster_mode:
cfg->cluster_mode = true;
break;
case o_command: {
// add new arbitrary command
arbitrary_command cmd(optarg);
if (cmd.split_command_to_args()) {
cfg->arbitrary_commands->add_command(cmd);
} else {
fprintf(stderr, "error: failed to parse arbitrary command.\n");
return -1;
}
break;
}
case o_command_key_pattern: {
if (cfg->arbitrary_commands->size() == 0) {
fprintf(stderr, "error: no arbitrary command found.\n");
return -1;
}
// command configuration always applied on last configured command
arbitrary_command& cmd = cfg->arbitrary_commands->get_last_command();
if (!cmd.set_key_pattern(optarg)) {
fprintf(stderr, "error: key-pattern for command %s must be in the format of [S/R/G/P].\n", cmd.command_name.c_str());
return -1;
}
break;
}
case o_command_ratio: {
if (cfg->arbitrary_commands->size() == 0) {
fprintf(stderr, "error: no arbitrary command found.\n");
return -1;
}
// command configuration always applied on last configured command
arbitrary_command& cmd = cfg->arbitrary_commands->get_last_command();
if (!cmd.set_ratio(optarg)) {
fprintf(stderr, "error: failed to set ratio for command %s.\n", cmd.command_name.c_str());
return -1;
}
break;
}
case o_rate_limiting: {
endptr = NULL;
cfg->request_rate = (unsigned int) strtoul(optarg, &endptr, 10);
if (!cfg->request_rate || !endptr || *endptr != '\0') {
fprintf(stderr, "error: rate must be greater than zero.\n");
return -1;
}
break;
}
#ifdef USE_TLS
case o_tls:
cfg->tls = true;
break;
case o_tls_cert:
cfg->tls_cert = optarg;
break;
case o_tls_key:
cfg->tls_key = optarg;
break;
case o_tls_cacert:
cfg->tls_cacert = optarg;
break;
case o_tls_skip_verify:
cfg->tls_skip_verify = true;
break;
case o_tls_sni:
cfg->tls_sni = optarg;
break;
case o_tls_protocols:
{
const char* tls_delimiter = ",";
char* tls_token = std::strtok(optarg, tls_delimiter);
while (tls_token != NULL) {
if (!strcasecmp(tls_token, "tlsv1"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1;
else if (!strcasecmp(tls_token, "tlsv1.1"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_1;
else if (!strcasecmp(tls_token, "tlsv1.2"))
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_2;
else if (!strcasecmp(tls_token, "tlsv1.3")) {
#ifdef TLS1_3_VERSION
cfg->tls_protocols |= REDIS_TLS_PROTO_TLSv1_3;
#else
fprintf(stderr, "TLSv1.3 is specified in tls-protocols but not supported by OpenSSL.");
return -1;
#endif
} else {
fprintf(stderr, "Invalid tls-protocols specified %s. "
"Use a combination of 'TLSv1', 'TLSv1.1', 'TLSv1.2' and 'TLSv1.3'.", tls_token);
return -1;
break;
}
tls_token = std::strtok(NULL, tls_delimiter);
}
break;
}
#endif
default:
return -1;
break;
}
}
if ((cfg->cluster_mode && !verify_cluster_option(cfg)) ||
(cfg->arbitrary_commands->is_defined() && !verify_arbitrary_command_option(cfg))) {
return -1;
}
return 0;
}
void usage() {
fprintf(stdout, "Usage: memtier_benchmark [options]\n"
"A memcache/redis NoSQL traffic generator and performance benchmarking tool.\n"
"\n"
"Connection and General Options:\n"
" -h, --host=ADDR Server address (default: localhost)\n"
" -s, --server=ADDR Same as --host\n"
" -p, --port=PORT Server port (default: 6379)\n"
" -S, --unix-socket=SOCKET UNIX Domain socket name (default: none)\n"
" -4, --ipv4 Force IPv4 address resolution.\n"
" -6 --ipv6 Force IPv6 address resolution.\n"
" -P, --protocol=PROTOCOL Protocol to use (default: redis).\n"
" other supported protocols are resp2, resp3, memcache_text and memcache_binary.\n"
" when using one of resp2 or resp3 the redis protocol version will be set via HELLO command.\n"
" -a, --authenticate=CREDENTIALS Authenticate using specified credentials.\n"
" A simple password is used for memcache_text\n"
" and Redis <= 5.x. <USER>:<PASSWORD> can be\n"
" specified for memcache_binary or Redis 6.x\n"
" or newer with ACL user support.\n"
#ifdef USE_TLS
" --tls Enable SSL/TLS transport security\n"
" --cert=FILE Use specified client certificate for TLS\n"
" --key=FILE Use specified private key for TLS\n"
" --cacert=FILE Use specified CA certs bundle for TLS\n"
" --tls-skip-verify Skip verification of server certificate\n"
" --tls-protocols Specify the tls protocol version to use, comma delemited. Use a combination of 'TLSv1', 'TLSv1.1', 'TLSv1.2' and 'TLSv1.3'.\n"
" --sni=STRING Add an SNI header\n"
#endif
" -x, --run-count=NUMBER Number of full-test iterations to perform\n"
" -D, --debug Print debug output\n"
" --client-stats=FILE Produce per-client stats file\n"
" -o, --out-file=FILE Name of output file (default: stdout)\n"
" --json-out-file=FILE Name of JSON output file, if not set, will not print to json\n"
" --hdr-file-prefix=FILE Prefix of HDR Latency Histogram output files, if not set, will not save latency histogram files\n"
" --show-config Print detailed configuration before running\n"
" --hide-histogram Don't print detailed latency histogram\n"
" --print-percentiles Specify which percentiles info to print on the results table (by default prints percentiles: 50,99,99.9)\n"
" --cluster-mode Run client in cluster mode\n"
" -h, --help Display this help\n"
" -v, --version Display version information\n"
"\n"
"Test Options:\n"
" -n, --requests=NUMBER Number of total requests per client (default: 10000)\n"
" use 'allkeys' to run on the entire key-range\n"
" --rate-limiting=NUMBER The max number of requests to make per second from an individual connection (default is unlimited rate).\n"
" If you use --rate-limiting and a very large rate is entered which cannot be met, memtier will do as many requests as possible per second.\n"
" -c, --clients=NUMBER Number of clients per thread (default: 50)\n"
" -t, --threads=NUMBER Number of threads (default: 4)\n"
" --test-time=SECS Number of seconds to run the test\n"
" --ratio=RATIO Set:Get ratio (default: 1:10)\n"
" --pipeline=NUMBER Number of concurrent pipelined requests (default: 1)\n"
" --reconnect-interval=NUM Number of requests after which re-connection is performed\n"
" --multi-key-get=NUM Enable multi-key get commands, up to NUM keys (default: 0)\n"
" --select-db=DB DB number to select, when testing a redis server\n"
" --distinct-client-seed Use a different random seed for each client\n"
" --randomize random seed based on timestamp (default is constant value)\n"
"\n"
"Arbitrary command:\n"