### Basic Benchmark Example Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/README.md This C++ code demonstrates how to define and register a simple benchmark function using the Google Benchmark library. It includes setup, the timed code section, and registration. ```c++ #include static void BM_SomeFunction(benchmark::State& state) { // Perform setup here for (auto _ : state) { // This code gets timed SomeFunction(); } } // Register the function as a benchmark BENCHMARK(BM_SomeFunction); // Run the benchmark BENCHMARK_MAIN(); ``` -------------------------------- ### Constructor/Destructor vs. SetUp()/TearDown() Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/faq.md Illustrates the difference between using test fixture constructors/destructors and SetUp()/TearDown() methods for test initialization and cleanup. ```c++ class MyTest : public ::testing::Test { protected: virtual void SetUp() override { // Code here will be called immediately after the constructor (right // before each test). } virtual void TearDown() override { // Code here will be called immediately after each test (right // before the destructor). } }; TEST_F(MyTest, DoesSomething) { // ... test body ... } ``` -------------------------------- ### Environment Setup and Teardown Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/reference/testing.md These methods are intended to be overridden to define custom setup and teardown logic for the testing environment. ```APIDOC ## Environment::SetUp `virtual void Environment::SetUp()` Override this to define how to set up the environment. ## Environment::TearDown `virtual void Environment::TearDown()` Override this to define how to tear down the environment. ``` -------------------------------- ### Console Output Format Example Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/user_guide.md Example of the default console output format for benchmarks, showing performance metrics. ```text Benchmark Time(ns) CPU(ns) Iterations ---------------------------------------------------------------------- BM_SetInsert/1024/1 28928 29349 23853 133.097kiB/s 33.2742k items/s BM_SetInsert/1024/8 32065 32913 21375 949.487kiB/s 237.372k items/s BM_SetInsert/1024/10 33157 33648 21431 1.13369MiB/s 290.225k items/s ``` -------------------------------- ### Multithreaded Benchmark Setup Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/user_guide.md Use `state.thread_index() == 0` to perform setup and teardown only once in multithreaded benchmarks. This ensures that global resources are initialized and cleaned up correctly. ```c++ static void BM_MultiThreaded(benchmark::State& state) { if (state.thread_index() == 0) { // Setup code here. } for (auto _ : state) { // Run the test as normal. } if (state.thread_index() == 0) { // Teardown code here. } } BENCHMARK(BM_MultiThreaded)->Threads(2); ``` -------------------------------- ### Install Benchmark Dependencies Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/tools.md Install necessary dependencies for benchmark tools using pip. ```bash pip3 install -r requirements.txt ``` -------------------------------- ### Install Targets and Directories Source: https://github.com/google/boringssl/blob/main/CMakeLists.txt Configures installation rules for BoringSSL targets and headers. It installs the 'crypto' and 'ssl' targets, the 'bssl' target, and the 'include' directory. It also sets up CMake export files for package management. ```cmake if(INSTALL_ENABLED) install(TARGETS crypto ssl EXPORT OpenSSLTargets) install(TARGETS bssl) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT OpenSSLTargets FILE OpenSSLTargets.cmake NAMESPACE OpenSSL:: DESTINATION lib/cmake/OpenSSL) install(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL) endif() ``` -------------------------------- ### Compile and Install GoogleTest Source: https://github.com/google/boringssl/blob/main/third_party/googletest/googletest/README.md After generating build scripts with CMake, use 'make' to compile GoogleTest. If you have administrative privileges, 'sudo make install' can install it system-wide. ```bash make sudo make install # Install in /usr/local/ by default ``` -------------------------------- ### Benchmark Setup and Teardown Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/user_guide.md Define custom setup and teardown functions to be executed before and after each benchmark run. These callbacks are invoked once per benchmark, even for multi-threaded benchmarks, before each thread count iteration. ```c++ static void DoSetup(const benchmark::State& state) { } static void DoTeardown(const benchmark::State& state) { } static void BM_func(benchmark::State& state) {...} BENCHMARK(BM_func)->Arg(1)->Arg(3)->Threads(16)->Threads(32)->Setup(DoSetup)->Teardown(DoTeardown); ``` -------------------------------- ### Test Fixture Setup and Teardown Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/reference/testing.md Methods to be overridden for performing setup and teardown operations before and after each individual test case within a fixture. ```APIDOC ## Test::SetUp `virtual void Test::SetUp()` Override this to perform test fixture setup. GoogleTest calls `SetUp()` before running each individual test. ## Test::TearDown `virtual void Test::TearDown()` Override this to perform test fixture teardown. GoogleTest calls `TearDown()` after running each individual test. ``` -------------------------------- ### Install Project Targets Source: https://github.com/google/boringssl/blob/main/third_party/googletest/googlemock/CMakeLists.txt Installs the gmock and gmock_main targets as part of the project installation rules. ```cmake install_project(gmock gmock_main) ``` -------------------------------- ### JSON Output Format Example Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/user_guide.md Example of the JSON output format for benchmarks, including context and benchmark results. ```json { "context": { "date": "2015/03/17-18:40:25", "num_cpus": 40, "mhz_per_cpu": 2801, "cpu_scaling_enabled": false, "build_type": "debug" }, "benchmarks": [ { "name": "BM_SetInsert/1024/1", "iterations": 94877, "real_time": 29275, "cpu_time": 29836, "bytes_per_second": 134066, "items_per_second": 33516 }, { "name": "BM_SetInsert/1024/8", "iterations": 21609, "real_time": 32317, "cpu_time": 32429, "bytes_per_second": 986770, "items_per_second": 246693 }, { "name": "BM_SetInsert/1024/10", "iterations": 21393, "real_time": 32724, "cpu_time": 33355, "bytes_per_second": 1199226, "items_per_second": 299807 } ] } ``` -------------------------------- ### Example pkg-config file for GoogleTest Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/pkgconfig.md This is an example of a pkg-config file generated for GoogleTest. It specifies library directories, include directories, and necessary flags for linking and compilation. ```text libdir=/usr/lib64 includedir=/usr/include Name: gtest Description: GoogleTest (without main() function) Version: 1.11.0 URL: https://github.com/google/googletest Libs: -L${libdir} -lgtest -lpthread Cflags: -I${includedir} -DGTEST_HAS_PTHREAD=1 -lpthread ``` -------------------------------- ### Project Minimum Requirements and Setup Source: https://github.com/google/boringssl/blob/main/third_party/googletest/googlemock/CMakeLists.txt Sets the minimum CMake version and defines the project name, version, and languages. Also includes hermetic build setup if the command is available. ```cmake cmake_minimum_required(VERSION 3.13) project(gmock VERSION ${GOOGLETEST_VERSION} LANGUAGES CXX C) if (COMMAND set_up_hermetic_build) set_up_hermetic_build() endif() ``` -------------------------------- ### Enable Testing and Google Test Setup Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/CMakeLists.txt Configures testing, enabling it if BENCHMARK_ENABLE_TESTING is set. It also handles the setup for Google Test, either by including it directly or finding it via configuration. ```cmake if (BENCHMARK_ENABLE_TESTING) enable_testing() if (BENCHMARK_ENABLE_GTEST_TESTS AND NOT (TARGET gtest AND TARGET gtest_main AND TARGET gmock AND TARGET gmock_main)) if (BENCHMARK_USE_BUNDLED_GTEST) include(GoogleTest) else() find_package(GTest CONFIG REQUIRED) add_library(gtest ALIAS GTest::gtest) add_library(gtest_main ALIAS GTest::gtest_main) add_library(gmock ALIAS GTest::gmock) add_library(gmock_main ALIAS GTest::gmock_main) endif() endif() add_subdirectory(test) endif() ``` -------------------------------- ### Install Google Benchmark Python Bindings Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/python_bindings.md Installs the latest version of Google Benchmark's Python bindings from PyPI. It's recommended to upgrade pip first for manylinux2014 support. Consider using a virtual environment. ```bash python -m pip install --upgrade pip # for manylinux2014 support python -m pip install google-benchmark ``` -------------------------------- ### Example of Programmatic Test Registration Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/advanced.md This example demonstrates how to define a test fixture and register custom tests dynamically using a lambda function as a factory. Ensure the factory returns the correct fixture type. ```c++ class MyFixture : public testing::Test { public: // All of these optional, just like in regular macro usage. static void SetUpTestSuite() { ... } static void TearDownTestSuite() { ... } void SetUp() override { ... } void TearDown() override { ... } }; class MyTest : public MyFixture { public: explicit MyTest(int data) : data_(data) {} void TestBody() override { ... } private: int data_; }; void RegisterMyTests(const std::vector& values) { for (int v : values) { testing::RegisterTest( "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, std::to_string(v).c_str(), __FILE__, __LINE__, // Important to use the fixture type as the return type here. [=]() -> MyFixture* { return new MyTest(v); }); } } ... int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); std::vector values_to_test = LoadValuesFromConfig(); RegisterMyTests(values_to_test); ... return RUN_ALL_TESTS(); } ``` -------------------------------- ### Create Project Directory Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/quickstart-cmake.md Initializes a new project directory and navigates into it. ```bash $ mkdir my_project && cd my_project ``` -------------------------------- ### Set Include Directories for gtest_main Source: https://github.com/google/boringssl/blob/main/third_party/googletest/googletest/CMakeLists.txt Configures system include directories for the gtest_main library, mirroring the setup for gtest to ensure consistent include paths for both build and installation. ```cmake target_include_directories(gtest_main SYSTEM INTERFACE "$" "$/${CMAKE_INSTALL_INCLUDEDIR}>") ``` -------------------------------- ### Install Benchmark using CMake Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/README.md These bash commands show how to clone the benchmark library, configure it with CMake, and build it. It covers downloading dependencies and setting the build type to Release. ```bash # Check out the library. $git clone https://github.com/google/benchmark.git # Go to the library root directory $cd benchmark # Make a build directory to place the build output. $cmake -E make_directory "build" # Generate build system files with cmake, and download any dependencies. $cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ # or, starting with CMake 3.13, use a simpler form: # cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release -S . -B "build" # Build the library. $cmake --build "build" --config Release ``` -------------------------------- ### Build Google Benchmark Python Bindings from Source Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/python_bindings.md Clones the Google Benchmark repository, sets up a virtual environment, and builds a wheel file from the source code. This process requires Bazel to be installed. ```bash git clone https://github.com/google/benchmark.git cd benchmark # create a virtual environment and activate it python3 -m venv venv --system-site-packages source venv/bin/activate # .\venv\Scripts\Activate.ps1 on Windows # upgrade Python's system-wide packages python -m pip install --upgrade pip build # builds the wheel and stores it in the directory "dist". python -m build ``` -------------------------------- ### Link Shlwapi and Benchmark Libraries with Visual Studio Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/platform_specific_build_instructions.md Include Shlwapi.lib for CPUInfo registry access and benchmark.lib for the benchmark library. Ensure BENCHMARK_STATIC_DEFINE is set when using the static library. ```cpp #ifdef _WIN32 #pragma comment ( lib, "Shlwapi.lib" ) #ifdef _DEBUG #pragma comment ( lib, "benchmark.lib" ) #else #pragma comment ( lib, "benchmark.lib" ) #endif #endif ``` -------------------------------- ### CSV Output Format Example Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/docs/user_guide.md Example of the CSV output format for benchmarks, providing data in a comma-separated value format. ```csv name,iterations,real_time,cpu_time,bytes_per_second,items_per_second,label "BM_SetInsert/1024/1",65465,17890.7,8407.45,475768,118942, "BM_SetInsert/1024/8",116606,18810.1,9766.64,3.27646e+06,819115, "BM_SetInsert/1024/10",106365,17238.4,8421.53,4.74973e+06,1.18743e+06, ``` -------------------------------- ### Example Test Definitions Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/advanced.md These are example C++ test definitions that would be processed by GoogleTest. They illustrate how tests are declared using the TEST macro. ```c++ TEST(MathTest, Addition) { ... } TEST(MathTest, Subtraction) { ... } TEST(LogicTest, NonContradiction) { ... } ``` -------------------------------- ### Install GoogleTest Project Source: https://github.com/google/boringssl/blob/main/third_party/googletest/googletest/CMakeLists.txt Installs the GoogleTest project, including the gtest and gtest_main libraries. This command makes the built libraries available for use in other projects. ```cmake install_project(gtest gtest_main) ``` -------------------------------- ### Compile and Link Benchmark Executable Source: https://github.com/google/boringssl/blob/main/third_party/benchmark/README.md Example compilation command for a benchmark C++ file, linking against the benchmark library and pthread. ```bash # Example on linux after running the build steps above. Assumes the # `benchmark` and `build` directories are under the current directory. $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \ -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark ``` -------------------------------- ### Install Git Commit Hook for Gerrit Source: https://github.com/google/boringssl/blob/main/CONTRIBUTING.md Installs a Git commit hook to automatically add Change-Ids to commits, which is required for uploading changes to Gerrit. ```bash curl -Lo .git/hooks/commit-msg https://boringssl-review.googlesource.com/tools/hooks/commit-msg chmod u+x .git/hooks/commit-msg ``` -------------------------------- ### Example Generated XML Report Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/advanced.md This is an example of a detailed XML report generated by GoogleTest for a set of tests. It includes test counts, durations, and failure information. ```xml ... ... ``` -------------------------------- ### Skip Test Execution Source: https://github.com/google/boringssl/blob/main/third_party/googletest/docs/reference/testing.md Prevents further test execution at runtime. Can be used in test cases, fixture SetUp(), or global environment SetUp() methods to skip tests conditionally. ```cpp GTEST_SKIP(); ``` -------------------------------- ### Android Debugging Setup Source: https://github.com/google/boringssl/blob/main/BUILDING.md Commands to set up debugging for Android-built binaries using gdbserver. Replace ARCH with the target device architecture (e.g., arm, arm64). ```bash adb push ${ANDROID_NDK}/prebuilt/android-ARCH/gdbserver/gdbserver \ /data/local/tmp adb forward tcp:5039 tcp:5039 adb shell /data/local/tmp/gdbserver :5039 /path/on/device/to/binary ```