summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/vendor/groonga/benchmark/bench-cache.c
blob: 104a7c19e24d9df84e020a9e60b12445ddada803 (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
/* -*- c-basic-offset: 2; coding: utf-8 -*- */
/*
  Copyright (C) 2017  Kouhei Sutou <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
*/

/*
  Groonga: eb65125330b3a8f920693ef3ad53011c7412f2c9
  CFLAGS: -O2 -g3
  CPU: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz

  % make --silent -C benchmark run-bench-cache
  run-bench-cache:
           (total)    (average)  (median)
     1000: (0.0458s)  (0.4576ms) (0.4170ms)
    10000: (0.3464s)  (0.0035s)  (0.0034s) 
  % GRN_CACHE_TYPE=persistent make --silent -C benchmark run-bench-cache
  run-bench-cache:
           (total)    (average)  (median)
     1000: (0.0480s)  (0.4801ms) (0.4700ms)
    10000: (0.4033s)  (0.0040s)  (0.0040s) 
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <grn_cache.h>

#include "lib/benchmark.h"

typedef struct _BenchmarkData
{
  grn_ctx *context;
  grn_cache *cache;
  grn_obj value;
} BenchmarkData;

static void
bench_n(BenchmarkData *data, gint64 n)
{
  gint64 i;
  grn_ctx *ctx;
  grn_cache *cache;
  grn_obj *value;
  grn_obj fetch_buffer;

  ctx = data->context;
  cache = data->cache;
  value = &(data->value);
  GRN_TEXT_INIT(&fetch_buffer, 0);
  for (i = 0; i < n; i++) {
    char key[GRN_TABLE_MAX_KEY_SIZE];
    grn_snprintf(key,
                 GRN_TABLE_MAX_KEY_SIZE,
                 GRN_TABLE_MAX_KEY_SIZE,
                 "key:%" GRN_FMT_INT64D,
                 i);
    GRN_BULK_REWIND(&fetch_buffer);
    grn_cache_fetch(ctx, cache, key, strlen(key), &fetch_buffer);
    grn_cache_update(ctx, cache, key, strlen(key), value);
  }
  GRN_OBJ_FIN(ctx, &fetch_buffer);
}

static void
bench_1000(gpointer user_data)
{
  BenchmarkData *data = user_data;
  bench_n(data, 1000);
}

static void
bench_10000(gpointer user_data)
{
  BenchmarkData *data = user_data;
  bench_n(data, 10000);
}

static void
bench_setup(gpointer user_data)
{
  BenchmarkData *data = user_data;

  data->cache = grn_cache_open(data->context);
  GRN_TEXT_INIT(&(data->value), 0);
  while (GRN_TEXT_LEN(&(data->value)) < 1024) {
    GRN_TEXT_PUTS(data->context, &(data->value), "XXXXXXXXXXX");
  }
}

static void
bench_teardown(gpointer user_data)
{
  BenchmarkData *data = user_data;

  grn_obj_close(data->context, &(data->value));
  grn_cache_close(data->context, data->cache);
}

int
main(int argc, gchar **argv)
{
  grn_rc rc;
  BenchmarkData data;
  BenchReporter *reporter;
  gchar *base_dir;
  grn_ctx ctx;
  gint n = 100;

  rc = grn_init();
  if (rc != GRN_SUCCESS) {
    g_print("failed to initialize Groonga: <%d>: %s\n",
            rc, grn_get_global_error_message());
    return EXIT_FAILURE;
  }
  bench_init(&argc, &argv);

  grn_ctx_init(&ctx, 0);

  data.context = &ctx;

  base_dir = g_build_filename(g_get_tmp_dir(), "groonga-bench", NULL);
  bench_utils_remove_path_recursive_force(base_dir);
  g_mkdir_with_parents(base_dir, 0755);

  reporter = bench_reporter_new();
  bench_reporter_register(reporter, "1000", n,
                          bench_setup, bench_1000,   bench_teardown, &data);
  bench_reporter_register(reporter, "10000", n,
                          bench_setup, bench_10000,  bench_teardown, &data);
  bench_reporter_run(reporter);
  g_object_unref(reporter);

  grn_ctx_fin(&ctx);

  bench_utils_remove_path_recursive_force(base_dir);

  bench_quit();
  grn_fin();

  return EXIT_SUCCESS;
}