[Groonga-commit] groonga/groonga at d795c99 [master] Add a benchmark script that shows how increase memory usage by mruby

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Oct 21 19:15:33 JST 2013


Kouhei Sutou	2013-10-21 19:15:33 +0900 (Mon, 21 Oct 2013)

  New Revision: d795c9947f89644a71d2088c19a19610e3a3119d
  https://github.com/groonga/groonga/commit/d795c9947f89644a71d2088c19a19610e3a3119d

  Message:
    Add a benchmark script that shows how increase memory usage by mruby

  Added files:
    benchmark/bench-ctx-create.c
  Modified files:
    .gitignore
    benchmark/Makefile.am

  Modified: .gitignore (+1 -0)
===================================================================
--- .gitignore    2013-10-21 19:11:34 +0900 (f795e61)
+++ .gitignore    2013-10-21 19:15:33 +0900 (9f1b23e)
@@ -84,6 +84,7 @@ cmake_install.cmake
 /benchmark/bench-normalize
 /benchmark/bench-geo-distance
 /benchmark/bench-table-factory
+/benchmark/bench-ctx-create
 /packages/apt/debian/groonga-keyring.postrm
 /packages/apt/repositories/debian/pool/*/*/*/*/*.diff.gz
 /packages/apt/repositories/debian/pool/*/*/*/*/*.tar.gz

  Modified: benchmark/Makefile.am (+11 -2)
===================================================================
--- benchmark/Makefile.am    2013-10-21 19:11:34 +0900 (ed073d4)
+++ benchmark/Makefile.am    2013-10-21 19:15:33 +0900 (b753566)
@@ -8,7 +8,8 @@ if WITH_BENCHMARK
 noinst_PROGRAMS =		\
 	bench-table-factory	\
 	bench-geo-distance	\
-	bench-geo-select
+	bench-geo-select	\
+	bench-ctx-create
 endif
 
 AM_CPPFLAGS =			\
@@ -35,10 +36,14 @@ nodist_EXTRA_bench_geo_distance_SOURCES = $(NONEXISTENT_CXX_SOURCE)
 bench_geo_select_SOURCES = bench-geo-select.c
 nodist_EXTRA_bench_geo_select_SOURCES = $(NONEXISTENT_CXX_SOURCE)
 
+bench_ctx_create_SOURCES = bench-ctx-create.c
+nodist_EXTRA_bench_ctx_create_SOURCES = $(NONEXISTENT_CXX_SOURCE)
+
 benchmarks =					\
 	run-bench-table-factory			\
 	run-bench-geo-distance			\
-	run-bench-geo-select
+	run-bench-geo-select			\
+	run-bench-ctx-create
 
 run-bench-table-factory: bench-table-factory
 	@echo $@:
@@ -56,4 +61,8 @@ run-bench-geo-select: bench-geo-select
 	  srcdir="$(srcdir)"			\
 	  $(srcdir)/bench-geo-select.sh
 
+run-bench-ctx-create: bench-ctx-create
+	@echo $@:
+	./bench-ctx-create
+
 benchmark: $(benchmarks)

  Added: benchmark/bench-ctx-create.c (+164 -0) 100644
===================================================================
--- /dev/null
+++ benchmark/bench-ctx-create.c    2013-10-21 19:15:33 +0900 (8c0c278)
@@ -0,0 +1,164 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+  Copyright (C) 2013  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
+*/
+
+/*
+  Groonga: a4f613dc6c6c07e1b695a0065c6fbc8e5f18022b
+  CFLAGS: -O0 -ggdb3
+  % (cd benchmark/ && make --quiet run-bench-ctx-create)
+  run-bench-ctx-create:
+     with mruby:    464KB
+  without mruby:      0KB
+*/
+
+#include <stdlib.h>
+
+#include <glib.h>
+
+#include <groonga.h>
+
+typedef void (*BenchFunc)         (gpointer user_data);
+
+typedef struct _BenchmarkData {
+  grn_ctx context;
+  grn_obj *database;
+} BenchmarkData;
+
+static guint
+get_memory_usage(void)
+{
+  GRegex *vm_rss_pattern;
+  gchar *status;
+  GMatchInfo *match_info;
+  gchar *vm_rss_string;
+  guint vm_rss;
+
+  g_file_get_contents("/proc/self/status", &status, NULL, NULL);
+
+  vm_rss_pattern = g_regex_new("VmRSS:\\s*(\\d*)\\s+kB", 0, 0, NULL);
+  if (!g_regex_match(vm_rss_pattern, status, 0, &match_info)) {
+    g_print("not match...: %s\n", status);
+    return 0;
+  }
+  vm_rss_string = g_match_info_fetch(match_info, 1);
+  vm_rss = atoi(vm_rss_string);
+  g_free(vm_rss_string);
+  g_match_info_free(match_info);
+  g_regex_unref(vm_rss_pattern);
+  g_free(status);
+
+  return vm_rss;
+}
+
+static void
+bench_with_mruby(gpointer user_data)
+{
+  BenchmarkData *data = user_data;
+
+  g_setenv("GRN_MRUBY_ENABLED", "yes", TRUE);
+  grn_ctx_init(&(data->context), 0);
+  grn_ctx_use(&(data->context), data->database);
+}
+
+static void
+bench_without_mruby(gpointer user_data)
+{
+  BenchmarkData *data = user_data;
+
+  g_setenv("GRN_MRUBY_ENABLED", "no", TRUE);
+  grn_ctx_init(&(data->context), 0);
+  grn_ctx_use(&(data->context), data->database);
+}
+
+static void
+bench_teardown(gpointer user_data)
+{
+  BenchmarkData *data = user_data;
+
+  grn_ctx_fin(&(data->context));
+}
+
+static void
+bench(const gchar *label, BenchFunc bench_func, BenchFunc teardown_func,
+      gpointer user_data)
+{
+  guint memory_usage_before;
+
+  memory_usage_before = get_memory_usage();
+  bench_func(user_data);
+  g_print("%s: %6dKB\n", label, get_memory_usage() - memory_usage_before);
+  teardown_func(user_data);
+}
+
+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 grn_obj *
+setup_database(grn_ctx *context)
+{
+  gchar *tmp_dir;
+  gchar *database_path;
+  grn_obj *database;
+
+  tmp_dir = get_tmp_dir();
+  database_path = g_build_filename(tmp_dir, "ctx-create", "db", NULL);
+  database = grn_db_open(context, database_path);
+
+  g_free(database_path);
+
+  return database;
+}
+
+static void
+teardown_database(grn_ctx *context, grn_obj *database)
+{
+  grn_obj_close(context, database);
+}
+
+int
+main(int argc, gchar **argv)
+{
+  grn_ctx context;
+  BenchmarkData data;
+
+  grn_init();
+
+  grn_ctx_init(&context, 0);
+
+  data.database = setup_database(&context);
+
+  bench("   with mruby", bench_with_mruby,    bench_teardown, &data);
+  bench("without mruby", bench_without_mruby, bench_teardown, &data);
+
+  teardown_database(&context, data.database);
+
+  grn_ctx_fin(&context);
+
+  grn_fin();
+
+  return 0;
+}
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index