~ubuntu-branches/debian/experimental/thunar/experimental

« back to all changes in this revision

Viewing changes to tdb/tdbspeed.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez
  • Date: 2006-01-02 23:42:32 UTC
  • Revision ID: james.westby@ubuntu.com-20060102234232-8xeq0lqhyn70syr0
Tags: upstream-0.1.4svn+r18850
ImportĀ upstreamĀ versionĀ 0.1.4svn+r18850

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: tdbspeed.c 18843 2005-11-14 14:25:58Z benny $ */
 
2
/*-
 
3
 * Copyright (c) 1999-2004 Andrew Tridgell <tridge@linuxcare.com>
 
4
 * Copyright (c) 2005      Benedikt Meurer <benny@xfce.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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.
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include <config.h>
 
27
#endif
 
28
 
 
29
#ifdef HAVE_SYS_TYPES_H
 
30
#include <sys/types.h>
 
31
#endif
 
32
#ifdef HAVE_SYS_MMAN_H
 
33
#include <sys/mman.h>
 
34
#endif
 
35
#ifdef HAVE_SYS_STAT_H
 
36
#include <sys/stat.h>
 
37
#endif
 
38
#ifdef HAVE_SYS_TIME_H
 
39
#include <sys/time.h>
 
40
#endif
 
41
 
 
42
#ifdef HAVE_STDLIB_H
 
43
#include <stdlib.h>
 
44
#endif
 
45
#include <stdio.h>
 
46
#ifdef HAVE_FCNTL_H
 
47
#include <fcntl.h>
 
48
#endif
 
49
#ifdef HAVE_UNISTD_H
 
50
#include <unistd.h>
 
51
#endif
 
52
#ifdef HAVE_STRING_H
 
53
#include <string.h>
 
54
#endif
 
55
#ifdef HAVE_MEMORY_H
 
56
#include <memory.h>
 
57
#endif
 
58
 
 
59
#include <tdb/tdb.h>
 
60
 
 
61
/* Turn off if error returns from TDB are sane (before v1.0.2) */
 
62
#if 1
 
63
#define TDB_ERROR(tdb, code) ((tdb)->ecode == code)
 
64
#else
 
65
#define TDB_ERROR(tdb, code) 1
 
66
#endif
 
67
/* a test program for tdb - the trivial database */
 
68
 
 
69
static TDB_DATA *randdata, *randkeys;
 
70
 
 
71
#define DELETE_PROB 7
 
72
#define STORE_PROB 5
 
73
 
 
74
static TDB_CONTEXT *db;
 
75
 
 
76
struct timeval tp1,tp2;
 
77
 
 
78
static void start_timer()
 
79
{
 
80
        gettimeofday(&tp1,NULL);
 
81
}
 
82
 
 
83
static double end_timer()
 
84
{
 
85
        gettimeofday(&tp2,NULL);
 
86
        return((tp2.tv_sec - tp1.tv_sec) + 
 
87
               (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
 
88
}
 
89
 
 
90
static void fatal(TDB_CONTEXT *tdb, const char *why)
 
91
{
 
92
        perror(why);
 
93
        if (tdb) fprintf(stderr, "TDB: (%u)\n", tdb->ecode);
 
94
        exit(1);
 
95
}
 
96
 
 
97
static char *randbuf(int len)
 
98
{
 
99
        char *buf;
 
100
        int i;
 
101
        buf = (char *)malloc(len+1);
 
102
 
 
103
        for (i=0;i<len;i++) {
 
104
                buf[i] = 'a' + (rand() % 26);
 
105
        }
 
106
        buf[i] = 0;
 
107
        return buf;
 
108
}
 
109
 
 
110
static void addrec_db(int i)
 
111
{
 
112
        TDB_DATA key, data;
 
113
 
 
114
        key.dptr = randkeys[i].dptr;
 
115
        key.dsize = randkeys[i].dsize+1;
 
116
 
 
117
        data.dptr = randdata[i].dptr;
 
118
        data.dsize = randdata[i].dsize+1;
 
119
 
 
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");
 
127
                }
 
128
        } else {
 
129
                data = tdb_fetch(db, key);
 
130
                if (data.dptr) free(data.dptr);
 
131
                else {
 
132
                        if (db->ecode && !TDB_ERROR(db,TDB_ERR_NOEXIST))
 
133
                                fatal(db, "tdb_fetch failed");
 
134
                }
 
135
        }
 
136
}
 
137
 
 
138
struct db_size {
 
139
        const char *name;
 
140
        int size;
 
141
} db_sizes[]
 
142
= { { "default", 0 },
 
143
    { "307", 307 },
 
144
    { "512", 512 },
 
145
    { "1024", 1024 },
 
146
    { "4096", 4096 },
 
147
    { "16384", 16384 },
 
148
    { "65536", 65536 } };
 
149
 
 
150
unsigned int num_loops[]  /* 10,000 each */
 
151
= { 1, 5, 25 };
 
152
 
 
153
struct tdb_flag {
 
154
        const char *name;
 
155
        int flags;
 
156
} tdb_flags[]
 
157
= { { "normal", TDB_CLEAR_IF_FIRST },
 
158
#ifdef TDB_CONVERT
 
159
    { "byte-reversed", TDB_CLEAR_IF_FIRST|TDB_CONVERT }
 
160
#endif
 
161
};
 
162
 
 
163
int main(int argc, char *argv[])
 
164
{
 
165
        int i, j, seed=0;
 
166
        int k;
 
167
 
 
168
        /* Precook random buffers */
 
169
        randdata = malloc(10000 * sizeof(randdata[0]));
 
170
        randkeys = malloc(10000 * sizeof(randkeys[0]));
 
171
 
 
172
        srand(seed);
 
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);
 
178
        }
 
179
 
 
180
        for (k = 0; k < sizeof(tdb_flags)/sizeof(struct tdb_flag); k++) {
 
181
                printf("Operations per second for %s database:\n",
 
182
                       tdb_flags[k].name);
 
183
 
 
184
                printf("Hashsize:   ");
 
185
                for (i = 0; i < sizeof(db_sizes)/sizeof(struct db_size); i++)
 
186
                        printf("%-8s ", db_sizes[i].name);
 
187
                printf("\n");
 
188
                
 
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,
 
194
                                              tdb_flags[k].flags,
 
195
                                              O_RDWR | O_CREAT | O_TRUNC, 0600);
 
196
                                srand(seed);
 
197
                                start_timer();
 
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()));
 
201
                                tdb_close(db);
 
202
                        }
 
203
                        printf("\n");
 
204
                }
 
205
                printf("\n");
 
206
        }
 
207
        return 0;
 
208
}