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

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif

/* high precision time */
#ifdef WIN32
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
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 <libmemcached/memcached.h>

#define NOPS 10000
#define NTHREADS 10 /* 100 does not work on my Windows */

static TFRETURN test1(void *data)
{
    const char *cfg = "--SERVER=localhost --BINARY-PROTOCOL";
    char *key = "hot";
    long long int val;
    int vallen, i;
    char *p;
    memcached_st *cache;
    memcached_return_t stat;
    cache = memcached(cfg, strlen(cfg));
    for(i = 0; i < NOPS / NTHREADS; i++)
    {
        p = memcached_get(cache, key, strlen(key), &vallen, NULL, &stat);
        if (stat != MEMCACHED_SUCCESS)
        {
            printf("get failed: %s\n", memcached_error(cache));
            exit(1);
        }
        val = *((long long int *)p);
        val = val + 1;
        stat = memcached_set(cache, key, strlen(key), (char *)&val, sizeof(val), 0, 0);
        if (stat != MEMCACHED_SUCCESS)
        {
            printf("set failed: %s\n", memcached_error(cache));
            exit(1);
        }
    }
    memcached_free(cache);
    return 0;
}

static TFRETURN test4(void *data)
{
    const char *cfg = "--SERVER=localhost --BINARY-PROTOCOL";
    char *key = "hot";
    long long int val;
    int vallen, i;
    char *p;
    memcached_st *cache;
    memcached_return_t stat;
    cache = memcached(cfg, strlen(cfg));
    for(i = 0; i < NOPS / NTHREADS; i++)
    {
        stat = memcached_increment(cache, key, strlen(key), 1, &val); 
        if (stat != MEMCACHED_SUCCESS)
        {
            printf("increment failed: %s\n", memcached_error(cache));
            exit(1);
        }
    }
    memcached_free(cache);
    return 0;
}

static void test(memcached_st *cache, char *lbl, fptr f)
{
    char *key = "hot";
    long long int val;
    int vallen, i;
    char *p;
    double t1, t2;
    T t[NTHREADS];
    memcached_return_t stat;
    val = 0;
    stat = memcached_set(cache, key, strlen(key), (char *)&val, sizeof(val), 0, 0);
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("set failed: %s\n", memcached_error(cache));
        exit(1);
    }

    if(f == test4)
    {
        /* hack input */
        stat = memcached_delete(cache, key, strlen(key), 0);
        stat = memcached_increment_with_initial(cache, key, strlen(key), 1, 0, 0, &val);
        stat = memcached_decrement(cache, key, strlen(key), 1, &val); 
    }

    t1 = xtime();
    for(i = 0; i < NTHREADS; i++)
    {
        TSTART(t[i], f);
    }
    for(i = 0; i < NTHREADS; i++)
    {
        TWAIT(t[i]);
    }
    t2 = xtime();
    
    if(f == test4)
    {
        /* hack output */
        p = memcached_get(cache, key, strlen(key), &vallen, NULL, &stat); 
        val = atoi(p);
        stat = memcached_set(cache, key, strlen(key), (char *)&val, sizeof(val), 0, 0);
    }

    p = memcached_get(cache, key, strlen(key), &vallen, NULL, &stat); 
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("get failed: %s\n", memcached_error(cache));
        exit(1);
    }
    val = *((long long int *)p);
    printf("%s : %lld = %d (%.3f ms)\n", lbl, val, NOPS, t2 - t1);
}

int main()
{
    const char *cfg = "--SERVER=localhost --BINARY-PROTOCOL";
    memcached_st *cache;
#ifdef WIN32
    WSADATA WSAData;
    WSAStartup(0x0101, &WSAData);
#endif
    cache = memcached(cfg, strlen(cfg));
    test(cache, "No handling", test1);
    test(cache, "Atomic increment", test4);
    memcached_free(cache);
#ifdef WIN32
    WSACleanup();
#endif
     return 0;
}
