forked from RedisLabs/memtier_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memtier_benchmark.cpp
983 lines (888 loc) · 36.6 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
/*
* Copyright (C) 2011-2013 Garantia Data 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 <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <limits.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#include "client.h"
#include "obj_gen.h"
#include "memtier_benchmark.h"
#ifdef HAVE_ASSERT_H
#include <assert.h>
#endif
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.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);
}
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"
"protocol = %s\n"
"out_file = %s\n"
"client_stats = %s\n"
"run_count = %u\n"
"debug = %u\n"
"requests = %u\n"
"clients = %u\n"
"threads = %u\n"
"test_time = %u\n"
"ratio = %u:%u\n"
"pipeline = %u\n"
"data_size = %u\n"
"random_data = %s\n"
"noop_get = %s\n"
"data_size_range = %u-%u\n"
"data_size_list = %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 = %u\n"
"key_maximum = %u\n"
"key_pattern = %s\n"
"key_padding = %u\n"
"reconnect_interval = %u\n"
"multi_key_get = %u\n"
"authenticate = %s\n"
"select-db = %d\n"
"no-expiry = %s\n",
cfg->server,
cfg->port,
cfg->unix_socket,
cfg->protocol,
cfg->out_file,
cfg->client_stats,
cfg->run_count,
cfg->debug,
cfg->requests,
cfg->clients,
cfg->threads,
cfg->test_time,
cfg->ratio.a, cfg->ratio.b,
cfg->pipeline,
cfg->data_size,
cfg->random_data ? "yes" : "no",
cfg->noop_get ? "yes" : "no",
cfg->data_size_range.min, cfg->data_size_range.max,
cfg->data_size_list.print(tmpbuf, sizeof(tmpbuf)-1),
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_padding,
cfg->reconnect_interval,
cfg->multi_key_get,
cfg->authenticate ? cfg->authenticate : "",
cfg->select_db,
cfg->no_expiry ? "yes" : "no");
}
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->protocol)
cfg->protocol = "redis";
if (!cfg->run_count)
cfg->run_count = 1;
if (!cfg->requests && !cfg->test_time)
cfg->requests = 10000;
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";
}
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_noop_get,
o_data_size_range,
o_data_size_list,
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_padding,
o_show_config,
o_client_stats,
o_reconnect_interval,
o_generate_keys,
o_multi_key_get,
o_select_db,
o_no_expiry
};
static struct option long_options[] = {
{ "server", 1, 0, 's' },
{ "port", 1, 0, 'p' },
{ "unix-socket", 1, 0, 'S' },
{ "protocol", 1, 0, 'P' },
{ "out-file", 1, 0, 'o' },
{ "client-stats", 1, 0, o_client_stats },
{ "run-count", 1, 0, 'x' },
{ "debug", 0, 0, 'D' },
{ "show-config", 0, 0, o_show_config },
{ "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' },
{ "random-data", 0, 0, 'R' },
{ "noop-get", 0, 0, o_noop_get },
{ "data-size-range", 1, 0, o_data_size_range },
{ "data-size-list", 1, 0, o_data_size_list },
{ "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-padding", 1, 0, o_key_padding },
{ "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 },
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ NULL, 0, 0, 0 }
};
int option_index;
int c;
char *endptr;
while ((c = getopt_long(argc, argv,
"s:S:p:P:o:x:DRn:c:t:d:a:h", long_options, &option_index)) != -1)
{
switch (c) {
case 'h':
return -1;
break;
case 'v':
puts(PACKAGE_STRING);
puts("Copyright (C) 2011-2013 Garantia Data 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':
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 'P':
if (strcmp(optarg, "memcache_text") &&
strcmp(optarg, "memcache_binary") &&
strcmp(optarg, "redis") &&
strcmp(optarg, "pequod")) {
fprintf(stderr, "error: supported protocols are 'memcache_text', 'memcache_binary', 'redis', and 'pequod'.\n");
return -1;
}
cfg->protocol = optarg;
break;
case 'o':
cfg->out_file = 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 'n':
endptr = NULL;
cfg->requests = (unsigned int) strtoul(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_noop_get:
cfg->noop_get = true;
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: data_size_range must be expressed as [0-n]-[1-n].\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 = (unsigned int) strtoul(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 = (unsigned int) strtoul(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_pattern:
cfg->key_pattern = optarg;
if (strlen(cfg->key_pattern) != 3 || cfg->key_pattern[1] != ':' ||
(cfg->key_pattern[0] != 'R' && cfg->key_pattern[0] != 'S') ||
(cfg->key_pattern[2] != 'R' && cfg->key_pattern[2] != 'S')) {
fprintf(stderr, "error: key_pattern must be in the format of [S/R]:[S/R].\n");
return -1;
}
break;
case o_key_padding:
endptr = NULL;
cfg->key_padding = (unsigned int) strtoul(optarg, &endptr, 10);
if (cfg->key_padding< 1 || !endptr || *endptr != '\0') {
fprintf(stderr, "error: key_padding must be greater than zero.\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;
default:
return -1;
break;
}
}
return 0;
}
void usage() {
fprintf(stderr, "Usage: memtier_benchmark [options]\n"
"A memcache/redis/pequod NoSQL traffic generator and performance benchmarking tool.\n"
"\n"
"Connection and General Options:\n"
" -s, --server=ADDR Server address (default: localhost)\n"
" -p, --port=PORT Server port (default: 6379)\n"
" -S, --unix-socket=SOCKET UNIX Domain socket name (default: none)\n"
" -P, --protocol=PROTOCOL Protocol to use (default: redis). Other\n"
" supported protocols are memcache_text,\n"
" memcache_binary, pequod.\n"
" -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"
" --out-file=FILE Name of output file (default: stdout)\n"
" --show-config Print detailed configuration before running\n"
"\n"
"Test Options:\n"
" -n, --requests=NUMBER Number of total requests per client (default: 10000)\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"
" -a, --authenticate=CREDENTIALS Authenticate to redis using CREDENTIALS, which depending\n"
" on the protocol can be PASSWORD or USER:PASSWORD.\n"
" --select-db=DB DB number to select, when testing a redis server\n"
"\n"
"Object Options:\n"
" -d --data-size=SIZE Object data size (default: 32)\n"
" -R --random-data Indicate that data should be randomized\n"
" --data-size-range=RANGE Use random-sized items in the specified range (min-max)\n"
" --data-size-list=LIST Use sizes from weight list (size1:weight1,..sizeN:weightN)\n"
" --expiry-range=RANGE Use random expiry values from the specified range\n"
" --noop-get Send noop-get requests (Pequod only)\n"
"\n"
"Imported Data Options:\n"
" --data-import=FILE Read object data from file\n"
" --data-verify Enable data verification when test is complete\n"
" --verify-only Only perform --data-verify, without any other test\n"
" --generate-keys Generate keys for imported objects\n"
" --no-expiry Ignore expiry information in imported data\n"
"\n"
"Key Options:\n"
" --key-prefix=PREFIX Prefix for keys (default: \"memtier-\")\n"
" --key-minimum=NUMBER Key ID minimum value (default: 0)\n"
" --key-maximum=NUMBER Key ID maximum value (default: 10000000)\n"
" --key-pattern=PATTERN Set:Get pattern (default: R:R)\n"
" --key-padding=NUMBER Pad key ID values with zeros to be a fixed size (default: none)\n"
"\n"
" --help Display this help\n"
" --version Display version information\n"
"\n"
);
exit(2);
}
static void* cg_thread_start(void *t);
struct cg_thread {
unsigned int m_thread_id;
benchmark_config* m_config;
object_generator* m_obj_gen;
client_group* m_cg;
abstract_protocol* m_protocol;
pthread_t m_thread;
bool m_finished;
cg_thread(unsigned int id, benchmark_config* config, object_generator* obj_gen) :
m_thread_id(id), m_config(config), m_obj_gen(obj_gen), m_cg(NULL), m_protocol(NULL), m_finished(false)
{
m_protocol = protocol_factory(m_config->protocol);
assert(m_protocol != NULL);
m_cg = new client_group(m_config, m_protocol, m_obj_gen);
}
~cg_thread()
{
if (m_cg != NULL) {
delete m_cg;
}
if (m_protocol != NULL) {
delete m_protocol;
}
}
int prepare(void)
{
if (m_cg->create_clients(m_config->clients) < (int) m_config->clients)
return -1;
return m_cg->prepare();
}
int start(void)
{
return pthread_create(&m_thread, NULL, cg_thread_start, (void *)this);
}
void join(void)
{
int* retval;
int ret;
ret = pthread_join(m_thread, (void **)&retval);
assert(ret == 0);
}
};
static void* cg_thread_start(void *t)
{
cg_thread* thread = (cg_thread*) t;
thread->m_cg->run();
thread->m_finished = true;
return t;
}
void size_to_str(unsigned long int size, char *buf, int buf_len)
{
if (size >= 1024*1024*1024) {
snprintf(buf, buf_len, "%.2fGB",
(float) size / (1024*1024*1024));
} else if (size >= 1024*1024) {
snprintf(buf, buf_len, "%.2fMB",
(float) size / (1024*1024));
} else {
snprintf(buf, buf_len, "%.2fKB",
(float) size / 1024);
}
}
run_stats run_benchmark(int run_id, benchmark_config* cfg, object_generator* obj_gen)
{
fprintf(stderr, "[RUN #%u] Preparing benchmark client...\n", run_id);
// prepare threads data
std::vector<cg_thread*> threads;
for (unsigned int i = 0; i < cfg->threads; i++) {
cg_thread* t = new cg_thread(i, cfg, obj_gen);
assert(t != NULL);
if (t->prepare() < 0) {
benchmark_error_log("error: failed to prepare thread %u for test.\n", i);
exit(1);
}
threads.push_back(t);
}
// launch threads
fprintf(stderr, "[RUN #%u] Launching threads now...\n", run_id);
for (std::vector<cg_thread*>::iterator i = threads.begin(); i != threads.end(); i++) {
(*i)->start();
}
// provide some feedback...
unsigned int active_threads = 0;
do {
active_threads = 0;
sleep(1);
unsigned long int total_ops = 0;
unsigned long int total_bytes = 0;
unsigned long int duration = 0;
unsigned long int total_latency = 0;
for (std::vector<cg_thread*>::iterator i = threads.begin(); i != threads.end(); i++) {
if (!(*i)->m_finished)
active_threads++;
total_ops += (*i)->m_cg->get_total_ops();
total_bytes += (*i)->m_cg->get_total_bytes();
total_latency += (*i)->m_cg->get_total_latency();
if ((*i)->m_cg->get_duration_usec() > duration)
duration = (*i)->m_cg->get_duration_usec();
}
unsigned long int ops_sec = 0;
unsigned long int bytes_sec = 0;
double avg_latency = 0;
if (duration > 1000000) {
ops_sec = (total_ops / (duration / 1000000));
bytes_sec = (total_bytes / (duration / 1000000));
avg_latency = ((double) total_latency / 1000 / total_ops) ;
}
char bytes_str[40];
size_to_str(bytes_sec, bytes_str, sizeof(bytes_str)-1);
fprintf(stderr, "[RUN #%u, %3u secs] %2u threads: %11lu ops, %7lu ops/sec, %s/sec, %5.2fmsec latency\r",
run_id, (unsigned int) (duration / 1000000), active_threads, total_ops, ops_sec, bytes_str, avg_latency);
} while (active_threads > 0);
fprintf(stderr, "\n\n");
// join all threads back and unify stats
run_stats stats;
for (std::vector<cg_thread*>::iterator i = threads.begin(); i != threads.end(); i++) {
(*i)->join();
(*i)->m_cg->merge_run_stats(&stats);
}
// Do we need to produce client stats?
if (cfg->client_stats != NULL) {
unsigned int cg_id = 0;
fprintf(stderr, "[RUN %u] Writing client stats files...\n", run_id);
for (std::vector<cg_thread*>::iterator i = threads.begin(); i != threads.end(); i++) {
char prefix[PATH_MAX];
snprintf(prefix, sizeof(prefix)-1, "%s-%u-%u", cfg->client_stats, run_id, cg_id++);
(*i)->m_cg->write_client_stats(prefix);
}
}
// clean up all client_groups. the main value of this is to be able to
// properly look for leaks...
while (threads.size() > 0) {
cg_thread* t = *threads.begin();
threads.erase(threads.begin());
delete t;
}
return stats;
}
int main(int argc, char *argv[])
{
struct benchmark_config cfg;
memset(&cfg, 0, sizeof(struct benchmark_config));
if (config_parse_args(argc, argv, &cfg) < 0) {
usage();
}
config_init_defaults(&cfg);
log_level = cfg.debug;
if (cfg.show_config) {
fprintf(stderr, "============== Configuration values: ==============\n");
config_print(stdout, &cfg);
fprintf(stderr, "===================================================\n");
}
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
benchmark_error_log("error: getrlimit failed: %s\n", strerror(errno));
exit(1);
}
if (cfg.unix_socket != NULL &&
(cfg.server != NULL || cfg.port > 0)) {
benchmark_error_log("error: UNIX domain socket and TCP cannot be used together.\n");
exit(1);
}
unsigned int fds_needed = (cfg.threads * cfg.clients) + (cfg.threads * 10) + 10;
if (fds_needed > rlim.rlim_cur) {
if (fds_needed > rlim.rlim_max && getuid() != 0) {
benchmark_error_log("error: running the tool with this number of connections requires 'root' privilegs.\n");
exit(1);
}
rlim.rlim_cur = rlim.rlim_max = fds_needed;
if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
benchmark_error_log("error: setrlimit failed: %s\n", strerror(errno));
exit(1);
}
}
if (cfg.noop_get && strcmp(cfg.protocol, "pequod")) {
fprintf(stderr, "error: noop-get can only be used with pequod protocol.\n");
usage();
}
// create and configure object generator
object_generator* obj_gen = NULL;
imported_keylist* keylist = NULL;
if (!cfg.data_import) {
if (cfg.data_verify) {
fprintf(stderr, "error: use data-verify only with data-import\n");
exit(1);
}
if (cfg.no_expiry) {
fprintf(stderr, "error: use no-expiry only with data-import\n");
exit(1);
}
obj_gen = new object_generator();
assert(obj_gen != NULL);
} else {
// check paramters
if (cfg.data_size ||
cfg.data_size_list.is_defined() ||
cfg.data_size_range.is_defined()) {
fprintf(stderr, "error: data size cannot be specified when importing.\n");
exit(1);
}
if (cfg.random_data) {
fprintf(stderr, "error: random-data cannot be specified when importing.\n");
exit(1);
}
if (!cfg.generate_keys &&
(cfg.key_maximum || cfg.key_minimum || cfg.key_prefix)) {
fprintf(stderr, "error: use key-minimum, key-maximum and key-prefix only with generate-keys.\n");
exit(1);
}
if (!cfg.generate_keys) {
// read keys
fprintf(stderr, "Reading keys from %s...", cfg.data_import);
keylist = new imported_keylist(cfg.data_import);
assert(keylist != NULL);
if (!keylist->read_keys()) {
fprintf(stderr, "\nerror: failed to read keys.\n");
exit(1);
} else {
fprintf(stderr, " %u keys read.\n", keylist->size());
}
}
obj_gen = new import_object_generator(cfg.data_import, keylist, cfg.no_expiry);
assert(obj_gen != NULL);
if (dynamic_cast<import_object_generator*>(obj_gen)->open_file() != true) {
fprintf(stderr, "error: %s: failed to open.\n", cfg.data_import);
exit(1);
}
}
if (cfg.authenticate) {
if (strcmp(cfg.protocol, "redis") != 0 &&
strcmp(cfg.protocol, "memcache_binary") != 0) {
fprintf(stderr, "error: authenticate can only be used with redis or memcache_binary.\n");
usage();
}
if (strcmp(cfg.protocol, "memcache_binary") == 0 &&
strchr(cfg.authenticate, ':') == NULL) {
fprintf(stderr, "error: binary_memcache credentials must be in the form of USER:PASSWORD.\n");
usage();
}
}
if (cfg.select_db > 0 && strcmp(cfg.protocol, "redis")) {
fprintf(stderr, "error: select-db can only be used with redis protocol.\n");
usage();
}
if (cfg.data_size) {
if (cfg.data_size_list.is_defined() || cfg.data_size_range.is_defined()) {
fprintf(stderr, "error: data-size cannot be used with data_size_list or data_size_range.\n");
usage();
}
obj_gen->set_data_size_fixed(cfg.data_size);
} else if (cfg.data_size_list.is_defined()) {
if (cfg.data_size_range.is_defined()) {
fprintf(stderr, "error: data-size-list cannot be used with data-size-range.\n");
usage();
}
obj_gen->set_data_size_list(&cfg.data_size_list);
} else if (cfg.data_size_range.is_defined()) {
obj_gen->set_data_size_range(cfg.data_size_range.min, cfg.data_size_range.max);
} else if (!cfg.data_import) {
fprintf(stderr, "error: data-size, data-size-list or data-size-range must be specified.\n");
usage();
}
if (!cfg.data_import) {
obj_gen->set_random_data(cfg.random_data);
}
if (!cfg.data_import || cfg.generate_keys) {
obj_gen->set_key_prefix(cfg.key_prefix);
obj_gen->set_key_range(cfg.key_minimum, cfg.key_maximum);
obj_gen->set_key_padding(cfg.key_padding);
}
obj_gen->set_expiry_range(cfg.expiry_range.min, cfg.expiry_range.max);
// Prepare output file
FILE *outfile;
if (cfg.out_file != NULL) {
fprintf(stderr, "Writing results to %s...\n", cfg.out_file);
outfile = fopen(cfg.out_file, "w");
if (!outfile) {
perror(cfg.out_file);
}
} else {
outfile = stdout;
}
if (!cfg.verify_only) {
std::vector<run_stats> all_stats;
for (unsigned int run_id = 1; run_id <= cfg.run_count; run_id++) {
if (run_id > 1)
sleep(1); // let connections settle
run_stats stats = run_benchmark(run_id, &cfg, obj_gen);
all_stats.push_back(stats);
}
// Print some run information
fprintf(outfile,
"%-9u Threads\n"
"%-9u Connections per thread\n"
"%-9u %s\n",
cfg.threads, cfg.clients,
cfg.requests > 0 ? cfg.requests : cfg.test_time,
cfg.requests > 0 ? "Requests per thread" : "Seconds");
// If more than 1 run was used, compute best, worst and average
if (cfg.run_count > 1) {
unsigned int min_ops_sec = (unsigned int) -1;
unsigned int max_ops_sec = 0;
run_stats* worst = NULL;
run_stats* best = NULL;
for (std::vector<run_stats>::iterator i = all_stats.begin(); i != all_stats.end(); i++) {
unsigned int secs = (i->get_duration_usec() / 1000);
unsigned int ops_sec = (i->get_total_ops() / (secs > 0 ? secs : 1)) * 1000;
if (ops_sec < min_ops_sec) {
min_ops_sec = ops_sec;
worst = &(*i);
}
if (ops_sec > max_ops_sec) {
max_ops_sec = ops_sec;
best = &(*i);
}
}
fprintf(outfile, "\n\n"
"BEST RUN RESULTS\n"
"========================================================================\n");
best->print(outfile);
fprintf(outfile, "\n\n"
"WORST RUN RESULTS\n"
"========================================================================\n");
worst->print(outfile);
fprintf(outfile, "\n\n"
"AGGREGATED AVERAGE RESULTS (%u runs)\n"
"========================================================================\n", cfg.run_count);
run_stats average;
average.aggregate_average(all_stats);
average.print(outfile);
} else {
all_stats.begin()->print(outfile);
}
}
// If needed, data verification is done now...
if (cfg.data_verify) {
struct event_base *verify_event_base = event_base_new();
abstract_protocol *verify_protocol = protocol_factory(cfg.protocol);
verify_client *client = new verify_client(verify_event_base, &cfg, verify_protocol, obj_gen);
fprintf(outfile, "\n\nPerforming data verification...\n");
// Run client in verification mode
client->prepare();
event_base_dispatch(verify_event_base);
fprintf(outfile, "Data verification completed:\n"
"%-10llu keys verified successfuly.\n"
"%-10llu keys failed.\n",
client->get_verified_keys(),
client->get_errors());
// Clean up...
delete client;
delete verify_protocol;
event_base_free(verify_event_base);
}
if (outfile != stdout) {
fclose(outfile);
}
delete obj_gen;
if (keylist != NULL)
delete keylist;
}