Skip to content

Instantly share code, notes, and snippets.

@zhujun1980
Created December 23, 2014 08:33
Show Gist options
  • Save zhujun1980/7e907705ab61a3e71a7b to your computer and use it in GitHub Desktop.
Save zhujun1980/7e907705ab61a3e71a7b to your computer and use it in GitHub Desktop.
conhash test
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <conhash/conhash.h>
#include "murmur_hash3.h"
struct node_s* connodes;
struct conhash_s* conhash;
int size;
int* stat;
int max;
long murmur_hash(const char *key) {
uint32_t b;
uint32_t seed = 0x7293AF;
MurmurHash3_x86_32(key, strlen(key), seed, &b);
return b;
}
void add_nodes() {
char key[10];
char *obj;
int r, i;
max = 1000000;
size = 5;
conhash = conhash_init(murmur_hash);
connodes = (struct node_s*)calloc(size, sizeof(struct node_s));
stat = malloc(sizeof(int) * size);
for(i = 0; i < size; ++i) {
r = snprintf(key, sizeof(key) - 1, "%d", i);
key[r] = '\0';
obj = (char*)malloc(10);
r = snprintf(obj, 9, "o%d", i);
obj[r] = '\0';
conhash_set_node(&connodes[i], key, 128, obj);
conhash_add_node(conhash, &connodes[i]);
}
}
void use_chash() {
int i;
struct node_s *node;
char q[13] = {0};
srand(time(NULL));
for(i = 0; i < max; ++i) {
int r = rand();
snprintf(q, sizeof(q) - 1, "%d", r);
node = conhash_lookup(conhash, q);
//printf("%s: %s %s\n", q, node->iden, (char*)node->pdata);
int idx = atoi(node->iden);
assert((idx < size));
stat[idx]++;
}
}
void chash_stat() {
int i, total;
//u_int num = conhash_get_vnodes_num(conhash);
//printf("%d\n", num);
//long *v = (long*)malloc(sizeof(long) * num);
//conhash_get_vnodes(conhash, v, num);
total = 0;
for(i = 0; i < size; ++i) {
double r = stat[i] / (max*1.0);
printf("%d: %d %f\n", i, stat[i], r * 100);
total += stat[i];
}
assert(max == total);
//free(v);
}
void clear_nodes() {
conhash_fini(conhash);
free(connodes);
free(stat);
}
int main(int argc, char* argv[]) {
add_nodes();
use_chash();
chash_stat();
clear_nodes();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment