Skip to content

Commit

Permalink
Add google benchmark library with example
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Vaneev <[email protected]>
  • Loading branch information
Warchant committed Oct 12, 2017
1 parent f6588cc commit 83f4923
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ include/generated/*
.scannerwork/
iroha.conf
peers.list
cmake-build*

4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ if(TESTING)
add_subdirectory(test)
endif()

if(BENCHMARKING)
add_subdirectory(benchmark)
endif()

if (FUZZING)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_subdirectory(fuzz)
Expand Down
35 changes: 35 additions & 0 deletions cmake/Modules/Findbenchmark.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
add_library(benchmark UNKNOWN IMPORTED)

find_path(benchmark_INCLUDE_DIR benchmark/benchmark.h)
mark_as_advanced(benchmark_INCLUDE_DIR)

find_library(benchmark_LIBRARY benchmark)
mark_as_advanced(benchmark_LIBRARY)

find_package(PackageHandleStandardArgs REQUIRED)
find_package_handle_standard_args(benchmark DEFAULT_MSG
benchmark_INCLUDE_DIR
benchmark_LIBRARY
)

if (NOT benchmark_FOUND)
externalproject_add(google_benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG 'v1.2.0'
INSTALL_COMMAND "" # remove install step
TEST_COMMAND "" # remove test step
UPDATE_COMMAND "" # remove update step
)
externalproject_get_property(google_benchmark binary_dir)
set(benchmark_INCLUDE_DIR ${binary_dir}/include)
set(benchmark_LIBRARY ${binary_dir}/lib/libbenchmark.a)
file(MAKE_DIRECTORY ${benchmark_INCLUDE_DIR})

add_dependencies(benchmark google_benchmark)
endif ()

set_target_properties(benchmark PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${benchmark_INCLUDE_DIR}
IMPORTED_LOCATION ${benchmark_LIBRARY}
INTERFACE_LINK_LIBRARIES "pthread"
)
12 changes: 11 additions & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ find_package(tbb)
##########################
# boost multiprecision #
##########################
find_package(Boost REQUIRED COMPONENTS filesystem)
find_package(Boost REQUIRED
COMPONENTS
filesystem
system
)


##########################
# benchmark #
##########################
find_package(benchmark)
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test_bin)
add_subdirectory(module)
add_subdirectory(framework)
add_subdirectory(integration)

if(BENCHMARKING)
add_subdirectory(benchmark)
endif()
7 changes: 7 additions & 0 deletions test/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# directory contains benchmarks
add_executable(benchmark_example
benchmark_example.cpp
)
target_link_libraries(benchmark_example
benchmark
)
47 changes: 47 additions & 0 deletions test/benchmark/benchmark_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

///
/// Documentation is at https://github.com/google/benchmark
///
/// General recommendations:
/// - build with -DCMAKE_BUILD_TYPE=Release
/// - disable CPU scaling (frequency changes depending on workload)
/// - put initialization code in fixtures
/// - write meaningful names for benchmarks
///

#include <benchmark/benchmark.h>
#include <string>

/// Test how long is empty std::string creation
// define a static function, which accepts 'state'
// function's name = benchmark's name
static void BM_StringCreation(benchmark::State &state) {
// define main benchmark loop
while (state.KeepRunning()) {
// define the code to be tested
std::string empty_string;
}
}
// define benchmark
BENCHMARK(BM_StringCreation);

/// That's all. More in documentation.

// don't forget to include this:
BENCHMARK_MAIN();

0 comments on commit 83f4923

Please sign in to comment.