Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 238 lines (206 sloc) 6.459 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 235 236 237
/* Simple speed tests for jmap. */
#include <ccan/jmap/jmap_type.h>
#include <ccan/jmap/jmap.c>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>

struct object {
/* Some contents. Doubles as consistency check. */
struct object *self;
};

/* Nanoseconds per operation */
static size_t normalize(const struct timeval *start,
const struct timeval *stop,
unsigned int num)
{
struct timeval diff;

timersub(stop, start, &diff);

/* Floating point is more accurate here. */
return (double)(diff.tv_sec * 1000000 + diff.tv_usec)
/ num * 1000;
}

JMAP_DEFINE_UINTIDX_TYPE(struct object, obj);

int main(int argc, char *argv[])
{
struct object *objs;
size_t i, j, num;
struct timeval start, stop;
struct jmap_obj *jmap;

num = argv[1] ? atoi(argv[1]) : 1000000;
objs = calloc(num, sizeof(objs[0]));

for (i = 0; i < num; i++) {
objs[i].self = &objs[i];
}

jmap = jmap_obj_new();

printf("Initial insert: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
jmap_obj_add(jmap, i, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Initial lookup (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (jmap_obj_get(jmap, i)->self != objs[i].self)
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Initial lookup (miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (jmap_obj_get(jmap, i+num))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

/* Lookups in order are very cache-friendly for judy; try random */
printf("Initial lookup (random): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (jmap_obj_get(jmap, j)->self != &objs[j])
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Initial delete all: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
jmap_obj_del(jmap, i);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Initial re-inserting: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
jmap_obj_add(jmap, i, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Deleting first half: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
jmap_obj_del(jmap, i);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Adding (a different) half: ");
fflush(stdout);

gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
jmap_obj_add(jmap, num+i, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Lookup after half-change (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 1; i < num; i+=2)
if (jmap_obj_get(jmap, i)->self != objs[i].self)
abort();
for (i = 0; i < num; i+=2)
if (jmap_obj_get(jmap, i+num)->self != objs[i].self)
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Lookup after half-change(miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (jmap_obj_get(jmap, i+num*2))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

/* Hashtables with delete markers can fill with markers over time.
* so do some changes to see how it operates in long-term. */
printf("Details: churning first time\n");
for (i = 1; i < num; i+=2) {
if (!jmap_obj_del(jmap, i))
abort();
jmap_obj_add(jmap, i, objs[i].self);
}
for (i = 0; i < num; i+=2) {
if (!jmap_obj_del(jmap, i+num))
abort();
jmap_obj_add(jmap, i, objs[i].self);
}
for (i = 1; i < 5; i++) {
printf("Churning %s time: ",
i == 1 ? "second"
: i == 2 ? "third"
: i == 3 ? "fourth"
: "fifth");
fflush(stdout);
gettimeofday(&start, NULL);
for (j = 0; j < num; j++) {
if (!jmap_obj_del(jmap, num*(i-1)+j))
abort();
jmap_obj_add(jmap, num*i+j, &objs[j]);
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));
}

/* Spread out the keys more to try to make it harder. */
printf("Details: reinserting with spread\n");
for (i = 0; i < num; i++) {
if (!jmap_obj_del(jmap, num*4 + i))
abort();
jmap_obj_add(jmap, num * 5 + i * 9, objs[i].self);
}

if (jmap_obj_popcount(jmap, 0, -1) != num)
abort();

printf("Lookup after churn & spread (match): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (jmap_obj_get(jmap, num * 5 + i * 9)->self != objs[i].self) {
printf("i =%u\n", i);
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Lookup after churn & spread (miss): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i++)
if (jmap_obj_get(jmap, num * 6 + i * 9))
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Lookup after churn & spread (random): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0, j = 0; i < num; i++, j = (j + 10007) % num)
if (jmap_obj_get(jmap, num * 5 + j * 9)->self != &objs[j])
abort();
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Lookup after churn & spread (half-random): ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0, j = 0; i < num/2; i++, j = (j + 10007) % num) {
if (jmap_obj_get(jmap, num * 5 + j * 9)->self != &objs[j])
abort();
if (jmap_obj_get(jmap, num * 5 + (j + 1) * 9)->self != &objs[j+1])
abort();
}
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Deleting half after churn & spread: ");
fflush(stdout);
gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
jmap_obj_del(jmap, num * 5 + i * 9);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

printf("Adding (a different) half after churn & spread: ");
fflush(stdout);

gettimeofday(&start, NULL);
for (i = 0; i < num; i+=2)
jmap_obj_add(jmap, num * 6 + i * 9, objs[i].self);
gettimeofday(&stop, NULL);
printf(" %zu ns\n", normalize(&start, &stop, num));

return 0;
}
Something went wrong with that request. Please try again.