Previous: , Up: Basic Usage   [Contents][Index]


1.4.6 Test Dimensions

Usually, we want to provide different inputs to the test code in order to compare its behavior. To this end, MicroBenchmark allows to control the sizes provided to the test (see Test Constraints Reference), which can be used to produce tailored input.

The following examples show how to control the input of the test code:

#include <mbenchmark/all.h>
#include <unistd.h>

static void
one_dimension (micro_benchmark_test_case test)
{
  /* This test will have three dimensions, but four different values
     on them, so only one execution will be performed.  */
  const size_t sizes[] = { 1, 2, 3, 4 };
  const size_t ssizes = sizeof (sizes) / sizeof (*sizes);
  micro_benchmark_test_case_add_dimension (test, ssizes, sizes);
}

static size_t *
set_up_1d (micro_benchmark_test_state state)
{
  /* Prepare the data */
  static size_t s;
  s = micro_benchmark_state_get_size (state, 0) * 100000;
  return &s;
}

static void
test_1d (size_t *sz)
{
  /* test code */
  usleep (*sz);
}

MICRO_BENCHMARK_REGISTER_AUTO_TEST (set_up_1d, test_1d, 0);
MICRO_BENCHMARK_CONSTRAINT_TEST ("test_1d", one_dimension);

static void
three_dimensions (micro_benchmark_test_case test)
{
  /* This test will have three dimensions, but no variation on them,
     so only one execution will be performed.  */
  const size_t sizes[] = { 1, 2, 3 };
  micro_benchmark_test_case_add_dimension (test, 1, sizes + 0);
  micro_benchmark_test_case_add_dimension (test, 1, sizes + 1);
  micro_benchmark_test_case_add_dimension (test, 1, sizes + 2);
}

static size_t *
set_up_3d (micro_benchmark_test_state state)
{
  /* Prepare the data */
  static size_t s[3];
  s[0] = micro_benchmark_state_get_size (state, 0) * 10000;
  s[1] = micro_benchmark_state_get_size (state, 1) * 100000;
  s[2] = micro_benchmark_state_get_size (state, 2) * 1000000;
  return s;
}

static void
test_3d (size_t *sz)
{
  /* test code */
  usleep (sz[0] + sz[1] + sz[2]);
}

MICRO_BENCHMARK_REGISTER_AUTO_TEST (set_up_3d, test_3d, 0);
MICRO_BENCHMARK_CONSTRAINT_TEST ("test_3d", three_dimensions);

MICRO_BENCHMARK_MAIN ();
#include <mbenchmark/all.hpp>
#include <chrono>
#include <thread>

namespace
{
  using micro_benchmark::with_constraints;

  void
  one_dimension (micro_benchmark::test_case& test)
  {
    test.add_dimension ({ 1, 2, 3 });
  }

  void
  test_1d (std::vector<std::size_t> const& v)
  {
    std::this_thread::sleep_for (std::chrono::milliseconds (v[0] * 100));
  }

  auto t1d = micro_benchmark::register_test (with_constraints, "test1d",
                                             one_dimension, test_1d);

  void
  three_dimensions (micro_benchmark::test_case& test)
  {
    test.add_dimension ({ 1 });
    test.add_dimension ({ 10 });
    test.add_dimension ({ 100 });
  }

  void
  test_3d (std::vector<std::size_t> const& v)
  {
    std::this_thread::sleep_for (std::chrono::milliseconds (v[0] + v[1] + v[2]));
  }

  auto t3d = micro_benchmark::register_test (with_constraints, "test3d",
                                             three_dimensions, test_3d);

}

MICRO_BENCHMARK_MAIN ();

(define (test1d n)
  (usleep (* n 100000)))

(register-test! "test1d" #:test test1d
                #:dimensions '((1 2 3)))

(define (test3d x y z)
  (usleep (* 100000 (+ x y z))))

(register-test! "test3d" #:test test3d
                #:dimensions '((1) (2) (3)))

(main (command-line))

This could be their output:

dimensions --brief --log-level=warn
Suite: dimensions (5 test executions)
========================================
    Test Name | Iterations | It.Time (μ)
========================================
      test_1d |         -- |          --
    test_1d/1 |         50 |       100ms
    test_1d/2 |         25 |       200ms
    test_1d/3 |         17 |       300ms
    test_1d/4 |         13 |       400ms
----------------------------------------
test_3d/1/2/3 |          2 |       3.21s
========================================

Previous: Test Constraints, Up: Basic Usage   [Contents][Index]