727697d3fba1ec9bb6afc09f5abd93842ee1fddf
[bertos.git] / bertos / sec / benchmarks.c
1 #include "benchmarks.h"
2 #include <sec/hash.h>
3 #include <drv/timer.h>
4 #include <string.h>
5
6 static uint8_t buf[512];
7
8 void hash_benchmark(Hash *h, const char *hname, int numk)
9 {
10         memset(buf, 0x12, sizeof(buf));
11         ticks_t t = timer_clock();
12
13         for (int j=0;j<64;++j) {
14                 hash_begin(h);
15                 for (int i=0; i<numk*2; ++i)
16                         hash_update(h, buf, 512);
17                 hash_final(h);
18         }
19
20         t = timer_clock() - t;
21
22         utime_t usec = ticks_to_us(t) / 64;
23         kprintf("%s @ %ldMhz: %s of %dKiB of data: %lu.%lu ms\n", CPU_CORE_NAME, CPU_FREQ/1000000, hname, numk, (usec/1000), (usec % 1000));
24 }
25
26 void prng_benchmark(PRNG *prng, const char *hname, int numbytes)
27 {
28         memset(buf, 0x12, sizeof(buf));
29
30         ASSERT(sizeof(buf) >= prng_seed_len(prng));
31         prng_reseed(prng, buf);
32
33         ticks_t t = timer_clock();
34
35         for (int j=0;j<2048;++j) {
36                 for (int i=0; i<(numbytes+511)/512-1; ++i)
37                         prng_generate(prng, buf, 512);
38                 if (numbytes % 512)
39                         prng_generate(prng, buf, numbytes%512);
40         }
41
42         t = timer_clock() - t;
43
44         utime_t usec = ticks_to_us(t) / 2048;
45         kprintf("%s @ %ldMhz: %s generation of %d random bytes: %lu.%lu ms\n", CPU_CORE_NAME, CPU_FREQ/1000000, hname, numbytes, (usec/1000), (usec % 1000));
46         kprintf("Sample of random data:\n");
47         kdump(buf, MIN(numbytes, 64));
48 }