1
/* $Id: tdbspeed.c 18843 2005-11-14 14:25:58Z benny $ */
3
* Copyright (c) 1999-2004 Andrew Tridgell <tridge@linuxcare.com>
4
* Copyright (c) 2005 Benedikt Meurer <benny@xfce.org>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the
18
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
* Boston, MA 02111-1307, USA.
21
* This file was originally part of the tdb library, which in turn is
22
* part of the Samba suite, a Unix SMB/CIFS implementation.
29
#ifdef HAVE_SYS_TYPES_H
30
#include <sys/types.h>
32
#ifdef HAVE_SYS_MMAN_H
35
#ifdef HAVE_SYS_STAT_H
38
#ifdef HAVE_SYS_TIME_H
61
/* Turn off if error returns from TDB are sane (before v1.0.2) */
63
#define TDB_ERROR(tdb, code) ((tdb)->ecode == code)
65
#define TDB_ERROR(tdb, code) 1
67
/* a test program for tdb - the trivial database */
69
static TDB_DATA *randdata, *randkeys;
74
static TDB_CONTEXT *db;
76
struct timeval tp1,tp2;
78
static void start_timer()
80
gettimeofday(&tp1,NULL);
83
static double end_timer()
85
gettimeofday(&tp2,NULL);
86
return((tp2.tv_sec - tp1.tv_sec) +
87
(tp2.tv_usec - tp1.tv_usec)*1.0e-6);
90
static void fatal(TDB_CONTEXT *tdb, const char *why)
93
if (tdb) fprintf(stderr, "TDB: (%u)\n", tdb->ecode);
97
static char *randbuf(int len)
101
buf = (char *)malloc(len+1);
103
for (i=0;i<len;i++) {
104
buf[i] = 'a' + (rand() % 26);
110
static void addrec_db(int i)
114
key.dptr = randkeys[i].dptr;
115
key.dsize = randkeys[i].dsize+1;
117
data.dptr = randdata[i].dptr;
118
data.dsize = randdata[i].dsize+1;
120
if (rand() % DELETE_PROB == 0) {
121
if (tdb_delete(db, key) == -1
122
&& !TDB_ERROR(db, TDB_ERR_NOEXIST))
123
fatal(db, "tdb_delete failed");
124
} else if (rand() % STORE_PROB == 0) {
125
if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
126
fatal(db, "tdb_store failed");
129
data = tdb_fetch(db, key);
130
if (data.dptr) free(data.dptr);
132
if (db->ecode && !TDB_ERROR(db,TDB_ERR_NOEXIST))
133
fatal(db, "tdb_fetch failed");
142
= { { "default", 0 },
148
{ "65536", 65536 } };
150
unsigned int num_loops[] /* 10,000 each */
157
= { { "normal", TDB_CLEAR_IF_FIRST },
159
{ "byte-reversed", TDB_CLEAR_IF_FIRST|TDB_CONVERT }
163
int main(int argc, char *argv[])
168
/* Precook random buffers */
169
randdata = malloc(10000 * sizeof(randdata[0]));
170
randkeys = malloc(10000 * sizeof(randkeys[0]));
173
for (i=0;i<10000;i++) {
174
randkeys[i].dsize = 1 + (rand() % 4);
175
randdata[i].dsize = 1 + (rand() % 100);
176
randkeys[i].dptr = randbuf(randkeys[i].dsize);
177
randdata[i].dptr = randbuf(randdata[i].dsize);
180
for (k = 0; k < sizeof(tdb_flags)/sizeof(struct tdb_flag); k++) {
181
printf("Operations per second for %s database:\n",
184
printf("Hashsize: ");
185
for (i = 0; i < sizeof(db_sizes)/sizeof(struct db_size); i++)
186
printf("%-8s ", db_sizes[i].name);
189
for (i = 0; i < sizeof(num_loops)/sizeof(int); i++) {
190
printf("%7u: ", num_loops[i]*10000);
191
for (j = 0; j < sizeof(db_sizes)/sizeof(struct db_size); j++) {
192
unsigned int l = 0, l2;
193
db = tdb_open("test.tdb", db_sizes[j].size,
195
O_RDWR | O_CREAT | O_TRUNC, 0600);
198
for (l2=0; l2 < num_loops[i]; l2++)
199
for (l=0;l<10000;l++) addrec_db(l);
200
printf("%-7u ", (int)(l*l2/end_timer()));