ptquery.cc 6.85 KB
Newer Older
1 2 3 4 5
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
#ident "Copyright (c) 2007-2012 Tokutek Inc.  All rights reserved."
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
6 7
#include <toku_portability.h>
#include "tokudb_common_funcs.h"
8
#include <toku_pthread.h>
9
#include <toku_assert.h>
10
#include <portability/toku_atomic.h>
11 12 13 14 15 16 17
#include <db.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

18 19
static DB_ENV *env = NULL;
static DB *db = NULL;
20 21 22

#define STRINGIFY2(s) #s
#define STRINGIFY(s) STRINGIFY2(s)
23
static const char *dbdir = "./bench."  STRINGIFY(DIRSUF); /* DIRSUF is passed in as a -D argument to the compiler. */
24
static int env_open_flags_yesx = DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL|DB_INIT_TXN|DB_INIT_LOG|DB_INIT_LOCK|DB_RECOVER|DB_THREAD;
25
// static int env_open_flags_nox = DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL;
26
static const char *dbfilename = "bench.db";
Yoni Fogel's avatar
Yoni Fogel committed
27
static uint64_t cachesize = 127*1024*1024;
28 29
static int nqueries = 1000000;
static int nthreads = 1;
30 31 32 33 34 35 36
static const char *log_dir = NULL;

static long long set_count = 0;

