1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42 #include "test.h"
43 
44 
45 #ifndef MIN
46 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
47 #endif
48 const double USECS_PER_SEC = 1000000.0;
49 
50 static int
long_key_cmp(DB * UU (e),const DBT * a,const DBT * b)51 long_key_cmp(DB *UU(e), const DBT *a, const DBT *b)
52 {
53     const long *CAST_FROM_VOIDP(x, a->data);
54     const long *CAST_FROM_VOIDP(y, b->data);
55     return (*x > *y) - (*x < *y);
56 }
57 
58 static void
run_test(unsigned long eltsize,unsigned long nodesize,unsigned long repeat)59 run_test(unsigned long eltsize, unsigned long nodesize, unsigned long repeat)
60 {
61     int cur = 0;
62     const int n = 1024;
63     long keys[n];
64     char *vals[n];
65     for (int i = 0; i < n; ++i) {
66         keys[i] = rand();
67         XMALLOC_N(eltsize - (sizeof keys[i]), vals[i]);
68         unsigned int j = 0;
69         char *val = vals[i];
70         for (; j < eltsize - (sizeof keys[i]) - sizeof(int); j += sizeof(int)) {
71             int *p = cast_to_typeof(p) &val[j];
72             *p = rand();
73         }
74         for (; j < eltsize - (sizeof keys[i]); ++j) {
75             char *p = &val[j];
76             *p = (rand() & 0xff);
77         }
78     }
79     XIDS xids_0 = toku_xids_get_root_xids();
80     XIDS xids_123;
81     int r = toku_xids_create_child(xids_0, &xids_123, (TXNID)123);
82     CKERR(r);
83 
84     NONLEAF_CHILDINFO bnc;
85     long long unsigned nbytesinserted = 0;
86     struct timeval t[2];
87     gettimeofday(&t[0], NULL);
88 
89     toku::comparator cmp;
90     cmp.create(long_key_cmp, nullptr);
91 
92     for (unsigned int i = 0; i < repeat; ++i) {
93         bnc = toku_create_empty_nl();
94         for (; toku_bnc_nbytesinbuf(bnc) <= nodesize; ++cur) {
95             toku_bnc_insert_msg(bnc,
96                                 &keys[cur % n], sizeof keys[cur % n],
97                                 vals[cur % n], eltsize - (sizeof keys[cur % n]),
98                                 FT_NONE, next_dummymsn(), xids_123, true,
99                                 cmp); assert_zero(r);
100         }
101         nbytesinserted += toku_bnc_nbytesinbuf(bnc);
102         destroy_nonleaf_childinfo(bnc);
103     }
104 
105     for (int i = 0; i < n; ++i) {
106         toku_free(vals[i]);
107         vals[i] = nullptr;
108     }
109 
110     toku_xids_destroy(&xids_123);
111 
112     gettimeofday(&t[1], NULL);
113     double dt;
114     dt = (t[1].tv_sec - t[0].tv_sec) + ((t[1].tv_usec - t[0].tv_usec) / USECS_PER_SEC);
115     double mbrate = ((double) nbytesinserted / (1 << 20)) / dt;
116     long long unsigned eltrate = (long) (cur / dt);
117     printf("%0.03lf MB/sec\n", mbrate);
118     printf("%llu elts/sec\n", eltrate);
119 
120     cmp.destroy();
121 }
122 
123 int
test_main(int argc,const char * argv[])124 test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
125     unsigned long eltsize, nodesize, repeat;
126 
127     initialize_dummymsn();
128     if (argc != 4) {
129         fprintf(stderr, "Usage: %s <eltsize> <nodesize> <repeat>\n", argv[0]);
130         return 2;
131     }
132     eltsize = strtoul(argv[1], NULL, 0);
133     nodesize = strtoul(argv[2], NULL, 0);
134     repeat = strtoul(argv[3], NULL, 0);
135 
136     run_test(eltsize, nodesize, repeat);
137 
138     return 0;
139 }
140