diff options
author | Christopher Haster <geky@geky.net> | 2022-11-30 20:23:04 +0300 |
---|---|---|
committer | Christopher Haster <geky@geky.net> | 2022-12-07 08:09:07 +0300 |
commit | b0382fa891722dea8339fead2f614d5a7b745929 (patch) | |
tree | 002af46d66a3c10e36311a048d9453a6bb48eb83 /benches | |
parent | d8e7ffb7fde46ac61d37652e01d33db8c4ab9002 (diff) |
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.
Diffstat (limited to 'benches')
-rw-r--r-- | benches/bench_dir.toml | 34 | ||||
-rw-r--r-- | benches/bench_file.toml | 24 |
2 files changed, 15 insertions, 43 deletions
diff --git a/benches/bench_dir.toml b/benches/bench_dir.toml index 937f70d..5f8cb49 100644 --- a/benches/bench_dir.toml +++ b/benches/bench_dir.toml @@ -1,17 +1,3 @@ - - -# deterministic prng -code = ''' -static uint32_t xorshift32(uint32_t *state) { - uint32_t x = *state; - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - *state = x; - return x; -} -''' - [cases.bench_dir_open] # 0 = in-order # 1 = reversed-order @@ -37,7 +23,7 @@ code = ''' uint32_t file_prng = i; for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) { for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) { - buffer[k] = xorshift32(&file_prng); + buffer[k] = BENCH_PRNG(&file_prng); } lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; } @@ -52,7 +38,7 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (N-1-i) - : xorshift32(&prng) % N; + : BENCH_PRNG(&prng) % N; sprintf(name, "file%08x", i_); lfs_file_t file; lfs_file_open(&lfs, &file, name, LFS_O_RDONLY) => 0; @@ -61,7 +47,7 @@ code = ''' for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) { lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) { - assert(buffer[k] == xorshift32(&file_prng)); + assert(buffer[k] == BENCH_PRNG(&file_prng)); } } @@ -93,7 +79,7 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (N-1-i) - : xorshift32(&prng) % N; + : BENCH_PRNG(&prng) % N; sprintf(name, "file%08x", i_); lfs_file_t file; lfs_file_open(&lfs, &file, name, @@ -102,7 +88,7 @@ code = ''' uint32_t file_prng = i_; for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) { for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) { - buffer[k] = xorshift32(&file_prng); + buffer[k] = BENCH_PRNG(&file_prng); } lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; } @@ -139,7 +125,7 @@ code = ''' uint32_t file_prng = i; for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) { for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) { - buffer[k] = xorshift32(&file_prng); + buffer[k] = BENCH_PRNG(&file_prng); } lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; } @@ -154,7 +140,7 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (N-1-i) - : xorshift32(&prng) % N; + : BENCH_PRNG(&prng) % N; sprintf(name, "file%08x", i_); int err = lfs_remove(&lfs, name); assert(!err || err == LFS_ERR_NOENT); @@ -185,7 +171,7 @@ code = ''' uint32_t file_prng = i; for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) { for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) { - buffer[k] = xorshift32(&file_prng); + buffer[k] = BENCH_PRNG(&file_prng); } lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; } @@ -235,7 +221,7 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (N-1-i) - : xorshift32(&prng) % N; + : BENCH_PRNG(&prng) % N; printf("hm %d\n", i); sprintf(name, "dir%08x", i_); int err = lfs_mkdir(&lfs, name); @@ -271,7 +257,7 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (N-1-i) - : xorshift32(&prng) % N; + : BENCH_PRNG(&prng) % N; sprintf(name, "dir%08x", i_); int err = lfs_remove(&lfs, name); assert(!err || err == LFS_ERR_NOENT); diff --git a/benches/bench_file.toml b/benches/bench_file.toml index 7e55642..168eaad 100644 --- a/benches/bench_file.toml +++ b/benches/bench_file.toml @@ -1,17 +1,3 @@ - - -# deterministic prng -code = ''' -static uint32_t xorshift32(uint32_t *state) { - uint32_t x = *state; - x ^= x << 13; - x ^= x >> 17; - x ^= x << 5; - *state = x; - return x; -} -''' - [cases.bench_file_read] # 0 = in-order # 1 = reversed-order @@ -33,7 +19,7 @@ code = ''' for (lfs_size_t i = 0; i < chunks; i++) { uint32_t chunk_prng = i; for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) { - buffer[j] = xorshift32(&chunk_prng); + buffer[j] = BENCH_PRNG(&chunk_prng); } lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; @@ -50,14 +36,14 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (chunks-1-i) - : xorshift32(&prng) % chunks; + : BENCH_PRNG(&prng) % chunks; lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET) => i_*CHUNK_SIZE; lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE; uint32_t chunk_prng = i_; for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) { - assert(buffer[j] == xorshift32(&chunk_prng)); + assert(buffer[j] == BENCH_PRNG(&chunk_prng)); } } @@ -91,10 +77,10 @@ code = ''' lfs_off_t i_ = (ORDER == 0) ? i : (ORDER == 1) ? (chunks-1-i) - : xorshift32(&prng) % chunks; + : BENCH_PRNG(&prng) % chunks; uint32_t chunk_prng = i_; for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) { - buffer[j] = xorshift32(&chunk_prng); + buffer[j] = BENCH_PRNG(&chunk_prng); } lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET) |