static void pt_query_setup (void) {
    int r;
    r = db_env_create(&env, 0);                                                           assert(r==0);
37
    r = env->set_cachesize(env, cachesize / (1<<30), cachesize % (1<<30), 1);             assert(r==0);
38 39 40 41 42
    if (log_dir) {
        r = env->set_lg_dir(env, log_dir);                                                assert(r==0);
    }
    r = env->open(env, dbdir, env_open_flags_yesx, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);       assert(r==0);
    r = db_create(&db, env, 0);                                                            assert(r==0);
43
    r = db->open(db, NULL, dbfilename, NULL, DB_BTREE, DB_THREAD|DB_AUTO_COMMIT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); assert(r==0);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

static void pt_query_shutdown (void) {
    int r;
    r = db->close(db, 0);                                       assert(r==0);
    r = env->close(env, 0);                                     assert(r==0);
    env = NULL;
}

static unsigned long long maxkey;
enum { SERIAL_SPACING = 1<<6 };
static const int keylen=8;

static double gettime (void) {
    struct timeval tv;
    int r = gettimeofday(&tv, 0);
    assert(r==0);
    return tv.tv_sec + 1e-6*tv.tv_usec;
}

/* From db-benchmark-test.c */
static void long_long_to_array (unsigned char *a, int array_size, unsigned long long l) {
66
    for (int i=0; i<8 && i<array_size; i++)
67 68 69 70 71
	a[i] = (l>>(56-8*i))&0xff;
}

static void array_to_long_long (unsigned long long *l, unsigned char *a, int array_size) {
    *l = 0;
72 73
    for(int i=0; i<8 && i<array_size; i++) {
        unsigned long long tmp =  a[i] & 0xff;
74 75 76 77
        *l += tmp << (56-8*i);
    }
}

78
#if TOKUDB
79
static int do_nothing (DBT const* UU(key), DBT const* UU(data), void* UU(extrav)) {
80
    return TOKUDB_CURSOR_CONTINUE;
81
}
82
#endif
83

84
static void test_begin_commit(int _nqueries) {
85 86 87
    int r;
    unsigned long long k;
    unsigned char kv[keylen];
88
    for (int i = 0; i < _nqueries; i++) {
89
        DB_TXN *txn = NULL;
90
        r = env->txn_begin(env, NULL, &txn, DB_TXN_SNAPSHOT); assert_zero(r);
91 92 93 94 95 96 97
        DBC *c = NULL;
        r = db->cursor(db, txn, &c, 0); assert_zero(r);

        k = (random() + (random() << 31)) % maxkey;
        k = ( k / SERIAL_SPACING ) * SERIAL_SPACING;
        long_long_to_array(kv, keylen, k);
        
98 99 100 101 102 103 104
        DBT key = { .data = kv, .size = 8 };
        DBT val = { .data = NULL, .size = 0 };
#if TOKUDB
        r = c->c_getf_set(c, 0, &key, do_nothing, &val);
#else
        r = c->c_get(c, &key, &val, DB_SET);
#endif
105
        assert_zero(r);
106
        (void) toku_sync_fetch_and_add(&set_count, 1);
107 108 109 110 111
        r = c->c_close(c); assert_zero(r);
        r = txn->commit(txn, 0); assert_zero(r);
    }
}

112

113 114 115 116 117 118
// read in the entire DB, warming up the cache
//  - record maxkey 
static void warmup(void) {
    int r;
    DB_TXN *txn=NULL;
    DBC *c = NULL;
119 120
    DBT key = { .data = NULL };
    DBT val = { .data = NULL };
121 122 123 124 125 126
    double tstart = gettime();
    r = env->txn_begin(env, NULL, &txn, 0); assert_zero(r);
    r = db->cursor(db, txn, &c, 0); assert_zero(r);
    r = c->c_get(c, &key, &val, DB_FIRST); assert_zero(r);
    assert(key.size == 8);
    while ( r != DB_NOTFOUND ) {
127
#if TOKUDB
128
        r = c->c_getf_next(c, DB_PRELOCKED, do_nothing, NULL);
129 130 131
#else
        r = c->c_get(c, &key, &val, DB_NEXT);
#endif
132 133 134 135 136
        if ( r != 0 && r != DB_NOTFOUND) assert_zero(r);
    }
    memset(&key, 0, sizeof key);
    memset(&val, 0, sizeof val);
    r = c->c_get(c, &key, &val, DB_LAST); assert_zero(r);
137
    array_to_long_long(&maxkey, (unsigned char*)key.data, key.size);
138 139 140 141
    r = c->c_close(c); assert_zero(r);
    r = txn->commit(txn, 0); assert_zero(r);
    double tdiff = gettime() - tstart;
    unsigned long long rows = maxkey / SERIAL_SPACING;
142
    printf("Warmup        : read %12llu rows in %6.1fs %8.0f/s\n", rows, tdiff, rows/tdiff); fflush(stdout);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}


struct arg {
//    DB_ENV *env;
//    DB *db;
    int nqueries;
//    int nrows;
};

static void *test_thread(void *arg) {
    struct arg *myarg = (struct arg *) arg;
    test_begin_commit(myarg->nqueries);
    return arg;
}

159 160 161 162 163
static void usage(void) {
    fprintf(stderr, "--nqueries %d\n", nqueries);
    fprintf(stderr, "--nthreads %d\n", nthreads);
    fprintf(stderr, "--cachesize %" PRId64 "\n", cachesize);
}
164 165 166 167 168 169 170 171 172 173 174 175

int test_main(int argc, char * const argv[]) {
    for (int i = 1; i < argc; i++) {
        char * const arg = argv[i];
        if (strcmp(arg, "--nqueries") == 0 && i+1 < argc) {
            nqueries = atoi(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--nthreads") == 0 && i+1 < argc) {
            nthreads = atoi(argv[++i]);
            continue;
        }
176 177 178 179
        if (strcmp(arg, "--cachesize") == 0 && i+1 < argc) {
            cachesize = atoll(argv[++i]);
            continue;
        }
180
        usage();
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
        return 1;
    }

    int r;
    pt_query_setup();

    warmup();
    
    assert(nthreads > 0);
    struct arg myargs[nthreads-1];
    toku_pthread_t mytids[nthreads];
    double tstart = gettime();
    for (int i = 0; i < nthreads-1; i++) {
        myargs[i] = (struct arg) { nqueries };
        r = toku_pthread_create(&mytids[i], NULL, test_thread, &myargs[i]); assert_zero(r);
    }
    test_begin_commit(nqueries);
    for (int i = 0; i < nthreads-1; i++) {
        void *ret;
        r = toku_pthread_join(mytids[i], &ret); assert_zero(r);
    }
    assert(set_count == nthreads * nqueries);
    double tdiff = gettime() - tstart;
204
    printf("Point Queries : read %12llu rows in %6.1fs %8.0f/s with %d threads\n", set_count, tdiff, set_count/tdiff, nthreads); fflush(stdout);
205 206 207 208

    pt_query_shutdown();
    return 0;
}