Kouhei Sutou
null+****@clear*****
Sat Sep 13 20:40:19 JST 2014
Kouhei Sutou 2014-09-13 20:40:19 +0900 (Sat, 13 Sep 2014) New Revision: 09a4c4e00832fb90dee74c5b97b7cf0f5952f85b https://github.com/groonga/groonga/commit/09a4c4e00832fb90dee74c5b97b7cf0f5952f85b Message: benchmark: add benchmark for range search It can be optimized by mruby. Added files: benchmark/bench-range-select.c Modified files: .gitignore benchmark/Makefile.am Modified: .gitignore (+2 -1) =================================================================== --- .gitignore 2014-09-13 11:28:17 +0900 (18f84e6) +++ .gitignore 2014-09-13 20:40:19 +0900 (8be470d) @@ -85,7 +85,7 @@ cmake_install.cmake /coverage /coverage.info /test/unit/lib/*-*.*.*/ -/benchmark/tmp/ +/benchmark/tmp /benchmark/fixtures/geo-select/13_2010.CSV /benchmark/fixtures/geo-select/load.grn /benchmark/bench-geo-select @@ -94,6 +94,7 @@ cmake_install.cmake /benchmark/bench-table-factory /benchmark/bench-ctx-create /benchmark/bench-query-optimizer +/benchmark/bench-range-select /packages/*/*.log /packages/apt/debian/groonga-keyring.postrm /packages/apt/repositories/debian/pool/*/*/*/*/*.diff.gz Modified: benchmark/Makefile.am (+15 -2) =================================================================== --- benchmark/Makefile.am 2014-09-13 11:28:17 +0900 (146bd49) +++ benchmark/Makefile.am 2014-09-13 20:40:19 +0900 (310fb45) @@ -10,7 +10,8 @@ noinst_PROGRAMS = \ bench-geo-distance \ bench-geo-select \ bench-ctx-create \ - bench-query-optimizer + bench-query-optimizer \ + bench-range-select endif EXTRA_DIST = \ @@ -46,12 +47,16 @@ nodist_EXTRA_bench_ctx_create_SOURCES = $(NONEXISTENT_CXX_SOURCE) bench_query_optimizer_SOURCES = bench-query-optimizer.c nodist_EXTRA_bench_query_optimizer_SOURCES = $(NONEXISTENT_CXX_SOURCE) +bench_range_select_SOURCES = bench-range-select.c +nodist_EXTRA_bench_range_select_SOURCES = $(NONEXISTENT_CXX_SOURCE) + benchmarks = \ run-bench-table-factory \ run-bench-geo-distance \ run-bench-geo-select \ run-bench-ctx-create \ - run-bench-query-optimizer + run-bench-query-optimizer \ + run-bench-range-select run-bench-table-factory: bench-table-factory @echo $@: @@ -86,4 +91,12 @@ run-bench-query-optimizer: bench-query-optimizer GRN_RUBY_SCRIPTS_DIR=$(top_srcdir)/lib/mrb/scripts \ ./bench-query-optimizer +run-bench-range-select: bench-range-select + @echo $@: + @[ ! -e tmp ] && ln -s /dev/shm tmp || : + @mkdir -p tmp/range-select + env \ + GRN_RUBY_SCRIPTS_DIR=$(top_srcdir)/lib/mrb/scripts \ + ./bench-range-select + benchmark: $(benchmarks) Added: benchmark/bench-range-select.c (+240 -0) 100644 =================================================================== --- /dev/null +++ benchmark/bench-range-select.c 2014-09-13 20:40:19 +0900 (98cd7db) @@ -0,0 +1,240 @@ +/* -*- c-basic-offset: 2; coding: utf-8 -*- */ +/* + Copyright (C) 2014 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-1301 USA +*/ + +#include <stdio.h> +#include <string.h> + +#include <db.h> +#include <groonga.h> + +#include "lib/benchmark.h" + +#define GET(context, name) (grn_ctx_get(context, name, strlen(name))) + +typedef struct _BenchmarkData +{ + grn_ctx context; + grn_obj *database; + guint n_records; + grn_bool use_mruby; + const gchar *command; +} BenchmarkData; + +static void +run_command(grn_ctx *context, const gchar *command) +{ + gchar *response; + unsigned int response_length; + int flags; + + grn_ctx_send(context, command, strlen(command), 0); + grn_ctx_recv(context, &response, &response_length, &flags); +} + +static void +bench(gpointer user_data) +{ + BenchmarkData *data = user_data; + grn_ctx *context = &(data->context); + + run_command(context, data->command); +} + +static gchar * +get_tmp_dir(void) +{ + gchar *current_dir; + gchar *tmp_dir; + + current_dir = g_get_current_dir(); + tmp_dir = g_build_filename(current_dir, "tmp", NULL); + g_free(current_dir); + + return tmp_dir; +} + +static void +setup_database(BenchmarkData *data) +{ + grn_ctx *context = &(data->context); + gchar *tmp_dir; + gchar *database_last_component_name; + gchar *database_path; + guint i; + + tmp_dir = get_tmp_dir(); + database_last_component_name = + g_strdup_printf("db-%d-%s-mruby", + data->n_records, + data->use_mruby ? "with" : "without"); + database_path = g_build_filename(tmp_dir, + "range-select", + database_last_component_name, + NULL); + g_free(database_last_component_name); + + if (g_file_test(database_path, G_FILE_TEST_EXISTS)) { + data->database = grn_db_open(context, database_path); + run_command(context, "dump"); + } else { + data->database = grn_db_create(context, database_path, NULL); + + run_command(context, "table_create Entries TABLE_NO_KEY"); + run_command(context, "column_create Entries rank COLUMN_SCALAR Int32"); + run_command(context, "table_create Ranks TABLE_PAT_KEY Int32"); + run_command(context, + "column_create Ranks entries_rank COLUMN_INDEX Entries rank"); + + run_command(context, "load --table Entries"); + run_command(context, "["); + for (i = 0; i < data->n_records; i++) { +#define BUFFER_SIZE 4096 + gchar buffer[BUFFER_SIZE]; + const gchar *separator; + if (i == (data->n_records - 1)) { + separator = ""; + } else { + separator = ","; + } + snprintf(buffer, BUFFER_SIZE, "{\"rank\": %u}%s", i, separator); + run_command(context, buffer); +#undef BUFFER_SIZE + } + run_command(context, "]"); + } + + g_free(database_path); +} + +static void +bench_startup(BenchmarkData *data) +{ + if (data->use_mruby) { + g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE); + } else { + g_setenv("GRN_MRUBY_ENABLED", "no", TRUE); + } + grn_ctx_init(&(data->context), 0); + setup_database(data); +} + +static void +bench_shutdown(BenchmarkData *data) +{ + grn_ctx *context = &(data->context); + + grn_obj_close(context, data->database); + grn_ctx_fin(context); +} + +int +main(int argc, gchar **argv) +{ + BenchReporter *reporter; + gint n = 10; + + grn_init(); + + g_print("Process %d times in each pattern\n", n); + + bench_init(&argc, &argv); + reporter = bench_reporter_new(); + + { + BenchmarkData data_small_with_mruby; + BenchmarkData data_small_without_mruby; + BenchmarkData data_medium_with_mruby; + BenchmarkData data_medium_without_mruby; + BenchmarkData data_large_with_mruby; + BenchmarkData data_large_without_mruby; + BenchmarkData data_very_large_with_mruby; + BenchmarkData data_very_large_without_mruby; + +#define REGISTER(data, n_records_, min, max, use_mruby_) \ + do { \ + gchar *label; \ + label = g_strdup_printf("(%6d, %6d] (%7d): %7s mruby", \ + min, max, n_records_, \ + use_mruby_ ? "with" : "without"); \ + data.use_mruby = use_mruby_; \ + data.n_records = n_records_; \ + data.command = \ + "select Entries --cache no " \ + "--filter 'rank > " #min " && rank <= " #max "'"; \ + bench_startup(&data); \ + bench_reporter_register(reporter, label, \ + n, \ + NULL, \ + bench, \ + NULL, \ + &data); \ + g_free(label); \ + } while(FALSE) + + REGISTER(data_small_with_mruby, + 1000, + 500, 600, + GRN_TRUE); + REGISTER(data_small_without_mruby, + 1000, + 500, 600, + GRN_FALSE); + REGISTER(data_medium_with_mruby, + 10000, + 5000, 5100, + GRN_TRUE); + REGISTER(data_medium_without_mruby, + 10000, + 5000, 5100, + GRN_FALSE); + REGISTER(data_large_with_mruby, + 100000, + 50000, 50100, + GRN_TRUE); + REGISTER(data_large_without_mruby, + 100000, + 50000, 50100, + GRN_FALSE); + REGISTER(data_very_large_with_mruby, + 1000000, + 500000, 500100, + GRN_TRUE); + REGISTER(data_very_large_without_mruby, + 1000000, + 500000, 500100, + GRN_FALSE); + +#undef REGISTER + + bench_reporter_run(reporter); + + bench_shutdown(&data_small_with_mruby); + bench_shutdown(&data_small_without_mruby); + bench_shutdown(&data_medium_with_mruby); + bench_shutdown(&data_medium_without_mruby); + bench_shutdown(&data_large_with_mruby); + bench_shutdown(&data_large_without_mruby); + bench_shutdown(&data_very_large_with_mruby); + bench_shutdown(&data_very_large_without_mruby); + } + g_object_unref(reporter); + + grn_fin(); + + return 0; +} -------------- next part -------------- HTML����������������������������...Download