d0e3cdfffaf8aaacc21d34c47c7508a41c10af10
[bertos.git] / bertos / sec / benchmarks.c
1 #include "benchmarks.h"
2 #include <drv/timer.h>
3 #include <string.h>
4
5 static uint8_t buf[512];
6
7 void hash_benchmark(Hash *h, const char *hname, int numk)
8 {
9         memset(buf, 0x12, sizeof(buf));
10         ticks_t t = timer_clock();
11
12         for (int j=0;j<64;++j) {
13                 hash_begin(h);
14                 for (int i=0; i<numk*2; ++i)
15                         hash_update(h, buf, 512);
16                 hash_final(h);
17         }
18
19         t = timer_clock() - t;
20
21         utime_t usec = ticks_to_us(t) / 64;
22         kprintf("%s @ %ldMhz: %s of %dKiB of data: %lu.%lu ms\n", CPU_CORE_NAME, CPU_FREQ/1000000, hname, numk, (usec/1000), (usec % 1000));
23 }
24
25 void prng_benchmark(PRNG *prng, const char *hname, int numbytes)
26 {
27         memset(buf, 0x12, sizeof(buf));
28
29         ASSERT(sizeof(buf) >= prng_seed_len(prng));
30         prng_reseed(prng, buf);
31
32         ticks_t t = timer_clock();
33         enum { CYCLES = 2048 };
34
35         for (int j=0;j<CYCLES;++j)
36         {
37                 for (int i=0; i<(numbytes+511)/512-1; ++i)
38                         prng_generate(prng, buf, 512);
39                 if (numbytes % 512)
40                         prng_generate(prng, buf, numbytes%512);
41         }
42
43         t = timer_clock() - t;
44
45         utime_t usec = ticks_to_us(t) / CYCLES;
46         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));
47         kprintf("Sample of random data:\n");
48         kdump(buf, MIN(numbytes, 64));
49 }
50
51 void cipher_benchmark(BlockCipher *c, const char *cname, int numbytes)
52 {
53         memset(buf, 0x12, sizeof(buf));
54
55         ASSERT(sizeof(buf) >= cipher_key_len(c));
56         cipher_set_key(c, buf);
57
58         uint8_t iv[cipher_block_len(c)];
59         memset(iv, 0, sizeof(iv));
60
61         ticks_t t = timer_clock();
62         enum { CYCLES = 64 };
63
64         for (int j=0;j<CYCLES;++j)
65         {
66                 cipher_cbc_begin(c, iv);
67                 int numblocks = (numbytes / cipher_block_len(c)) + 1;
68                 for (int i=0; i<numblocks; ++i)
69                         cipher_cbc_encrypt(c, buf);
70         }
71
72         t = timer_clock() - t;
73
74         utime_t usec = ticks_to_us(t) / CYCLES;
75         kprintf("%s @ %ldMhz: %s-CBC of %d bytes: %lu.%lu ms (%d KiB/s)\n",
76                         CPU_CORE_NAME, CPU_FREQ/1000000,
77                         cname, numbytes,
78                         (usec/1000), (usec % 1000),
79                         (uint32_t)(numbytes * (CYCLES * 1000000 / 1024) / ticks_to_us(t)));
80 }