Next: Test Dimensions, Previous: Directed Test Cases, Up: Basic Usage [Contents][Index]
The examples shown on the previous section use the library defaults, which could not be appropriate for your specific use case. MicroBenchmark includes several options to customize the environment where the test is executed (see Test Constraints Reference.)
The following examples show the basic constraint usage on the implemented languages:
#include <mbenchmark/all.h> #include <unistd.h> static void limit_iterations (micro_benchmark_test_case test) { micro_benchmark_test_case_limit_iterations (test, 300, 400); micro_benchmark_test_case_limit_samples (test, 2, 5); } static void test_1 (void *ptr) { (void) ptr; /* This test code will run for 300 to 400 iterations, taking measurements each 2 to 5 iterations. */ usleep (10000); } MICRO_BENCHMARK_REGISTER_SIMPLE_TEST (test_1); MICRO_BENCHMARK_CONSTRAINT_TEST ("test_1", limit_iterations); static void limit_time (micro_benchmark_test_case test) { micro_benchmark_clock_time max = { 1, 0 }; micro_benchmark_test_case_set_max_time (test, max); } static void test_2 (void *ptr) { (void) ptr; /* This test code will run for 1 second. */ usleep (10000); } MICRO_BENCHMARK_REGISTER_SIMPLE_TEST (test_2); MICRO_BENCHMARK_CONSTRAINT_TEST ("test_2", limit_time); MICRO_BENCHMARK_MAIN ();
#include <mbenchmark/all.hpp> #include <chrono> #include <thread> namespace { using micro_benchmark::with_constraints; void test_1 (std::vector<std::size_t> const&) { std::this_thread::sleep_for (std::chrono::milliseconds(10)); } void limit_iterations (micro_benchmark::test_case& test) { test.limit_iterations (300, 400); test.limit_samples (2, 5); } auto rt1 = micro_benchmark::register_test (with_constraints, "test_1", limit_iterations, test_1); void test_2 (std::vector<std::size_t> const&) { std::this_thread::sleep_for (std::chrono::milliseconds(10)); } void limit_time (micro_benchmark::test_case& test) { test.max_time (std::chrono::seconds (1)); } auto rt2 = micro_benchmark::register_test (with_constraints, "test_2", limit_time, test_2); } MICRO_BENCHMARK_MAIN ();
(define (test-1) (usleep 10000)) (register-test! "test-1" #:test test-1 ;; This test will run for 300 to 400 iterations... #:min-iterations 300 #:max-iterations 400 ;; ... taking measurements each 2 to 5 iterations. #:min-sample-iterations 2 #:max-sample-iterations 5) (register-test! "test-2" #:test test-1 ;; This test will run for 1 second. #:max-time 1) (main (command-line))
Their output could look like this:
constraints --brief --log-level=warn
⇒
Suite: constraints (2 test executions) ==================================== Test Name | Iterations | It.Time (μ) ==================================== test_1 | 400 | 10.3ms test_2 | 97 | 10.3ms ====================================
Next: Test Dimensions, Previous: Directed Test Cases, Up: Basic Usage [Contents][Index]