summaryrefslogtreecommitdiff
path: root/test/itzam_btree_test_stress.c
blob: f9d1e776534abbdb423a54ec0b619da090ad699e (plain)
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
/*
    Itzam/C (version 6.0) is an embedded database engine written in Standard C.

    Copyright 2011 Scott Robert Ladd. All rights reserved.

    Older versions of Itzam/C are:
        Copyright 2002, 2004, 2006, 2008 Scott Robert Ladd. All rights reserved.

    Ancestral code, from Java and C++ books by the author, is:
        Copyright 1992, 1994, 1996, 2001 Scott Robert Ladd.  All rights reserved.

    Itzam/C is user-supported open source software. It's continued development is dependent on
    financial support from the community. You can provide funding by visiting the Itzam/C
    website at:

        http://www.coyotegulch.com

    You may license Itzam/C in one of two fashions:

    1) Simplified BSD License (FreeBSD License)

    Redistribution and use in source and binary forms, with or without modification, are
    permitted provided that the following conditions are met:

    1.  Redistributions of source code must retain the above copyright notice, this list of
        conditions and the following disclaimer.

    2.  Redistributions in binary form must reproduce the above copyright notice, this list
        of conditions and the following disclaimer in the documentation and/or other materials
        provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY SCOTT ROBERT LADD ``AS IS'' AND ANY EXPRESS OR IMPLIED
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SCOTT ROBERT LADD OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    The views and conclusions contained in the software and documentation are those of the
    authors and should not be interpreted as representing official policies, either expressed
    or implied, of Scott Robert Ladd.

    2) Closed-Source Proprietary License

    If your project is a closed-source or proprietary project, the Simplified BSD License may
    not be appropriate or desirable. In such cases, contact the Itzam copyright holder to
    arrange your purchase of an appropriate license.

    The author can be contacted at:

          scott.ladd@coyotegulch.com
          scott.ladd@gmail.com
          http:www.coyotegulch.com
*/

#include "../src/itzam.h"
#include "itzam_errors.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>

/*----------------------------------------------------------
 * embedded random number generator; ala Park and Miller
 */
static int32_t seed = 1325;

void init_test_prng(int32_t s)
{
	seed = s;
}

int32_t random_int32(int32_t limit)
{
    static const int32_t IA   = 16807;
    static const int32_t IM   = 2147483647;
    static const int32_t IQ   = 127773;
    static const int32_t IR   = 2836;
    static const int32_t MASK = 123459876;

    int32_t k;
    int32_t result;

    seed ^= MASK;
    k = seed / IQ;
    seed = IA * (seed - k * IQ) - IR * k;

    if (seed < 0L)
        seed += IM;

    result = (seed % limit);
    seed ^= MASK;

    return result;
}

/*----------------------------------------------------------
 *  Reports an itzam error
 */
void not_okay(itzam_state state)
{
    fprintf(stderr, "\nItzam problem: %s\n", STATE_MESSAGES[state]);
    exit(EXIT_FAILURE);
}

void error_handler(const char * function_name, itzam_error error)
{
    fprintf(stderr, "Itzam error in %s: %s\n", function_name, ERROR_STRINGS[error]);
    exit(EXIT_FAILURE);
}

/*----------------------------------------------------------
 *  Verifies that the database contains the expected records
 */
static itzam_bool verify(itzam_btree * btree, itzam_bool * key_flags, int maxkey)
{
    itzam_bool result = itzam_true;
    int32_t key, rec;

    printf(" -- verifying... ");
    fflush(stdout);

    for (key = 0; key < maxkey; ++key)
    {
        if (itzam_btree_find(btree,(const void *)(&key),(void *)(&rec)))
        {
            if (key_flags[key])
            {
                if (rec != key)
                {
                    printf("data does not match key %d\n", key);
                    result = itzam_false;
                }
            }
            else
            {
                printf("key %d found, and should not have been\n", key);
                result = itzam_false;
            }
        }
        else
        {
            if (key_flags[key])
            {
                printf("expected key %d not found\n", key);
                result = itzam_false;
            }
        }
    }

    if (result)
        printf("okay\n");

    return result;
}

/*----------------------------------------------------------
 * tests
 */
