susumu.yata
null+****@clear*****
Fri Dec 5 19:07:27 JST 2014
susumu.yata 2014-12-05 19:07:27 +0900 (Fri, 05 Dec 2014) New Revision: 9f41808cfe198efa3ca7ebead043042041f70e48 https://github.com/groonga/grnxx/commit/9f41808cfe198efa3ca7ebead043042041f70e48 Message: Add a benchmark for foreign key. (#127) Added files: benchmark/benchmark_foreign_key.cpp Modified files: .gitignore benchmark/Makefile.am Modified: .gitignore (+1 -0) =================================================================== --- .gitignore 2014-12-05 19:06:16 +0900 (eee1a27) +++ .gitignore 2014-12-05 19:07:27 +0900 (ba524d9) @@ -20,6 +20,7 @@ benchmark/benchmark_filter_less benchmark/benchmark_filter_and benchmark/benchmark_filter_or benchmark/benchmark_filter_reference +benchmark/benchmark_foreign_key benchmark/benchmark_sorter compile config.* Modified: benchmark/Makefile.am (+4 -0) =================================================================== --- benchmark/Makefile.am 2014-12-05 19:06:16 +0900 (6624d94) +++ benchmark/Makefile.am 2014-12-05 19:07:27 +0900 (eea5c51) @@ -3,6 +3,7 @@ noinst_PROGRAMS = \ benchmark_filter_and \ benchmark_filter_or \ benchmark_filter_reference \ + benchmark_foreign_key \ benchmark_sorter benchmark_filter_less_SOURCES = benchmark_filter_less.cpp @@ -17,5 +18,8 @@ benchmark_filter_or_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la benchmark_filter_reference_SOURCES = benchmark_filter_reference.cpp benchmark_filter_reference_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la +benchmark_foreign_key_SOURCES = benchmark_foreign_key.cpp +benchmark_foreign_key_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la + benchmark_sorter_SOURCES = benchmark_sorter.cpp benchmark_sorter_LDADD = $(top_srcdir)/lib/grnxx/libgrnxx.la Added: benchmark/benchmark_foreign_key.cpp (+154 -0) 100644 =================================================================== --- /dev/null +++ benchmark/benchmark_foreign_key.cpp 2014-12-05 19:07:27 +0900 (3224516) @@ -0,0 +1,154 @@ +/* + 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 <set> + +#include "grnxx/db.hpp" +#include "grnxx/pipeline.hpp" + +namespace { + +constexpr size_t VALUES_SIZE = 10000; +constexpr size_t REFS_SIZE = 1000000; +constexpr size_t LOOP = 5; + +class Timer { + public: + Timer() : base_(now()) {} + + double elapsed() const { + return now() - base_; + } + + static double now() { + struct timespec ts; + ::clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec + (ts.tv_nsec / 1000000000.0); + } + + private: + double base_; +}; + +grnxx::Array<grnxx::Text> values; +grnxx::Array<grnxx::String> bodies; +grnxx::Array<grnxx::Int> refs; + +void generate_data() { + std::mt19937_64 rng; + + std::set<grnxx::String> set; + while (set.size() < VALUES_SIZE) { + constexpr size_t MIN_SIZE = 16; + constexpr size_t MAX_SIZE = 255; + grnxx::String string(MIN_SIZE + (rng() % (MAX_SIZE - MIN_SIZE + 1))); + for (size_t i = 0; i < string.size(); ++i) { + string[i] = '0' + (rng() % 10); + } + if (set.insert(string).second) { + values.push_back(grnxx::Text(string.data(), string.size())); + bodies.push_back(std::move(string)); + } + } + + refs.resize(REFS_SIZE); + for (size_t i = 0; i < REFS_SIZE; ++i) { + refs[i] = grnxx::Int(rng() % VALUES_SIZE); + } +} + +void benchmark_direct_build() { + std::cout << __PRETTY_FUNCTION__ << std::endl; + + double min_elapsed = std::numeric_limits<double>::max(); + for (size_t i = 0; i < LOOP; ++i) { + Timer timer; + + auto db = grnxx::open_db(""); + auto table = db->create_table("Values"); + auto column = table->create_column("Value", grnxx::TEXT_DATA); + for (size_t i = 0; i < VALUES_SIZE; ++i) { + grnxx::Int row_id = table->insert_row(); + column->set(row_id, values[i]); + } + + table = db->create_table("Refs"); + grnxx::ColumnOptions options; + options.reference_table_name = "Values"; + column = table->create_column("Ref", grnxx::INT_DATA, options); + for (size_t i = 0; i < REFS_SIZE; ++i) { + grnxx::Int row_id = table->insert_row(); + column->set(row_id, refs[i]); + } + + double elapsed = timer.elapsed(); + if (elapsed < min_elapsed) { + min_elapsed = elapsed; + } + } + std::cout << "min. elapsed [s] = " << min_elapsed << std::endl; +} + +void benchmark_indirect_build() try { + std::cout << __PRETTY_FUNCTION__ << std::endl; + + double min_elapsed = std::numeric_limits<double>::max(); + for (size_t i = 0; i < LOOP; ++i) { + Timer timer; + + auto db = grnxx::open_db(""); + auto to_table = db->create_table("Values"); + auto column = to_table->create_column("Value", grnxx::TEXT_DATA); + column->create_index("Index", grnxx::TREE_INDEX); + to_table->set_key_column("Value"); + for (size_t j = 0; j < VALUES_SIZE; ++j) { + to_table->insert_row(values[j]); + } + + auto from_table = db->create_table("Refs"); + grnxx::ColumnOptions options; + options.reference_table_name = "Values"; + column = from_table->create_column("Ref", grnxx::INT_DATA, options); + for (size_t j = 0; j < REFS_SIZE; ++j) { + grnxx::Int row_id = from_table->insert_row(); + column->set(row_id, to_table->find_row(values[refs[j].raw()])); + } + + double elapsed = timer.elapsed(); + if (elapsed < min_elapsed) { + min_elapsed = elapsed; + } + } + std::cout << "min. elapsed [s] = " << min_elapsed << std::endl; +} catch (const char *x) { + std::cout << "x = " << x << std::endl; +} + +} // namespace + +int main() { + generate_data(); + + benchmark_direct_build(); + benchmark_indirect_build(); + + return 0; +} -------------- next part -------------- HTML����������������������������... Descargar