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 "test.h"
40 /* How fast can we do insertions when there are many files? */
41 
42 #include <db.h>
43 #include <sys/stat.h>
44 
45 #define NFILES 1000
46 #define NINSERTS_PER 1000
47 
48 static DB_ENV *env;
49 static DB *dbs[NFILES];
50 DB_TXN *txn;
51 
52 static void
test_setup(void)53 test_setup (void) {
54     int r;
55     toku_os_recursive_delete(TOKU_TEST_FILENAME);
56     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);       CKERR(r);
57 
58     r=db_env_create(&env, 0); CKERR(r);
59     env->set_errfile(env, stderr);
60     multiply_locks_for_n_dbs(env, NFILES);
61 
62     r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
63 
64     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
65 
66     int i;
67 
68     for (i=0; i<NFILES; i++) {
69 	char fname[20];
70 	snprintf(fname, sizeof(fname), "foo%d.db", i);
71 	r=db_create(&dbs[i], env, 0); CKERR(r);
72 	r = dbs[i]->set_pagesize(dbs[i], 4096);
73 	r=dbs[i]->open(dbs[i], txn, fname, 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
74     }
75     r=txn->commit(txn, 0);    assert(r==0);
76 }
77 
78 static void
test_shutdown(void)79 test_shutdown (void) {
80     int i;
81     int r;
82     for (i=0; i<NFILES; i++) {
83 	r= dbs[i]->close(dbs[i], 0); CKERR(r);
84     }
85     r= env->close(env, 0); CKERR(r);
86 }
87 
88 static void
doit(void)89 doit (void) {
90     int j;
91     int r;
92     struct timeval startt, endt;
93     gettimeofday(&startt, 0);
94     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
95     for (j=0; j<NINSERTS_PER; j++) {
96 	int i;
97 	DBT key,data;
98 	char str[10];
99 	snprintf(str, sizeof(str), "%08d", j);
100 	dbt_init(&key, str, 1+strlen(str));
101 	dbt_init(&data, str, 1+strlen(str));
102 	for (i=0; i<NFILES; i++) {
103 	    r = dbs[i]->put(dbs[i], txn, &key, &data, 0);
104 	    CKERR(r);
105 	}
106     }
107     r=txn->commit(txn, 0); assert(r==0);
108     gettimeofday(&endt, 0);
109     long long ninserts = NINSERTS_PER * NFILES;
110     double diff = (endt.tv_sec - startt.tv_sec) + 1e-6*(endt.tv_usec-startt.tv_usec);
111     if (verbose) printf("%lld insertions in %9.6fs, %9.3f ins/s \n", ninserts, diff, ninserts/diff);
112 }
113 
114 int
test_main(int argc,char * const argv[])115 test_main (int argc, char * const argv[]) {
116     parse_args(argc, argv);
117 
118     test_setup();
119     doit();
120     test_shutdown();
121 
122     return 0;
123 }
124