From f161bf585828a329e6d3a6a9b6fdf2535b9697bb Mon Sep 17 00:00:00 2001 From: "Sergei L. Khandrikov" Date: Tue, 12 Nov 2019 19:13:52 +0300 Subject: [PATCH] Add compile time benchmark --- benchmarks/CMakeLists.txt | 24 +++++++++++--- benchmarks/request.cpp.erb | 64 ++++++++++++++++++++++++++++++++++++ benchmarks/retry.cpp.erb | 67 ++++++++++++++++++++++++++++++++++++++ scripts/build.sh | 33 +++++++++++++++++++ 4 files changed, 183 insertions(+), 5 deletions(-) create mode 100644 benchmarks/request.cpp.erb create mode 100644 benchmarks/retry.cpp.erb diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index f21e681f7..019fd626d 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -1,9 +1,9 @@ -find_program(CCACHE_FOUND ccache) +# find_program(CCACHE_FOUND ccache) -if(CCACHE_FOUND) - set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) - set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) -endif() +# if(CCACHE_FOUND) +# set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) +# set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) +# endif() add_executable(ozo_benchmark ozo_benchmark.cpp) target_link_libraries(ozo_benchmark ozo) @@ -26,3 +26,17 @@ target_compile_options(ozo_benchmark_performance PRIVATE -Wall -Wextra -Wsign-co if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") target_compile_options(ozo_benchmark_performance PRIVATE -Wno-ignored-optimization-argument) endif() + +if(OZO_COMPILE_TIME_BENCHMARK) + # Actually include the module + include(metabench) + + # Add new datasets + metabench_add_dataset(request_dataset "request.cpp.erb" "[1, 2, 5, 10, 20, 50, 100, 200, 500]" MEDIAN_OF 3) + metabench_add_dataset(retry_dataset "retry.cpp.erb" "[1, 2, 5, 10, 20, 50, 100, 200, 500]" MEDIAN_OF 3) + + # Add a new chart + metabench_add_chart(chart DATASETS request_dataset retry_dataset) + target_link_libraries(request_dataset ozo) + target_link_libraries(retry_dataset ozo) +endif() diff --git a/benchmarks/request.cpp.erb b/benchmarks/request.cpp.erb new file mode 100644 index 000000000..bc2ddf965 --- /dev/null +++ b/benchmarks/request.cpp.erb @@ -0,0 +1,64 @@ +#include +#include +#include + +#include +#include + +#include + +namespace asio = boost::asio; + +const auto print_error = [](ozo::error_code ec, const auto& conn) { + std::cout << "error code message: \"" << ec.message(); + if (!ozo::is_null_recursive(conn)) { + std::cout << "\", libpq error message: \"" << ozo::error_message(conn) + << "\", error context: \"" << ozo::get_error_context(conn); + } + std::cout << "\"" << std::endl; +}; + +int main(int argc, char **argv) { + std::cout << "OZO request example" << std::endl; + + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " \n"; + return 1; + } + + asio::io_context io; + + auto conn_info = ozo::connection_info(argv[1]); + + const auto coroutine = [&] ([[maybe_unused]] asio::yield_context yield) { + [[maybe_unused]] ozo::rows_of result; + [[maybe_unused]] ozo::error_code ec; + +#if defined(METABENCH) + using namespace ozo::literals; + using namespace std::chrono_literals; + <% n.times do %> + { + const auto conn = ozo::request(conn_info[io], "SELECT <%= n %>"_SQL + std::int64_t(42), 1s, ozo::into(result), yield[ec]); + + if (ec) { + std::cout << "Request failed; "; + print_error(ec, conn); + return; + } + + std::cout << "Selected:" << std::endl; + for (auto value : result) { + std::cout << std::get<0>(value) << std::endl; + } + } + <% end %> +#endif // METABENCH + }; + + asio::spawn(io, coroutine); + + io.run(); + + return 0; +} diff --git a/benchmarks/retry.cpp.erb b/benchmarks/retry.cpp.erb new file mode 100644 index 000000000..8d9e2b3f2 --- /dev/null +++ b/benchmarks/retry.cpp.erb @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include +#include + +#include + +namespace asio = boost::asio; + +const auto print_error = [](ozo::error_code ec, const auto& conn) { + std::cout << "error code message: \"" << ec.message(); + if (!ozo::is_null_recursive(conn)) { + std::cout << "\", libpq error message: \"" << ozo::error_message(conn) + << "\", error context: \"" << ozo::get_error_context(conn); + } + std::cout << "\"" << std::endl; +}; + +int main(int argc, char **argv) { + std::cout << "OZO request example" << std::endl; + + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " \n"; + return 1; + } + + asio::io_context io; + + auto conn_info = ozo::connection_info(argv[1]); + + asio::spawn(io, [&] ([[maybe_unused]] asio::yield_context yield) { + [[maybe_unused]] ozo::rows_of result; + [[maybe_unused]] ozo::error_code ec; + using namespace ozo::literals; + using namespace std::chrono_literals; + namespace failover = ozo::failover; + using retry_options = failover::retry_options; + +#if defined(METABENCH) +<% n.times do %> + { + auto retry = 3*failover::retry(ozo::errc::connection_error) + .set(retry_options::on_retry = print_error); + auto conn = ozo::request[retry](conn_info[io], "SELECT <%= n %>"_SQL + std::int64_t(42), 1s, ozo::into(result), yield[ec]); + + if (ec) { + std::cout << "Request failed; "; + print_error(ec, conn); + return; + } + + std::cout << "Selected:" << std::endl; + for (auto value : result) { + std::cout << std::get<0>(value) << std::endl; + } + } +<% end %> +#endif + }); + + io.run(); + + return 0; +} diff --git a/scripts/build.sh b/scripts/build.sh index 667bfad87..107a9767b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -34,6 +34,10 @@ build_clang() { CMAKE_CXX_FLAGS_RELWITHDEBINFO='-g -O2 -fno-omit-frame-pointer -fsanitize=thread' build ;; + chart) + CMAKE_BUILD_TYPE=RelWithDebInfo + build_chart + ;; *) usage "bad clang target '$TARGET'";; esac } @@ -61,6 +65,10 @@ build_gcc() { OZO_COVERAGE=ON build ;; + chart) + CMAKE_BUILD_TYPE=RelWithDebInfo + build_chart + ;; *) usage "bad gcc target '$TARGET'";; esac } @@ -69,6 +77,31 @@ build_docs() { doxygen } +build_chart() { + echo "BASE_BUILD_DIR: ${BASE_BUILD_DIR}" + if ! [[ "${BASE_BUILD_DIR}" ]]; then + BASE_BUILD_DIR=build + fi + SOURCE_DIR=${PWD} + BUILD_DIR=${BASE_BUILD_DIR}/${COMPILER}_${TARGET} + mkdir -p ${BUILD_DIR} + cd ${BUILD_DIR} + cmake \ + -DCMAKE_C_COMPILER="${CC_COMPILER}" \ + -DCMAKE_CXX_COMPILER="${CXX_COMPILER}" \ + -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS"\ + -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="$CMAKE_CXX_FLAGS_RELWITHDEBINFO" \ + -DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE \ + -DOZO_BUILD_TESTS=ON \ + -DOZO_BUILD_EXAMPLES=ON \ + -DOZO_BUILD_BENCHMARKS=ON \ + -DOZO_COVERAGE=$OZO_COVERAGE \ + -DOZO_BUILD_PG_TESTS=$OZO_BUILD_PG_TESTS \ + -DOZO_PG_TEST_CONNINFO="host=${POSTGRES_HOST} port=5432 dbname=${POSTGRES_DB} user=${POSTGRES_USER} password=${POSTGRES_PASSWORD}" \ + ${SOURCE_DIR} + make chart +} + usage() { local NAME=`basename $0` echo $NAME: ERROR: $* 1>&2