itzam_bool test_btree_stress()
{
    itzam_btree  btree;
    itzam_state  state;
    int32_t      key;
    itzam_int    n, i;
    itzam_int    add_count        = 0;
    itzam_int    rem_count        = 0;
    itzam_int    tran_count       = 0;
    itzam_int    roll_count       = 0;
    itzam_int    commit_count     = 0;
    itzam_bool   in_transaction   = itzam_false;
    itzam_bool * save_flags       = NULL;
    itzam_int    save_add_count   = 0;
    itzam_int    save_rem_count   = 0;
    char *       tran_message     = NULL;
    char *       filename         = "stress.itz";
    itzam_int    maxkey           = 400000;
    itzam_int    test_size        = 1000000;
    int order                     = 25;
    itzam_bool   verified_run     = itzam_false;
    itzam_bool   verbose          = itzam_false;
    itzam_bool   use_transactions = itzam_true;
    itzam_bool * key_flags        = (itzam_bool *)malloc(maxkey * sizeof(itzam_bool));

    time_t start = time(NULL);

    for (n = 0; n < maxkey; ++n)
        key_flags[n] = itzam_false;

    // banner for this test
    printf("\nItzam/C B-Tree Test\nRandomized Stress Test\n");

    state = itzam_btree_create(&btree, filename, order, sizeof(int32_t), itzam_comparator_int32, error_handler);

    if (state != ITZAM_OKAY)
    {
        not_okay(state);
        return itzam_false;
    }

    save_flags = (itzam_bool *)malloc(sizeof(itzam_bool) * maxkey);

    for (n = 1; n <= test_size; ++ n)
    {
        if (use_transactions && !in_transaction)
        {
            if (random_int32(100) < 20)
            {
                state = itzam_btree_transaction_start(&btree);
                in_transaction = itzam_true;

                if (verbose) printf("transaction START\n");

                for (i = 0; i < maxkey; ++i)
                    save_flags[i] = key_flags[i];

                save_add_count = add_count;
                save_rem_count = rem_count;
                ++tran_count;
            }
        }

        key = (int32_t)random_int32((int32_t)maxkey);

        if (verbose) printf("%8d: test key = %8u: ", (int)n, key);

        state = itzam_btree_insert(&btree,(const void *)&key);

        switch (state)
        {
            case ITZAM_OKAY:
                if (key_flags[key])
                {
                    if (verbose) printf("key was not found, and should have been\n");
                    return itzam_false;
                }

                key_flags[key] = itzam_true;
                ++add_count;

                if (verbose) printf("written successfully");

                break;

            case ITZAM_DUPLICATE:
                if (!key_flags[key])
                {
                    if (verbose) printf("key was found, and should not have been\n");
                    return itzam_false;
                }

                state = itzam_btree_remove(&btree,(const void *)(&key));
                ++rem_count;

                if (state == ITZAM_OKAY)
                {
                    key_flags[key] = itzam_false;
                    if (verbose) printf("removed successfully");
                }
                else
                {
                    if (verbose) printf("unable to remove data\n");
                    return itzam_false;
                }

                break;

            default:
                not_okay(state);
                return itzam_false;
        }

        if (in_transaction)
        {
            int32_t choice = random_int32(100);

            if (choice < 35)
            {
                state = itzam_btree_transaction_commit(&btree);
                in_transaction = itzam_false;

                tran_message = "transaction COMMIT\n";
                ++commit_count;
            }
            else if (choice < 65)
            {
                state = itzam_btree_transaction_rollback(&btree);
                in_transaction = itzam_false;

                tran_message = "transaction ROLLBACK\n";

                for (i = 0; i < maxkey; ++i)
                    key_flags[i] = save_flags[i];

                add_count = save_add_count;
                rem_count = save_rem_count;

                ++roll_count;
            }
        }

        if (verified_run)
        {
            itzam_bool okay = verify(&btree,key_flags,maxkey);

            if (!okay)
                return itzam_false;
        }
        else
        {
            if (verbose) printf(" - done\n");
        }

        if (tran_message != NULL)
        {
            if (verbose)
                printf("%s\n",tran_message);

            tran_message = NULL;
        }
    }

    time_t elapsed = time(NULL) - start;

    int tests = add_count + rem_count;

    printf("\ntotal records   added: %u\n", (unsigned int)add_count);
    printf("total records removed: %u\n", (unsigned int)rem_count);

    if (use_transactions)
    {
        printf("         transactions: %u\n", (unsigned int)tran_count);
        printf("              commits: %u\n", (unsigned int)commit_count);
        printf("            rollbacks: %u\n", (unsigned int)roll_count);
    }

    printf("      database  count: %d\n", (int)btree.m_header->m_count);
    printf("      database ticker: %d\n", (int)btree.m_header->m_ticker);
    printf("       total run time: %u seconds\n", (unsigned int)elapsed);
    printf("operations per second: %f\n", ((double)tests / (double)elapsed));

    free(save_flags);

    state = itzam_btree_close(&btree);

    if (state != ITZAM_OKAY)
    {
        not_okay(state);
        return itzam_false;
    }

    free(key_flags);

    return itzam_true;
}

int main(int argc, char* argv[])
{
    int result = EXIT_FAILURE;

    itzam_set_default_error_handler(error_handler);

    init_test_prng((long)time(NULL));

    if (test_btree_stress())
        result = EXIT_SUCCESS;

    return result;
}