Added BENCH/TEST_PRNG, replacing other ad-hoc sources of randomness

When you add a function to every benchmark suite, you know if should
probably be provided by the benchmark runner itself. That being said,
randomness in tests/benchmarks is a bit tricky because it needs to be
strictly controlled and reproducible.

No global state is used, allowing tests/benches to maintain multiple
randomness stream which can be useful for checking results during a run.

There's an argument for having global prng state in that the prng could
be preserved across power-loss, but I have yet to see a use for this,
and it would add a significant requirement to any future test/bench runner.
This commit is contained in:
Christopher Haster
2022-11-30 11:23:04 -06:00
parent d8e7ffb7fd
commit b0382fa891
10 changed files with 134 additions and 124 deletions
+13
View File
@@ -529,6 +529,19 @@ void bench_trace(const char *fmt, ...) {
}
// bench prng
uint32_t bench_prng(uint32_t *state) {
// A simple xorshift32 generator, easily reproducible. Keep in mind
// determinism is much more important than actual randomness here.
uint32_t x = *state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*state = x;
return x;
}
// bench recording state
static struct lfs_config *bench_cfg = NULL;
static lfs_emubd_io_t bench_last_readed = 0;
+6
View File
@@ -74,6 +74,12 @@ struct bench_suite {
};
// deterministic prng for pseudo-randomness in benches
uint32_t bench_prng(uint32_t *state);
#define BENCH_PRNG(state) bench_prng(state)
// access generated bench defines
intmax_t bench_define(size_t define);
+13
View File
@@ -545,6 +545,19 @@ void test_trace(const char *fmt, ...) {
}
// test prng
uint32_t test_prng(uint32_t *state) {
// A simple xorshift32 generator, easily reproducible. Keep in mind
// determinism is much more important than actual randomness here.
uint32_t x = *state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*state = x;
return x;
}
// encode our permutation into a reusable id
static void perm_printid(
const struct test_suite *suite,
+6
View File
@@ -67,6 +67,12 @@ struct test_suite {
};
// deterministic prng for pseudo-randomness in testes
uint32_t test_prng(uint32_t *state);
#define TEST_PRNG(state) test_prng(state)
// access generated test defines
intmax_t test_define(size_t define);