Welcome to little lamb

Code » test-hashes » master » tree

[master] / src / test.c

#include <time.h>
#include <stdio.h>

#include <skalibs/buffer.h>
#include <skalibs/fmtscan.h>
static void
dump(const unsigned char *data, size_t dlen, int compact)
{
    char buf[3];
    buf[2] = ' ';
    int lf = compact < 2;
    compact = (compact) ? 1 : 0;

    size_t i;
    for (i = 0; i < dlen; ) {
        buf[1] = fmtscan_asc((unsigned char) data[i] % 16);
        buf[0] = fmtscan_asc((unsigned char) data[i] / 16);
        buffer_put(buffer_1, buf, 2 + !compact);
        if (!(++i % 8) && !compact) buffer_put(buffer_1, buf + 2, 1);
        if (!compact && !(i % 16)) buffer_put(buffer_1, "\n", 1);
    }
    if (lf && (compact || i % 16))
        buffer_put(buffer_1, "\n", 1);
    buffer_flush(buffer_1);
}

extern void init(void);
extern void update(const char *msg, size_t size);
extern void final(unsigned char *md);
extern int hashlen(void);

#if 0
#define SIZE    (512 << 10)
#define BLOCKS  20
#define ITER    100
#else
#define SIZE    (64 << 10)
#define BLOCKS  1
#define ITER    1000
#endif
int main(void)
{
    struct timespec ts1, ts2;
    char msg[SIZE];
    unsigned char md[64] = { 0 };

    for (int i = SIZE; i; --i)
        msg[i] = i % 256;

    clock_gettime(CLOCK_MONOTONIC, &ts1);
    for (int iter = 0; iter < ITER; ++iter) {
        init();
        for (int i = 0; i < BLOCKS; ++i)
            update(msg, sizeof(msg));
        final(md);
    }
    clock_gettime(CLOCK_MONOTONIC, &ts2);
    dump(md, hashlen(), 1);

    ts2.tv_sec -= ts1.tv_sec;
    ts2.tv_nsec -= ts1.tv_nsec;
    double took = ts2.tv_sec + (ts2.tv_nsec / 1000000000.0);
    double speed = ((SIZE * BLOCKS * ITER) / took) / (1 << 20);
    printf("took %.09f seconds, hashing %f MiB/s\n", took, speed);

    return 0;
}