|
| 1 | +/* mtest-append.c - Howard Chu's benchmark from ITS#10406 */ |
| 2 | +/* |
| 3 | + * Copyright 2011-2021 Howard Chu, Symas Corp. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted only as authorized by the OpenLDAP |
| 8 | + * Public License. |
| 9 | + * |
| 10 | + * A copy of this license is available in the file LICENSE in the |
| 11 | + * top-level directory of the distribution or, alternatively, at |
| 12 | + * <http://www.OpenLDAP.org/license.html>. |
| 13 | + */ |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <time.h> |
| 17 | +#include <sys/time.h> |
| 18 | + |
| 19 | +#include "lmdb.h" |
| 20 | + |
| 21 | +#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) |
| 22 | +#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) |
| 23 | +#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ |
| 24 | + "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) |
| 25 | + |
| 26 | +int main(int argc,char * argv[]) |
| 27 | +{ |
| 28 | + int i = 0, j = 0, rc; |
| 29 | + MDB_env *env; |
| 30 | + MDB_dbi dbi; |
| 31 | + MDB_val key, data; |
| 32 | + MDB_txn *txn; |
| 33 | + MDB_cursor *cursor = NULL; |
| 34 | + int count = 1000000; |
| 35 | + char sval[100] = ""; |
| 36 | + struct timeval beg, end; |
| 37 | + |
| 38 | + E(mdb_env_create(&env)); |
| 39 | + E(mdb_env_set_mapsize(env, 1048576000)); |
| 40 | + E(mdb_env_open(env, "./testdb", MDB_NOSYNC, 0664)); |
| 41 | + |
| 42 | + E(mdb_txn_begin(env, NULL, 0, &txn)); |
| 43 | + E(mdb_dbi_open(txn, NULL, MDB_INTEGERKEY, &dbi)); |
| 44 | + |
| 45 | + key.mv_size = sizeof(int); |
| 46 | + key.mv_data = &i; |
| 47 | + |
| 48 | + data.mv_size = sizeof(sval); |
| 49 | + data.mv_data = sval; |
| 50 | + printf("Adding %d values\n", count); |
| 51 | + gettimeofday(&beg, NULL); |
| 52 | + for (i=0;i<count;i++) { |
| 53 | + if (!cursor) |
| 54 | + E(mdb_cursor_open(txn, dbi, &cursor)); |
| 55 | + E(mdb_cursor_put(cursor, &key, &data, MDB_APPEND)); |
| 56 | + j++; |
| 57 | + if (j == 1000) { |
| 58 | + mdb_cursor_close(cursor); |
| 59 | + cursor = NULL; |
| 60 | + E(mdb_txn_commit(txn)); |
| 61 | + E(mdb_txn_begin(env, NULL, 0, &txn)); |
| 62 | + j = 0; |
| 63 | + } |
| 64 | + } |
| 65 | + if (cursor) |
| 66 | + mdb_cursor_close(cursor); |
| 67 | + E(mdb_txn_commit(txn)); |
| 68 | + gettimeofday(&end, NULL); |
| 69 | + |
| 70 | + mdb_dbi_close(env, dbi); |
| 71 | + mdb_env_close(env); |
| 72 | + |
| 73 | + end.tv_usec -= beg.tv_usec; |
| 74 | + if (end.tv_usec < 0) { |
| 75 | + end.tv_usec += 1000000; |
| 76 | + end.tv_sec--; |
| 77 | + } |
| 78 | + end.tv_sec -= beg.tv_sec; |
| 79 | + printf("Added %d values in %ld.%06ldsec\n", count, end.tv_sec, end.tv_usec); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments