susumu.yata
null+****@clear*****
Tue Sep 9 16:11:49 JST 2014
susumu.yata 2014-09-09 16:11:49 +0900 (Tue, 09 Sep 2014) New Revision: 8dd744ec50d974fce1d140f5414fe7506ca8ef58 https://github.com/groonga/grnxx/commit/8dd744ec50d974fce1d140f5414fe7506ca8ef58 Message: Add a benchmark for score adjustment. (#50) Added files: benchmark/Makefile.am benchmark/benchmark_adjuster.cpp Modified files: .gitignore Makefile.am configure.ac test/Makefile.am Renamed files: benchmark/benchmark_dummy.cpp (from test/benchmark_grnxx.cpp) Modified: .gitignore (+3 -1) =================================================================== --- .gitignore 2014-09-09 13:12:07 +0900 (38e351f) +++ .gitignore 2014-09-09 16:11:49 +0900 (373fdd8) @@ -15,6 +15,9 @@ Makefile Makefile.in aclocal.m4 autom4te.cache/ +benchmark/*.trs +benchmark/benchmark_adjuster +benchmark/benchmark_dummy compile config.* configure @@ -37,5 +40,4 @@ test/test_index test/test_expression test/test_sorter test/test_pipeline -test/benchmark_grnxx test-driver Modified: Makefile.am (+6 -1) =================================================================== --- Makefile.am 2014-09-09 13:12:07 +0900 (5c53eb7) +++ Makefile.am 2014-09-09 16:11:49 +0900 (8146423) @@ -1,6 +1,11 @@ ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = include lib src test +SUBDIRS = \ + include \ + lib \ + src \ + test \ + benchmark EXTRA_DIST = \ COPYING-GPL-3.0 \ Added: benchmark/Makefile.am (+11 -0) 100644 =================================================================== --- /dev/null +++ benchmark/Makefile.am 2014-09-09 16:11:49 +0900 (f488b48) @@ -0,0 +1,11 @@ +TESTS = \ + benchmark_adjuster \ + benchmark_dummy + +check_PROGRAMS = $(TESTS) + +benchmark_adjuster_SOURCES = benchmark_adjuster.cpp +benchmark_adjuster_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la + +benchmark_dummy_SOURCES = benchmark_dummy.cpp +benchmark_dummy_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la Added: benchmark/benchmark_adjuster.cpp (+247 -0) 100644 =================================================================== --- /dev/null +++ benchmark/benchmark_adjuster.cpp 2014-09-09 16:11:49 +0900 (7862fa6) @@ -0,0 +1,247 @@ +/* + Copyright (C) 2012-2014 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + 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 <cassert> +#include <ctime> +#include <iostream> +#include <random> + +#include "grnxx/column.hpp" +#include "grnxx/cursor.hpp" +#include "grnxx/datum.hpp" +#include "grnxx/db.hpp" +#include "grnxx/error.hpp" +#include "grnxx/expression.hpp" +#include "grnxx/pipeline.hpp" +#include "grnxx/sorter.hpp" +#include "grnxx/table.hpp" + +int NUM_ROWS = 1 << 16; +int NUM_LOOPS = 5; + +class Timer { + public: + Timer() : base_(now()) {} + + void reset() { + base_ = now(); + } + + double elapsed() const { + return now() - base_; + } + + private: + double base_; + + static double now() { + struct timespec current_time; + ::clock_gettime(CLOCK_MONOTONIC, ¤t_time); + return current_time.tv_sec + (current_time.tv_nsec / 1000000000.0); + } +}; + +struct { + grnxx::unique_ptr<grnxx::DB> db; + grnxx::Table *table; + grnxx::Array<grnxx::Bool> bool_values; + grnxx::Array<grnxx::Int> int_values; + grnxx::Array<grnxx::Float> float_values; +} test; + +void init_test() { + grnxx::Error error; + + // Create a database with the default options. + test.db = grnxx::open_db(&error, ""); + assert(test.db); + + // Create a table with the default options. + test.table = test.db->create_table(&error, "Table"); + assert(test.table); + + // Create columns for Bool, Int, and Float values. + grnxx::DataType data_type = grnxx::BOOL_DATA; + auto bool_column = test.table->create_column(&error, "Bool", data_type); + assert(bool_column); + + data_type = grnxx::INT_DATA; + auto int_column = test.table->create_column(&error, "Int", data_type); + assert(int_column); + + data_type = grnxx::FLOAT_DATA; + auto float_column = test.table->create_column(&error, "Float", data_type); + assert(float_column); + + // Generate random values. + // Bool: true or false. + // Int: [0, 100). + // Float: [0.0, 1.0]. + std::mt19937_64 mersenne_twister; + assert(test.bool_values.resize(&error, NUM_ROWS + 1)); + assert(test.int_values.resize(&error, NUM_ROWS + 1)); + assert(test.float_values.resize(&error, NUM_ROWS + 1)); + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + test.bool_values.set(i, (mersenne_twister() & 1) != 0); + test.int_values.set(i, mersenne_twister() % 100); + constexpr auto MAX_VALUE = mersenne_twister.max(); + test.float_values.set(i, 1.0 * mersenne_twister() / MAX_VALUE); + } + + // Store generated values into columns. + for (grnxx::Int i = 1; i <= NUM_ROWS; ++i) { + grnxx::Int row_id; + assert(test.table->insert_row(&error, grnxx::NULL_ROW_ID, + grnxx::Datum(), &row_id)); + assert(bool_column->set(&error, row_id, test.bool_values[i])); + assert(int_column->set(&error, row_id, test.int_values[i])); + assert(float_column->set(&error, row_id, test.float_values[i])); + } +} + +void test_adjust() { + grnxx::Error error; + + // Create an object for building a pipeline. + auto pipeline_builder = grnxx::PipelineBuilder::create(&error, test.table); + assert(pipeline_builder); + + // Create a cursor which reads all the records. + auto cursor = test.table->create_cursor(&error); + assert(cursor); + assert(pipeline_builder->push_cursor(&error, std::move(cursor))); + + // Create an object for building expressions. + auto expression_builder = + grnxx::ExpressionBuilder::create(&error, test.table); + assert(expression_builder); + + // Create an adjuster (Float). + assert(expression_builder->push_column(&error, "Float")); + auto expression = expression_builder->release(&error); + assert(expression); + assert(pipeline_builder->push_adjuster(&error, std::move(expression))); + + // Complete a pipeline. + auto pipeline = pipeline_builder->release(&error); + assert(pipeline); + + // Read records through the pipeline. + grnxx::Array<grnxx::Record> records; + assert(pipeline->flush(&error, &records)); +} + +void test_adjust2() { + grnxx::Error error; + + // Create an object for building a pipeline. + auto pipeline_builder = grnxx::PipelineBuilder::create(&error, test.table); + assert(pipeline_builder); + + // Create a cursor which reads all the records. + auto cursor = test.table->create_cursor(&error); + assert(cursor); + assert(pipeline_builder->push_cursor(&error, std::move(cursor))); + + // Create an object for building expressions. + auto expression_builder = + grnxx::ExpressionBuilder::create(&error, test.table); + assert(expression_builder); + + // Create an adjuster (Float). + assert(expression_builder->push_column(&error, "Int")); + assert(expression_builder->push_operator(&error, grnxx::TO_FLOAT_OPERATOR)); + assert(expression_builder->push_datum(&error, grnxx::Float(100.0))); + assert(expression_builder->push_operator(&error, grnxx::DIVISION_OPERATOR)); + assert(expression_builder->push_column(&error, "Float")); + assert(expression_builder->push_operator(&error, grnxx::PLUS_OPERATOR)); + auto expression = expression_builder->release(&error); + assert(expression); + assert(pipeline_builder->push_adjuster(&error, std::move(expression))); + + // Complete a pipeline. + auto pipeline = pipeline_builder->release(&error); + assert(pipeline); + + // Read records through the pipeline. + grnxx::Array<grnxx::Record> records; + assert(pipeline->flush(&error, &records)); +} + +void test_filter_and_adjust() { + grnxx::Error error; + + // Create an object for building a pipeline. + auto pipeline_builder = grnxx::PipelineBuilder::create(&error, test.table); + assert(pipeline_builder); + + // Create a cursor which reads all the records. + auto cursor = test.table->create_cursor(&error); + assert(cursor); + assert(pipeline_builder->push_cursor(&error, std::move(cursor))); + + // Create an object for building expressions. + auto expression_builder = + grnxx::ExpressionBuilder::create(&error, test.table); + assert(expression_builder); + + // Create a filter (Bool). + assert(expression_builder->push_column(&error, "Bool")); + auto expression = expression_builder->release(&error); + assert(expression); + assert(pipeline_builder->push_filter(&error, std::move(expression))); + + // Create an adjuster (Float). + assert(expression_builder->push_column(&error, "Float")); + expression = expression_builder->release(&error); + assert(expression); + assert(pipeline_builder->push_adjuster(&error, std::move(expression))); + + // Complete a pipeline. + auto pipeline = pipeline_builder->release(&error); + assert(pipeline); + + // Read records through the pipeline. + grnxx::Array<grnxx::Record> records; + assert(pipeline->flush(&error, &records)); +} + +void run_loop(void (*test)()) { + double total_elapsed = 0.0; + for (int i = 0; i < NUM_LOOPS; ++i) { + Timer timer; + test(); + double elapsed = timer.elapsed(); + std::cout << "elapsed [s] = " << elapsed << std::endl; + total_elapsed += elapsed; + } + std::cout << "total elapsed [s] = " << total_elapsed << std::endl; +} + +int main(int argc, char *argv[]) { + if (argc > 1) { + NUM_ROWS = std::atoi(argv[1]); + } + if (argc > 2) { + NUM_LOOPS = std::atoi(argv[2]); + } + init_test(); + run_loop(test_adjust); + run_loop(test_adjust2); + run_loop(test_filter_and_adjust); + return 0; +} Renamed: benchmark/benchmark_dummy.cpp (+0 -0) 100% =================================================================== Modified: configure.ac (+2 -1) =================================================================== --- configure.ac 2014-09-09 13:12:07 +0900 (1e74a12) +++ configure.ac 2014-09-09 16:11:49 +0900 (b76fb3c) @@ -56,7 +56,8 @@ AC_CONFIG_FILES([Makefile lib/Makefile lib/grnxx/Makefile src/Makefile - test/Makefile]) + test/Makefile + benchmark/Makefile]) AC_OUTPUT echo Modified: test/Makefile.am (+9 -13) =================================================================== --- test/Makefile.am 2014-09-09 13:12:07 +0900 (3550995) +++ test/Makefile.am 2014-09-09 16:11:49 +0900 (5af7d6b) @@ -1,13 +1,12 @@ -TESTS = \ - test_array \ - test_db \ - test_table \ - test_column \ - test_index \ - test_expression \ - test_sorter \ - test_pipeline \ - benchmark_grnxx +TESTS = \ + test_array \ + test_db \ + test_table \ + test_column \ + test_index \ + test_expression \ + test_sorter \ + test_pipeline check_PROGRAMS = $(TESTS) @@ -34,6 +33,3 @@ test_sorter_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la test_pipeline_SOURCES = test_pipeline.cpp test_pipeline_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la - -benchmark_grnxx_SOURCES = benchmark_grnxx.cpp -benchmark_grnxx_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la -------------- next part -------------- HTML����������������������������...Download