bb-latency-bench.c 5.82 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

16
#include "bake-bulk-client.h"
Philip Carns's avatar
Philip Carns committed
17

18 19 20 21 22 23 24 25
static void bench_routine_write(bake_target_id_t bti, int iterations, double* measurement_array, int size);
static void bench_routine_read(bake_target_id_t bti, int iterations, double* measurement_array, int size);
static void bench_routine_noop(bake_target_id_t bti, int iterations, double* measurement_array);
static void bench_routine_print(const char* op, int size, int iterations, double* measurement_array);
static int measurement_cmp(const void* a, const void *b);

static double *measurement_array = NULL;
static bake_bulk_region_id_t rid;
Philip Carns's avatar
Philip Carns committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

int main(int argc, char **argv) 
{
    int ret;
    bake_target_id_t bti;
    int min_size, max_size, iterations, cur_size;
 
    if(argc != 5)
    {
        fprintf(stderr, "Usage: bb-latency-bench <server addr> <iterations> <min_sz> <max_sz>\n");
        fprintf(stderr, "  Example: ./bb-latency-bench tcp://localhost:1234 1000 4 32\n");
        return(-1);
    }       

    ret = sscanf(argv[2], "%d", &iterations);
    assert(ret == 1);

    ret = sscanf(argv[3], "%d", &min_size);
    assert(ret == 1);

    ret = sscanf(argv[4], "%d", &max_size);
    assert(ret == 1);

49 50 51
    measurement_array = malloc(sizeof(*measurement_array)*iterations);
    assert(measurement_array);

Philip Carns's avatar
Philip Carns committed
52 53 54 55 56 57 58
    ret = bake_probe_instance(argv[1], &bti);
    if(ret < 0)
    {
        fprintf(stderr, "Error: bake_probe_instance()\n");
        return(-1);
    }

59 60 61 62
    printf("# <op> <iterations> <size> <min> <q1> <med> <avg> <q3> <max>\n");

    bench_routine_noop(bti, iterations, measurement_array);
    bench_routine_print("noop", 0, iterations, measurement_array);
Philip Carns's avatar
Philip Carns committed
63
    for(cur_size=min_size; cur_size <= max_size; cur_size *= 2)
64 65 66 67 68 69
    {
        bench_routine_write(bti, iterations, measurement_array, cur_size);
        bench_routine_print("write", cur_size, iterations, measurement_array);
        bench_routine_read(bti, iterations, measurement_array, cur_size);
        bench_routine_print("read", cur_size, iterations, measurement_array);
    }
Philip Carns's avatar
Philip Carns committed
70 71 72
    
    bake_release_instance(bti);

73 74
    free(measurement_array);

Philip Carns's avatar
Philip Carns committed
75 76 77 78 79
    return(0);
}

static double Wtime(void)
{
80 81 82
    struct timespec tp;
    clock_gettime(CLOCK_MONOTONIC, &tp);
    return((double)tp.tv_sec + (double)(tp.tv_nsec) / (double)1000000000.0);
Philip Carns's avatar
Philip Carns committed
83 84
}

85
static void bench_routine_write(bake_target_id_t bti, int iterations, double *measurement_array, int size)
Philip Carns's avatar
Philip Carns committed
86 87
{
    int ret;
88
    double tm1, tm2;
Philip Carns's avatar
Philip Carns committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    char *buffer;
    uint64_t region_offset = 0;
    int i;

    buffer = calloc(1, size);
    assert(buffer);

    /* create region */
    ret = bake_bulk_create(bti, size*iterations, &rid);
    assert(ret == 0);

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* transfer data (writes) */
        ret = bake_bulk_write(
            bti,
            rid,
            region_offset,
            buffer,
            size);
        tm2 = Wtime();
        assert(ret == 0);
        region_offset += size;
115
        measurement_array[i] = tm2-tm1;
Philip Carns's avatar
Philip Carns committed
116 117
    }

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    /* persist */
    ret = bake_bulk_persist(bti, rid);
    assert(ret == 0);

    free(buffer);

    return;
}

static void bench_routine_read(bake_target_id_t bti, int iterations, double *measurement_array, int size)
{
    int ret;
    double tm1, tm2;
    char *buffer;
    uint64_t region_offset = 0;
    int i;

    buffer = calloc(1, size);
    assert(buffer);
Philip Carns's avatar
Philip Carns committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* transfer data (reads) */
        ret = bake_bulk_read(
            bti,
            rid,
            region_offset,
            buffer,
            size);
        tm2 = Wtime();
        assert(ret == 0);
        region_offset += size;
153
        measurement_array[i] = tm2-tm1;
Philip Carns's avatar
Philip Carns committed
154 155 156 157 158 159
    }

    free(buffer);

    return;
}
Philip Carns's avatar
Philip Carns committed
160

161
static void bench_routine_noop(bake_target_id_t bti, int iterations, double *measurement_array)
Philip Carns's avatar
Philip Carns committed
162 163
{
    int ret;
164
    double tm1, tm2;
Philip Carns's avatar
Philip Carns committed
165 166 167 168 169 170 171 172 173 174 175 176
    int i;

    sleep(1);

    for(i=0; i<iterations; i++)
    {
        tm1 = Wtime();
        /* noop */
        ret = bake_bulk_noop(bti);
        tm2 = Wtime();
        assert(ret == 0);

177
        measurement_array[i] = tm2-tm1;
Philip Carns's avatar
Philip Carns committed
178 179
    }

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    return;
}

static int measurement_cmp(const void* a, const void *b)
{
    const double *d_a = a;
    const double *d_b = b;

    if(*d_a < *d_b)
        return(-1);
    else if(*d_a > *d_b)
        return(1);
    else
        return(0);
}

static void bench_routine_print(const char* op, int size, int iterations, double* measurement_array)
{
    double min, max, q1, q3, med, avg, sum;
    int bracket1, bracket2;
    int i;

    qsort(measurement_array, iterations, sizeof(double), measurement_cmp);

    min = measurement_array[0];
    max = measurement_array[iterations-1];

    sum = 0;
    for(i=0; i<iterations; i++)
    {
        sum += measurement_array[i];
    }
    avg = sum/(double)iterations;

    bracket1 = iterations/2;
    if(iterations%2)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    med = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

    bracket1 = iterations/4;
    if(iterations%4)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    q1 = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

    bracket1 *= 3;
    if(iterations%4)
        bracket2 = bracket1 + 1;
    else
        bracket2 = bracket1;
    q3 = (measurement_array[bracket1] + measurement_array[bracket2])/(double)2;

Philip Carns's avatar
Philip Carns committed
235 236 237 238 239 240 241
    printf("%s\t%d\t%d\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f", op, iterations, size, min, q1, med, avg, q3, max);
    for(i=0; i<iterations; i++)
    {
        printf("\t%.9f", measurement_array[i]);
    }
    printf("\n");
    fflush(NULL);
Philip Carns's avatar
Philip Carns committed
242 243 244

    return;
}