forked from h0x91b/redis-fast-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
149 lines (127 loc) · 3.14 KB
/
example.js
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
var Redis = require('.');
//var Redis = require('redis-fast-driver');
var r = new Redis({
host: '127.0.0.1',
port: 6379
});
//happen only once
r.on('ready', function(){
console.log('redis ready');
});
//happen each time when reconnected
r.on('connected', function(){
console.log('redis connected');
});
r.on('disconnected', function(){
console.log('redis disconnected');
});
r.on('error', function(e){
console.log('redis error', e);
});
//rawCall function has 2 arguments,
//1 - array which contain a redis command
//2 - optional callback
//Redis command is case insesitive, e.g. you can specify HMGET as HMGET, hmget or HmGeT
//but keys and value are case sensitive, foo, Foo, FoO not the same...
r.rawCall(['set', 'foo', 'bar'], function(err, resp){
console.log('SET via rawCall command returns err: %s, resp: %s', err, resp);
});
r.rawCall(['hmset', 'hset:1', 'a', 1, 'b', 2, 'c', 3]);
r.rawCall(['zadd', 'zset:1', 1, 'a', 2, 'b', 3, 'c', 4, 'd']);
var pings = 3;
function ping() {
r.rawCall(['PING'], function(e, d){
console.log('PING', e, d);
});
r.rawCall(['hmget', 'hset:1', 'a', 'b', 'c', 'f'], function(e,d,size){
console.log('hmget', e, d, size);
});
r.rawCall(['zrange', 'zset:1', 0, -1, 'WITHSCORES'], function(e,d){
console.log('zrange', e, d);
});
r.rawCall(['hgetall', 'hset:1'], function(e,d,size){
console.log('hgetall', e, d, size);
});
r.rawCall(['SCAN', 0], function(e,d){
console.log('scan 0', e, d);
});
r.rawCall(['HSCAN', 'hset:1', 0], function(e,d){
console.log('hscan 0', e, d);
});
r.rawCall(['SET', 'kv:long', Array(1024 + 1).join('.')], function(e,d){
r.rawCall(['GET', 'kv:long'], function(e,d,size){
console.log('sizeof long key is %s', size); //1024
});
});
if(--pings > 0)
setTimeout(ping, 1000);
else
setTimeout(bench, 1000, [2500, 5000, 10000, 25000, 50000]);
}
var tests = [
{
description: 'PING command',
cmd: ['PING']
},
{
description: 'INCR command',
cmd: ['INCR', 'INCR:TMP']
},
{
description: 'GET command',
cmd: ['GET', 'INCR:TMP']
},
{
description: 'HGET command',
cmd: ['HGET', 'hset:1', 'a']
},
{
description: 'HGETALL command',
cmd: ['HGETALL', 'hset:1']
},
{
description: 'ZRANGE 0 4 command',
cmd: ['ZRANGE', 'zset:1', 0, 4]
}
];
function bench(repeats) {
console.log(Array(50).join('='));
var repeatIndex = 0;
var i = 0;
start();
function start() {
var repeat = repeats[repeatIndex++];
if(!repeat) {
console.log(Array(50).join('='));
console.log('the end');
r.end();
return;
}
i=0;
doIt(repeat, start);
}
function doIt(repeat, done) {
var cmd = tests[i++];
if(!cmd) {
return done();
}
console.log('===\nStart test: %s %s times', cmd.description, repeat);
var start = +new Date();
for(var n=0;n<repeat;n++) {
r.rawCall(cmd.cmd, onComplete);
}
var complete = 0;
function onComplete(e, data){
if(e) {
console.log('error', e);
}
if(++complete === repeat) {
var now = +new Date();
var dt = now - start;
console.log('Test complete in %sms, speed %s in second, cold down 1.5 sec', dt, (repeat/(dt/1000)).toFixed(2));
setTimeout(doIt, 1500, repeat, done);
}
}
}
}
ping();