BeRTOS
benchmarks.c
00001 #include "benchmarks.h"
00002 #include <drv/timer.h>
00003 #include <string.h>
00004 
00005 static uint8_t buf[512];
00006 
00007 void hash_benchmark(Hash *h, const char *hname, int numk)
00008 {
00009     memset(buf, 0x12, sizeof(buf));
00010     ticks_t t = timer_clock();
00011 
00012     for (int j=0;j<64;++j) {
00013         hash_begin(h);
00014         for (int i=0; i<numk*2; ++i)
00015             hash_update(h, buf, 512);
00016         hash_final(h);
00017     }
00018 
00019     t = timer_clock() - t;
00020 
00021     utime_t usec = ticks_to_us(t) / 64;
00022     kprintf("%s @ %ldMhz: %s of %dKiB of data: %lu.%lu ms\n", CPU_CORE_NAME, CPU_FREQ/1000000, hname, numk, (usec/1000), (usec % 1000));
00023 }
00024 
00025 void prng_benchmark(PRNG *prng, const char *hname, int numbytes)
00026 {
00027     memset(buf, 0x12, sizeof(buf));
00028 
00029     ASSERT(sizeof(buf) >= prng_seed_len(prng));
00030     prng_reseed(prng, buf);
00031 
00032     ticks_t t = timer_clock();
00033     enum { CYCLES = 2048 };
00034 
00035     for (int j=0;j<CYCLES;++j)
00036     {
00037         for (int i=0; i<(numbytes+511)/512-1; ++i)
00038             prng_generate(prng, buf, 512);
00039         if (numbytes % 512)
00040             prng_generate(prng, buf, numbytes%512);
00041     }
00042 
00043     t = timer_clock() - t;
00044 
00045     utime_t usec = ticks_to_us(t) / CYCLES;
00046     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));
00047     kprintf("Sample of random data:\n");
00048     kdump(buf, MIN(numbytes, 64));
00049 }
00050 
00051 void cipher_benchmark(BlockCipher *c, const char *cname, int numbytes)
00052 {
00053     memset(buf, 0x12, sizeof(buf));
00054 
00055     ASSERT(sizeof(buf) >= cipher_key_len(c));
00056     cipher_set_key(c, buf);
00057 
00058     uint8_t iv[cipher_block_len(c)];
00059     memset(iv, 0, sizeof(iv));
00060 
00061     ticks_t t = timer_clock();
00062     enum { CYCLES = 64 };
00063 
00064     for (int j=0;j<CYCLES;++j)
00065     {
00066         cipher_cbc_begin(c, iv);
00067         int numblocks = (numbytes / cipher_block_len(c)) + 1;
00068         for (int i=0; i<numblocks; ++i)
00069             cipher_cbc_encrypt(c, buf);
00070     }
00071 
00072     t = timer_clock() - t;
00073 
00074     utime_t usec = ticks_to_us(t) / CYCLES;
00075     kprintf("%s @ %ldMhz: %s-CBC of %d bytes: %lu.%lu ms (%d KiB/s)\n",
00076             CPU_CORE_NAME, CPU_FREQ/1000000,
00077             cname, numbytes,
00078             (usec/1000), (usec % 1000),
00079             (uint32_t)(numbytes * (CYCLES * 1000000 / 1024) / ticks_to_us(t)));
00080 }