summaryrefslogtreecommitdiff
path: root/src/dict.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dict.c')
-rw-r--r--src/dict.c64
1 files changed, 38 insertions, 26 deletions
diff --git a/src/dict.c b/src/dict.c
index 4a9f3fb0a..21c616e6f 100644
--- a/src/dict.c
+++ b/src/dict.c
@@ -45,11 +45,7 @@
#include "dict.h"
#include "zmalloc.h"
-#ifndef DICT_BENCHMARK_MAIN
#include "redisassert.h"
-#else
-#include <assert.h>
-#endif
/* Using dictEnableResize() / dictDisableResize() we make possible to
* enable/disable resizing of the hash table as needed. This is very important
@@ -1175,20 +1171,18 @@ void dictGetStats(char *buf, size_t bufsize, dict *d) {
/* ------------------------------- Benchmark ---------------------------------*/
-#ifdef DICT_BENCHMARK_MAIN
-
-#include "sds.h"
+#ifdef REDIS_TEST
uint64_t hashCallback(const void *key) {
- return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
+ return dictGenHashFunction((unsigned char*)key, strlen((char*)key));
}
int compareCallback(void *privdata, const void *key1, const void *key2) {
int l1,l2;
DICT_NOTUSED(privdata);
- l1 = sdslen((sds)key1);
- l2 = sdslen((sds)key2);
+ l1 = strlen((char*)key1);
+ l2 = strlen((char*)key2);
if (l1 != l2) return 0;
return memcmp(key1, key2, l1) == 0;
}
@@ -1196,7 +1190,19 @@ int compareCallback(void *privdata, const void *key1, const void *key2) {
void freeCallback(void *privdata, void *val) {
DICT_NOTUSED(privdata);
- sdsfree(val);
+ zfree(val);
+}
+
+char *stringFromLongLong(long long value) {
+ char buf[32];
+ int len;
+ char *s;
+
+ len = sprintf(buf,"%lld",value);
+ s = zmalloc(len+1);
+ memcpy(s, buf, len);
+ s[len] = '\0';
+ return s;
}
dictType BenchmarkDictType = {
@@ -1215,22 +1221,26 @@ dictType BenchmarkDictType = {
printf(msg ": %ld items in %lld ms\n", count, elapsed); \
} while(0)
-/* dict-benchmark [count] */
-int main(int argc, char **argv) {
+/* ./redis-server test dict [<count> | --accurate] */
+int dictTest(int argc, char **argv, int accurate) {
long j;
long long start, elapsed;
dict *dict = dictCreate(&BenchmarkDictType,NULL);
long count = 0;
- if (argc == 2) {
- count = strtol(argv[1],NULL,10);
+ if (argc == 4) {
+ if (accurate) {
+ count = 5000000;
+ } else {
+ count = strtol(argv[3],NULL,10);
+ }
} else {
- count = 5000000;
+ count = 5000;
}
start_benchmark();
for (j = 0; j < count; j++) {
- int retval = dictAdd(dict,sdsfromlonglong(j),(void*)j);
+ int retval = dictAdd(dict,stringFromLongLong(j),(void*)j);
assert(retval == DICT_OK);
}
end_benchmark("Inserting");
@@ -1243,28 +1253,28 @@ int main(int argc, char **argv) {
start_benchmark();
for (j = 0; j < count; j++) {
- sds key = sdsfromlonglong(j);
+ char *key = stringFromLongLong(j);
dictEntry *de = dictFind(dict,key);
assert(de != NULL);
- sdsfree(key);
+ zfree(key);
}
end_benchmark("Linear access of existing elements");
start_benchmark();
for (j = 0; j < count; j++) {
- sds key = sdsfromlonglong(j);
+ char *key = stringFromLongLong(j);
dictEntry *de = dictFind(dict,key);
assert(de != NULL);
- sdsfree(key);
+ zfree(key);
}
end_benchmark("Linear access of existing elements (2nd round)");
start_benchmark();
for (j = 0; j < count; j++) {
- sds key = sdsfromlonglong(rand() % count);
+ char *key = stringFromLongLong(rand() % count);
dictEntry *de = dictFind(dict,key);
assert(de != NULL);
- sdsfree(key);
+ zfree(key);
}
end_benchmark("Random access of existing elements");
@@ -1277,17 +1287,17 @@ int main(int argc, char **argv) {
start_benchmark();
for (j = 0; j < count; j++) {
- sds key = sdsfromlonglong(rand() % count);
+ char *key = stringFromLongLong(rand() % count);
key[0] = 'X';
dictEntry *de = dictFind(dict,key);
assert(de == NULL);
- sdsfree(key);
+ zfree(key);
}
end_benchmark("Accessing missing");
start_benchmark();
for (j = 0; j < count; j++) {
- sds key = sdsfromlonglong(j);
+ char *key = stringFromLongLong(j);
int retval = dictDelete(dict,key);
assert(retval == DICT_OK);
key[0] += 17; /* Change first number to letter. */
@@ -1295,5 +1305,7 @@ int main(int argc, char **argv) {
assert(retval == DICT_OK);
}
end_benchmark("Removing and adding");
+ dictRelease(dict);
+ return 0;
}
#endif