/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>

/* high precision time */
#ifdef WIN32
#include <windows.h>
static inline double xtime()
{
    BOOL res;
    LARGE_INTEGER t, f;
    QueryPerformanceFrequency(&f);
    res = QueryPerformanceCounter(&t);
    return res ? (t.QuadPart * 1.0 / f.QuadPart) : 0;
}
#else
#include <time.h>
static inline double xtime()
{
    int res;
    struct timespec t;
    res = clock_gettime(CLOCK_MONOTONIC, &t);
    return (res == 0) ? (t.tv_sec + t.tv_nsec / 1000000000.0) : 0;
}
#endif

/* simple thread API */
#ifdef WIN32
#define T HANDLE
#define TSTART(t, f) t = CreateThread(NULL, 0, f, NULL, 0, NULL)
#define TWAIT(t) WaitForSingleObject(t, INFINITE); CloseHandle(t)
#define TFRETURN DWORD __stdcall
typedef DWORD (__stdcall *fptr)(void *data);
#else
#include <pthread.h>
#define T pthread_t
#define TSTART(t, f) pthread_create(&t, NULL, f, NULL)
#define TWAIT(t) pthread_join(t, NULL);
#define TFRETURN void *
typedef void *(*fptr)(void *data);
#endif

#include <hiredis/hiredis.h>

#define NOPS 10000
#define NTHREADS 100

static TFRETURN test1(void *data)
{
    redisContext *cache;
    redisReply *reply;
    char *key = "hot";
    char val[21];
    int i, ival;
    cache = redisConnect("localhost", 6379);
    for(i = 0; i < NOPS / NTHREADS; i++)
    {
        reply = redisCommand(cache, "GET %s", key);
        if (reply == NULL)
        {
            printf("get failed: %s\n", cache->errstr);
            exit(1);
        }
        strcpy(val, reply->str);
        freeReplyObject(reply);
        ival = atoi(val);
        ival = ival + 1;
        sprintf(val, "%d", ival);
        reply = redisCommand(cache, "SET %s %s", key, val);
        if (reply == NULL)
        {
            printf("set failed: %s\n", cache->errstr);
            exit(1);
        }
        freeReplyObject(reply);
    }
    redisFree(cache);
    return 0;
}

static TFRETURN test4(void *data)
{
    redisContext *cache;
    redisReply *reply;
    char *key = "hot";
    long long int val;
    int i;
    cache = redisConnect("localhost", 6379);
    for(i = 0; i < NOPS / NTHREADS; i++)
    {
        reply = redisCommand(cache, "INCRBY %s %s", key, "1");
        if (reply == NULL)
        {
            printf("incrby failed: %s\n", cache->errstr);
            exit(1);
        }
        freeReplyObject(reply);
    }
    redisFree(cache);
    return 0;
}

static void test(redisContext *cache, char *lbl, fptr f)
{
    redisReply *reply;
    char *key = "hot";
    char val[21];
    int i;
    double t1, t2;
    T t[NTHREADS];
    strcpy(val, "0");
    reply = redisCommand(cache, "SET %s %s", key, val);
    if (reply == NULL)
    {
        printf("set failed: %s\n", cache->errstr);
        exit(1);
    }
    freeReplyObject(reply);
    t1 = xtime();
    for(i = 0; i < NTHREADS; i++)
    {
        TSTART(t[i], f);
    }
    for(i = 0; i < NTHREADS; i++)
    {
        TWAIT(t[i]);
    }
    t2 = xtime();
    reply = redisCommand(cache, "GET %s", key);
    if (reply == NULL)
    {
        printf("get failed: %s\n", cache->errstr);
        exit(1);
    }
    strcpy(val, reply->str);
    freeReplyObject(reply);
    printf("%s : %s = %d (%.3f ms)\n", lbl, val, NOPS, t2 - t1);
}

int main()
{
    redisContext *cache;
    cache = redisConnect("localhost", 6379);
    test(cache, "No handling", test1);
    test(cache, "Atomic increment", test4);
    redisFree(cache);
    return 0;
}
