LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.6 KiB

пре 1 месец
  1. # Benchmark
  2. [![build-and-test](https://github.com/google/benchmark/workflows/build-and-test/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Abuild-and-test)
  3. [![bazel](https://github.com/google/benchmark/actions/workflows/bazel.yml/badge.svg)](https://github.com/google/benchmark/actions/workflows/bazel.yml)
  4. [![pylint](https://github.com/google/benchmark/workflows/pylint/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Apylint)
  5. [![test-bindings](https://github.com/google/benchmark/workflows/test-bindings/badge.svg)](https://github.com/google/benchmark/actions?query=workflow%3Atest-bindings)
  6. [![Build Status](https://travis-ci.org/google/benchmark.svg?branch=main)](https://travis-ci.org/google/benchmark)
  7. [![Coverage Status](https://coveralls.io/repos/google/benchmark/badge.svg)](https://coveralls.io/r/google/benchmark)
  8. A library to benchmark code snippets, similar to unit tests. Example:
  9. ```c++
  10. #include <benchmark/benchmark.h>
  11. static void BM_SomeFunction(benchmark::State& state) {
  12. // Perform setup here
  13. for (auto _ : state) {
  14. // This code gets timed
  15. SomeFunction();
  16. }
  17. }
  18. // Register the function as a benchmark
  19. BENCHMARK(BM_SomeFunction);
  20. // Run the benchmark
  21. BENCHMARK_MAIN();
  22. ```
  23. ## Getting Started
  24. To get started, see [Requirements](#requirements) and
  25. [Installation](#installation). See [Usage](#usage) for a full example and the
  26. [User Guide](docs/user_guide.md) for a more comprehensive feature overview.
  27. It may also help to read the [Google Test documentation](https://github.com/google/googletest/blob/main/docs/primer.md)
  28. as some of the structural aspects of the APIs are similar.
  29. ## Resources
  30. [Discussion group](https://groups.google.com/d/forum/benchmark-discuss)
  31. IRC channels:
  32. * [libera](https://libera.chat) #benchmark
  33. [Additional Tooling Documentation](docs/tools.md)
  34. [Assembly Testing Documentation](docs/AssemblyTests.md)
  35. [Building and installing Python bindings](docs/python_bindings.md)
  36. ## Requirements
  37. The library can be used with C++03. However, it requires C++11 to build,
  38. including compiler and standard library support.
  39. The following minimum versions are required to build the library:
  40. * GCC 4.8
  41. * Clang 3.4
  42. * Visual Studio 14 2015
  43. * Intel 2015 Update 1
  44. See [Platform-Specific Build Instructions](docs/platform_specific_build_instructions.md).
  45. ## Installation
  46. This describes the installation process using cmake. As pre-requisites, you'll
  47. need git and cmake installed.
  48. _See [dependencies.md](docs/dependencies.md) for more details regarding supported
  49. versions of build tools._
  50. ```bash
  51. # Check out the library.
  52. $ git clone https://github.com/google/benchmark.git
  53. # Go to the library root directory
  54. $ cd benchmark
  55. # Make a build directory to place the build output.
  56. $ cmake -E make_directory "build"
  57. # Generate build system files with cmake, and download any dependencies.
  58. $ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
  59. # or, starting with CMake 3.13, use a simpler form:
  60. # cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
  61. # Build the library.
  62. $ cmake --build "build" --config Release
  63. ```
  64. This builds the `benchmark` and `benchmark_main` libraries and tests.
  65. On a unix system, the build directory should now look something like this:
  66. ```
  67. /benchmark
  68. /build
  69. /src
  70. /libbenchmark.a
  71. /libbenchmark_main.a
  72. /test
  73. ...
  74. ```
  75. Next, you can run the tests to check the build.
  76. ```bash
  77. $ cmake -E chdir "build" ctest --build-config Release
  78. ```
  79. If you want to install the library globally, also run:
  80. ```
  81. sudo cmake --build "build" --config Release --target install
  82. ```
  83. Note that Google Benchmark requires Google Test to build and run the tests. This
  84. dependency can be provided two ways:
  85. * Checkout the Google Test sources into `benchmark/googletest`.
  86. * Otherwise, if `-DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON` is specified during
  87. configuration as above, the library will automatically download and build
  88. any required dependencies.
  89. If you do not wish to build and run the tests, add `-DBENCHMARK_ENABLE_GTEST_TESTS=OFF`
  90. to `CMAKE_ARGS`.
  91. ### Debug vs Release
  92. By default, benchmark builds as a debug library. You will see a warning in the
  93. output when this is the case. To build it as a release library instead, add
  94. `-DCMAKE_BUILD_TYPE=Release` when generating the build system files, as shown
  95. above. The use of `--config Release` in build commands is needed to properly
  96. support multi-configuration tools (like Visual Studio for example) and can be
  97. skipped for other build systems (like Makefile).
  98. To enable link-time optimisation, also add `-DBENCHMARK_ENABLE_LTO=true` when
  99. generating the build system files.
  100. If you are using gcc, you might need to set `GCC_AR` and `GCC_RANLIB` cmake
  101. cache variables, if autodetection fails.
  102. If you are using clang, you may need to set `LLVMAR_EXECUTABLE`,
  103. `LLVMNM_EXECUTABLE` and `LLVMRANLIB_EXECUTABLE` cmake cache variables.
  104. To enable sanitizer checks (eg., `asan` and `tsan`), add:
  105. ```
  106. -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all"
  107. -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all "
  108. ```
  109. ### Stable and Experimental Library Versions
  110. The main branch contains the latest stable version of the benchmarking library;
  111. the API of which can be considered largely stable, with source breaking changes
  112. being made only upon the release of a new major version.
  113. Newer, experimental, features are implemented and tested on the
  114. [`v2` branch](https://github.com/google/benchmark/tree/v2). Users who wish
  115. to use, test, and provide feedback on the new features are encouraged to try
  116. this branch. However, this branch provides no stability guarantees and reserves
  117. the right to change and break the API at any time.
  118. ## Usage
  119. ### Basic usage
  120. Define a function that executes the code to measure, register it as a benchmark
  121. function using the `BENCHMARK` macro, and ensure an appropriate `main` function
  122. is available:
  123. ```c++
  124. #include <benchmark/benchmark.h>
  125. static void BM_StringCreation(benchmark::State& state) {
  126. for (auto _ : state)
  127. std::string empty_string;
  128. }
  129. // Register the function as a benchmark
  130. BENCHMARK(BM_StringCreation);
  131. // Define another benchmark
  132. static void BM_StringCopy(benchmark::State& state) {
  133. std::string x = "hello";
  134. for (auto _ : state)
  135. std::string copy(x);
  136. }
  137. BENCHMARK(BM_StringCopy);
  138. BENCHMARK_MAIN();
  139. ```
  140. To run the benchmark, compile and link against the `benchmark` library
  141. (libbenchmark.a/.so). If you followed the build steps above, this library will
  142. be under the build directory you created.
  143. ```bash
  144. # Example on linux after running the build steps above. Assumes the
  145. # `benchmark` and `build` directories are under the current directory.
  146. $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \
  147. -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark
  148. ```
  149. Alternatively, link against the `benchmark_main` library and remove
  150. `BENCHMARK_MAIN();` above to get the same behavior.
  151. The compiled executable will run all benchmarks by default. Pass the `--help`
  152. flag for option information or see the [User Guide](docs/user_guide.md).
  153. ### Usage with CMake
  154. If using CMake, it is recommended to link against the project-provided
  155. `benchmark::benchmark` and `benchmark::benchmark_main` targets using
  156. `target_link_libraries`.
  157. It is possible to use ```find_package``` to import an installed version of the
  158. library.
  159. ```cmake
  160. find_package(benchmark REQUIRED)
  161. ```
  162. Alternatively, ```add_subdirectory``` will incorporate the library directly in
  163. to one's CMake project.
  164. ```cmake
  165. add_subdirectory(benchmark)
  166. ```
  167. Either way, link to the library as follows.
  168. ```cmake
  169. target_link_libraries(MyTarget benchmark::benchmark)
  170. ```