From f45d35c9804be1a53360fed1f8fcb8d3432ce8ea Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Sat, 5 Aug 2017 11:12:29 +0100 Subject: [PATCH] Added benchmark tests --- CMakeLists.txt | 3 +- .../Baselines/console.std.approved.txt | 20 +++++- .../Baselines/console.sw.approved.txt | 65 ++++++++++++++++++- .../SelfTest/Baselines/junit.sw.approved.txt | 3 +- .../SelfTest/Baselines/xml.sw.approved.txt | 39 ++++++++++- projects/SelfTest/Benchmark.tests.cpp | 43 ++++++++++++ 6 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 projects/SelfTest/Benchmark.tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 37111da0..211bfd9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ endfunction() set(TEST_SOURCES ${SELF_TEST_DIR}/ApproxTests.cpp ${SELF_TEST_DIR}/BDDTests.cpp + ${SELF_TEST_DIR}/Benchmark.tests.cpp ${SELF_TEST_DIR}/ClassTests.cpp ${SELF_TEST_DIR}/CmdLineTests.cpp ${SELF_TEST_DIR}/CompilationTests.cpp @@ -96,9 +97,9 @@ set(SURROGATE_SOURCES ${SELF_TEST_DIR}/SurrogateCpps/catch_option.cpp ${SELF_TEST_DIR}/SurrogateCpps/catch_stream.cpp ${SELF_TEST_DIR}/SurrogateCpps/catch_streambuf.cpp + ${SELF_TEST_DIR}/SurrogateCpps/catch_test_case_tracker.cpp ${SELF_TEST_DIR}/SurrogateCpps/catch_test_spec.cpp ${SELF_TEST_DIR}/SurrogateCpps/catch_xmlwriter.cpp - ${SELF_TEST_DIR}/SurrogateCpps/catch_test_case_tracker.cpp ) CheckFileList(SURROGATE_SOURCES ${SELF_TEST_DIR}/SurrogateCpps) diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt index 9e41f7f5..acd58b94 100644 --- a/projects/SelfTest/Baselines/console.std.approved.txt +++ b/projects/SelfTest/Baselines/console.std.approved.txt @@ -850,6 +850,22 @@ warning: Uncomment the code in this test to check that it gives a sensible compiler error +------------------------------------------------------------------------------- +benchmarked +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + ++-----------------------------------------------------------------------------+ +| benchmark name | iters | elapsed ns | average | ++-----------------------------------------------------------------------------+ + +| Load up a vector | 1 | 8459 | 8459 ns | +| Load up a map | 1 | 28774 | 28774 ns | +| Reserved vector | 1 | 2430 | 2430 ns | +| A fixed size array that should require | | | | +| no allocations | 10 | 2563 | 256 ns | ++-----------------------------------------------------------------------------+ ------------------------------------------------------------------------------- checkedElse, failing ------------------------------------------------------------------------------- @@ -1016,6 +1032,6 @@ with expansion: "{?}" == "1" =============================================================================== -test cases: 182 | 131 passed | 47 failed | 4 failed as expected -assertions: 896 | 779 passed | 96 failed | 21 failed as expected +test cases: 183 | 132 passed | 47 failed | 4 failed as expected +assertions: 900 | 783 passed | 96 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index 971c0e32..7f4861d4 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -6381,6 +6381,67 @@ PASSED: with expansion: 0 == 0 +------------------------------------------------------------------------------- +benchmarked +------------------------------------------------------------------------------- +String.tests.cpp: +............................................................................... + ++-----------------------------------------------------------------------------+ +| benchmark name | iters | elapsed ns | average | ++-----------------------------------------------------------------------------+ + +| Load up a vector | 1 | 6957 | 6957 ns | ++-----------------------------------------------------------------------------+ +String.tests.cpp:: +PASSED: + REQUIRE( v.size() == size ) +with expansion: + 100 == 100 + + ++-----------------------------------------------------------------------------+ + +| benchmark name | iters | elapsed ns | average | ++-----------------------------------------------------------------------------+ + +| Load up a map | 1 | 27868 | 27868 ns | ++-----------------------------------------------------------------------------+ +String.tests.cpp:: +PASSED: + REQUIRE( m.size() == size ) +with expansion: + 100 == 100 + + ++-----------------------------------------------------------------------------+ + +| benchmark name | iters | elapsed ns | average | ++-----------------------------------------------------------------------------+ + +| Reserved vector | 1 | 2711 | 2711 ns | ++-----------------------------------------------------------------------------+ +String.tests.cpp:: +PASSED: + REQUIRE( v.size() == size ) +with expansion: + 100 == 100 + + ++-----------------------------------------------------------------------------+ + +| benchmark name | iters | elapsed ns | average | ++-----------------------------------------------------------------------------+ + +| A fixed size array that should require | | | | +| no allocations | 1 | 530 | 530 ns | ++-----------------------------------------------------------------------------+ +String.tests.cpp:: +PASSED: + REQUIRE( sum > size ) +with expansion: + 4950 (0x) > 100 + ------------------------------------------------------------------------------- boolean member ------------------------------------------------------------------------------- @@ -7590,6 +7651,6 @@ MiscTests.cpp:: PASSED: =============================================================================== -test cases: 182 | 130 passed | 48 failed | 4 failed as expected -assertions: 898 | 779 passed | 98 failed | 21 failed as expected +test cases: 183 | 131 passed | 48 failed | 4 failed as expected +assertions: 902 | 783 passed | 98 failed | 21 failed as expected diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index 58807c18..31ad3c5a 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -1,6 +1,6 @@ - + @@ -641,6 +641,7 @@ ExceptionTests.cpp: + diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index e70ad12e..01ec013c 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -7201,6 +7201,41 @@ Message from section two + + + + v.size() == size + + + 100 == 100 + + + + + m.size() == size + + + 100 == 100 + + + + + v.size() == size + + + 100 == 100 + + + + + sum > size + + + 4950 (0x) > 100 + + + + @@ -8441,7 +8476,7 @@ spanner - + - + diff --git a/projects/SelfTest/Benchmark.tests.cpp b/projects/SelfTest/Benchmark.tests.cpp new file mode 100644 index 00000000..ff1ec479 --- /dev/null +++ b/projects/SelfTest/Benchmark.tests.cpp @@ -0,0 +1,43 @@ +#include "catch.hpp" + +#include + +TEST_CASE( "benchmarked", "[.][benchmark]" ) { + + static const int size = 100; + + std::vector v; + std::map m; + + BENCHMARK( "Load up a vector" ) { + v = std::vector(); + for(int i =0; i < size; ++i ) + v.push_back( i ); + } + REQUIRE( v.size() == size ); + + BENCHMARK( "Load up a map" ) { + m = std::map(); + for(int i =0; i < size; ++i ) + m.insert( { i, i+1 } ); + } + REQUIRE( m.size() == size ); + + BENCHMARK( "Reserved vector" ) { + v = std::vector(); + v.reserve(size); + for(int i =0; i < size; ++i ) + v.push_back( i ); + } + REQUIRE( v.size() == size ); + + int array[size]; + BENCHMARK( "A fixed size array that should require no allocations" ) { + for(int i =0; i < size; ++i ) + array[i] = i; + } + int sum = 0; + for(int i =0; i < size; ++i ) + sum += array[i]; + REQUIRE( sum > size ); +}