From 5f94e79c956f8d0a12dc2b782f3517592c0667e5 Mon Sep 17 00:00:00 2001
From: Stuart Mark James <stuart.james@esrf.fr>
Date: Thu, 18 Jul 2019 14:31:44 +0200
Subject: [PATCH] Added google benchmark and some initial tests

---
 .gitmodules                  |  6 ++++++
 CHANGELOG.md                 |  9 ++++++++-
 CMakeLists.txt               | 22 +++++++++++++++++++++-
 src/LibUtils.cpp             |  9 +++++++++
 src/LibUtils.hpp             |  5 +++++
 src/QueryBuilder.hpp         |  2 +-
 thirdparty/google/benchmark  |  1 +
 thirdparty/google/googletest |  1 +
 8 files changed, 52 insertions(+), 3 deletions(-)
 create mode 160000 thirdparty/google/benchmark
 create mode 160000 thirdparty/google/googletest

diff --git a/.gitmodules b/.gitmodules
index 352f749..92fd4e6 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -7,3 +7,9 @@
 [submodule "thirdparty/Catch2"]
 	path = thirdparty/Catch2
 	url = https://github.com/catchorg/Catch2.git
+[submodule "thirdparty/google/googletest"]
+	path = thirdparty/google/googletest
+	url = https://github.com/google/googletest.git
+[submodule "thirdparty/google/benchmark"]
+	path = thirdparty/google/benchmark
+	url = https://github.com/google/benchmark.git
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 98121ca..64b2592 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,8 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 
 ## [Unreleased]
 
+### Added
+
+- Google benchmark submodule for micro benchmarking. This will enabled future optimisation
+  - Benchmark tests for QueryBuilder
+
 ## [0.9.0] - 2019-07-12
 
 ### Added
 
-* Pre v1 release of the library. This is an almost complete version of the library. Next couple of versions will complete the library before a v1 release.
\ No newline at end of file
+- Pre v1 release of the library. 
+  - This is an almost complete version of the library. 
+  - Next couple of versions will complete the library before a v1 release.
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 507e795..346a4cd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,12 +34,18 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 
 # Build options
 option(BUILD_UNIT_TESTS "Build unit tests" OFF)
+option(BUILD_BENCHMARK_TESTS "Build benchmarking tests (Forces RELEASE build)" OFF)
 option(ENABLE_CLANG "Enable clang code and layout analysis" OFF)
 
 if(BUILD_UNIT_TESTS)
     message(STATUS "Unit tests will be built")
 endif(BUILD_UNIT_TESTS)
 
+if(BUILD_BENCHMARK_TESTS)
+    message(STATUS "Benchmark tests will be built (Forces RELEASE build)")
+    set(CMAKE_BUILD_TYPE "Release")
+endif(BUILD_BENCHMARK_TESTS)
+
 # arch install definitions
 include(GNUInstallDirs)
 
@@ -69,6 +75,16 @@ find_package(Tango)
 set(THREADS_PREFER_PTHREAD_FLAG ON)
 find_package(Threads REQUIRED)
 
+# Thirdparty Integration -----------------------------------
+
+# build google benchmark (target: benchmark)
+# do not build tests of benchmarking lib
+set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Suppressing benchmark's tests" FORCE)
+add_subdirectory(thirdparty/google/benchmark)
+
+# build tests (targets: gtest_main, gtest)
+add_subdirectory(thirdparty/google/googletest/googletest)
+
 # Include the thirdparty projects
 add_subdirectory(thirdparty/libhdbpp EXCLUDE_FROM_ALL)
 add_subdirectory(thirdparty/libpqxx  EXCLUDE_FROM_ALL)
@@ -168,4 +184,8 @@ install(
 # Tests -----------------------------------
 if(BUILD_UNIT_TESTS)
     add_subdirectory(test)
-endif(BUILD_UNIT_TESTS)
\ No newline at end of file
+endif(BUILD_UNIT_TESTS)
+
+if(BUILD_BENCHMARK_TESTS)
+    add_subdirectory(benchmark)
+endif(BUILD_BENCHMARK_TESTS)
\ No newline at end of file
diff --git a/src/LibUtils.cpp b/src/LibUtils.cpp
index 4ff9713..9be85ba 100644
--- a/src/LibUtils.cpp
+++ b/src/LibUtils.cpp
@@ -175,10 +175,19 @@ void LogConfigurator::initLogging(bool enable_file, bool enable_console, const s
     catch (const spdlog::spdlog_ex &ex)
     {
         string msg {"Failed to initialise the logging system, caught error: " + string(ex.what())};
+        cout << msg << endl;
         Tango::Except::throw_exception("Runtime Error", msg, LOCATION_INFO);
     }
 }
 
+//=============================================================================
+//=============================================================================
+void LogConfigurator::initLoggingMetrics(bool enable_file, bool enable_console, const string &log_file_name)
+{
+    auto logger = spdlog::get(LibLoggerName);
+    if (!logger) initLogging(enable_file, enable_console, log_file_name);
+}
+
 //=============================================================================
 //=============================================================================
 void LogConfigurator::shutdownLogging()
diff --git a/src/LibUtils.hpp b/src/LibUtils.hpp
index 1fabc1b..23beb84 100644
--- a/src/LibUtils.hpp
+++ b/src/LibUtils.hpp
@@ -64,6 +64,11 @@ const string LibLoggerName = "hdbpp";
 struct LogConfigurator
 {
     static void initLogging(bool enable_file, bool enable_console, const std::string &log_file_name = "");
+
+    // this version is used for metrics testing, and ignores the call if the
+    // logger already exists
+    static void initLoggingMetrics(bool enable_file, bool enable_console, const std::string &log_file_name = "");
+
     static void shutdownLogging();
     static void setLoggingLevel(spdlog::level::level_enum level);
 };
diff --git a/src/QueryBuilder.hpp b/src/QueryBuilder.hpp
index a882807..79cb042 100644
--- a/src/QueryBuilder.hpp
+++ b/src/QueryBuilder.hpp
@@ -30,7 +30,7 @@
 
 namespace std
 {
-// This is a custom compariator for the AttributeTraits class to allow its
+// This is a custom comparator for the AttributeTraits class to allow its
 // use as a key in a map. The traits class is used as a key to reference
 // cached queries
 template<>
diff --git a/thirdparty/google/benchmark b/thirdparty/google/benchmark
new file mode 160000
index 0000000..090faec
--- /dev/null
+++ b/thirdparty/google/benchmark
@@ -0,0 +1 @@
+Subproject commit 090faecb454fbd6e6e17a75ef8146acb037118d4
diff --git a/thirdparty/google/googletest b/thirdparty/google/googletest
new file mode 160000
index 0000000..2ef13f5
--- /dev/null
+++ b/thirdparty/google/googletest
@@ -0,0 +1 @@
+Subproject commit 2ef13f524b837a68bae27ae1123da0400dff6285
-- 
GitLab