null+****@clear*****
null+****@clear*****
2011年 2月 21日 (月) 15:01:59 JST
Kouhei Sutou 2011-02-21 06:01:59 +0000 (Mon, 21 Feb 2011)
New Revision: b442493bf3e62beef28d48cdfc5a7cf0e8fb3b49
Log:
use a grn_ctx per thread.
Added files:
test/unit/grntest/test-gqtp.rb
Modified files:
src/grntest.c
test/unit/grntest/Makefile.am
Modified: src/grntest.c (+12 -15)
===================================================================
--- src/grntest.c 2011-02-21 05:59:06 +0000 (039eeb4)
+++ src/grntest.c 2011-02-21 06:01:59 +0000 (82b04c3)
@@ -1143,7 +1143,7 @@ worker_sub(grn_ctx *ctx, grn_obj *log, int task_id)
typedef struct _grntest_worker {
grn_ctx *ctx;
- grn_obj *log;
+ grn_obj log;
int task_id;
} grntest_worker;
@@ -1153,7 +1153,7 @@ __stdcall
worker(void *val)
{
grntest_worker *worker = val;
- worker_sub(worker->ctx, worker->log, worker->task_id);
+ worker_sub(worker->ctx, &worker->log, worker->task_id);
return 0;
}
#else
@@ -1161,14 +1161,14 @@ static void *
worker(void *val)
{
grntest_worker *worker = val;
- worker_sub(worker->ctx, worker->log, worker->task_id);
+ worker_sub(worker->ctx, &worker->log, worker->task_id);
return NULL;
}
#endif /* WIN32 */
#ifdef WIN32
static int
-thread_main(grn_ctx *ctx, grn_obj *log, int num)
+thread_main(grn_ctx *ctx, int num)
{
int i;
int ret;
@@ -1177,8 +1177,8 @@ thread_main(grn_ctx *ctx, grn_obj *log, int num)
for (i = 0; i < num; i++) {
workers[i] = GRN_MALLOC(sizeof(grntest_worker));
- workers[i]->ctx = ctx;
- workers[i]->log = log;
+ workers[i]->ctx = &grntest_ctx[i];
+ GRN_TEXT_INIT(&workers[i]->log, 0);
workers[i]->task_id = i;
pthread[i] = (HANDLE)_beginthreadex(NULL, 0, worker, (void *)workers[i],
0, NULL);
@@ -1196,13 +1196,14 @@ thread_main(grn_ctx *ctx, grn_obj *log, int num)
for (i = 0; i < num; i++) {
CloseHandle(pthread[i]);
+ GRN_OBJ_FIN(workers[i]->ctx, &workers[i]->log);
GRN_FREE(workers[i]);
}
return 0;
}
#else
static int
-thread_main(grn_ctx *ctx, grn_obj *log, int num)
+thread_main(grn_ctx *ctx, int num)
{
intptr_t i;
int ret;
@@ -1211,8 +1212,8 @@ thread_main(grn_ctx *ctx, grn_obj *log, int num)
for (i = 0; i < num; i++) {
workers[i] = GRN_MALLOC(sizeof(grntest_worker));
- workers[i]->ctx = ctx;
- workers[i]->log = log;
+ workers[i]->ctx = &grntest_ctx[i];
+ GRN_TEXT_INIT(&workers[i]->log, 0);
workers[i]->task_id = i;
ret = pthread_create(&pthread[i], NULL, worker, (void *)workers[i]);
if (ret) {
@@ -1223,6 +1224,7 @@ thread_main(grn_ctx *ctx, grn_obj *log, int num)
for (i = 0; i < num; i++) {
ret = pthread_join(pthread[i], NULL);
+ GRN_OBJ_FIN(workers[i]->ctx, &workers[i]->log);
GRN_FREE(workers[i]);
if (ret) {
fprintf(stderr, "Cannot join thread:ret=%d\n", ret);
@@ -2016,12 +2018,7 @@ printf("%d:type =%d:file=%s:con=%d:ntimes=%d\n", i, grntest_job[i].jobtype,
fflush(grntest_logfp);
}
- {
- grn_obj log;
- GRN_TEXT_INIT(&log, 0);
- thread_main(ctx, &log, task_num);
- GRN_OBJ_FIN(ctx, &log);
- }
+ thread_main(ctx, task_num);
for (i = 0; i < task_num; i++) {
if (grntest_owndb[i]) {
Modified: test/unit/grntest/Makefile.am (+2 -1)
===================================================================
--- test/unit/grntest/Makefile.am 2011-02-21 05:59:06 +0000 (919910a)
+++ test/unit/grntest/Makefile.am 2011-02-21 06:01:59 +0000 (7a1735e)
@@ -1,2 +1,3 @@
EXTRA_DIST = \
- test-http.rb
+ test-http.rb \
+ test-gptq.rb
Added: test/unit/grntest/test-gqtp.rb (+54 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/grntest/test-gqtp.rb 2011-02-21 06:01:59 +0000 (8fa35b1)
@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2011 Kouhei Sutou <kou****@clear*****>
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+class GrnTestGQTPTest < Test::Unit::TestCase
+ include GroongaGrnTestTestUtils
+
+ CONFIG_ENV = {"GRN_CONFIG_PATH" => ""}
+
+ def setup
+ setup_database
+ end
+
+ def teardown
+ teardown_database
+ end
+
+ def test_do_multi_thread
+ command = 'select Shops --sortby _id --limit 5 --output_columns "name"'
+ command_file = tempfile("command") do |file|
+ file.puts(command)
+ end
+ script_file = tempfile("script") do |file|
+ file.puts("do_gqtp #{command_file.path} 10 5")
+ file.puts("do_gqtp #{command_file.path} 4 2")
+ end
+ output, error, status = invoke_grntest("--noftp",
+ "--groonga", groonga,
+ "--protocol", "gqtp",
+ "--log-output-dir", @tmp_dir,
+ script_file.path,
+ @database_path)
+ assert_predicate(status, :success?, [output, error])
+ log_file = nil
+ Dir.glob("#{@tmp_dir}/*.log") do |file|
+ log_file = file
+ end
+ result = JSON.parse(File.read(log_file))
+ assert_equal((10 * 5) + (4 * 2), result.last["queries"])
+ end
+end