1
1
'use strict' ;
2
2
3
- var redis = require ( "../index" ) ,
4
- metrics = require ( "metrics" ) ,
5
- num_clients = parseInt ( process . argv [ 2 ] , 10 ) || 5 ,
6
- num_requests = 20000 ,
7
- tests = [ ] ,
8
- versions_logged = false ,
9
- client_options = {
10
- return_buffers : false
11
- } ,
12
- small_str , large_str , small_buf , large_buf , very_large_str , very_large_buf ;
3
+ var path = require ( 'path' ) ;
4
+ var RedisProcess = require ( "../test/lib/redis-process" ) ;
5
+ var rp ;
6
+ var redis = require ( "../index" ) ;
7
+ var totalTime = 0 ;
8
+ var metrics = require ( "metrics" ) ;
9
+ var num_clients = parseInt ( process . argv [ 2 ] , 10 ) || 5 ;
10
+ var num_requests = 20000 ;
11
+ var tests = [ ] ;
12
+ var versions_logged = false ;
13
+ var client_options = {
14
+ return_buffers : false ,
15
+ max_attempts : 4 ,
16
+ parser : process . argv . indexOf ( 'parser=javascript' ) === - 1 ? 'hiredis' : 'javascript'
17
+ } ;
18
+ var small_str , large_str , small_buf , large_buf , very_large_str , very_large_buf ;
13
19
14
20
function lpad ( input , len , chr ) {
15
21
var str = input . toString ( ) ;
@@ -57,7 +63,7 @@ Test.prototype.run = function (callback) {
57
63
Test . prototype . new_client = function ( id ) {
58
64
var self = this , new_client ;
59
65
60
- new_client = redis . createClient ( 6379 , "127.0.0.1" , this . client_options ) ;
66
+ new_client = redis . createClient ( this . client_options ) ;
61
67
new_client . create_time = Date . now ( ) ;
62
68
63
69
new_client . on ( "connect" , function ( ) {
@@ -77,6 +83,24 @@ Test.prototype.new_client = function (id) {
77
83
}
78
84
} ) ;
79
85
86
+ // If no redis server is running, start one
87
+ new_client . on ( "error" , function ( err ) {
88
+ if ( err . code === 'CONNECTION_BROKEN' ) {
89
+ throw err ;
90
+ }
91
+ if ( rp ) {
92
+ return ;
93
+ }
94
+ rp = true ;
95
+ var conf = '../test/conf/redis.conf' ;
96
+ RedisProcess . start ( function ( err , _rp ) {
97
+ if ( err ) {
98
+ throw err ;
99
+ }
100
+ rp = _rp ;
101
+ } , path . resolve ( __dirname , conf ) ) ;
102
+ } ) ;
103
+
80
104
self . clients [ id ] = new_client ;
81
105
} ;
82
106
@@ -133,6 +157,7 @@ Test.prototype.send_next = function () {
133
157
134
158
Test . prototype . print_stats = function ( ) {
135
159
var duration = Date . now ( ) - this . test_start ;
160
+ totalTime += duration ;
136
161
137
162
console . log ( "min/max/avg/p95: " + this . command_latency . print_line ( ) + " " + lpad ( duration , 6 ) + "ms total, " +
138
163
lpad ( ( this . num_requests / ( duration / 1000 ) ) . toFixed ( 2 ) , 8 ) + " ops/sec" ) ;
@@ -199,8 +224,14 @@ function next() {
199
224
test . run ( function ( ) {
200
225
next ( ) ;
201
226
} ) ;
227
+ } else if ( rp ) {
228
+ // Stop the redis process if started by the benchmark
229
+ rp . stop ( function ( ) {
230
+ rp = undefined ;
231
+ next ( ) ;
232
+ } ) ;
202
233
} else {
203
- console . log ( "End of tests." ) ;
234
+ console . log ( "End of tests. Total time elapsed:" , totalTime , 'ms' ) ;
204
235
process . exit ( 0 ) ;
205
236
}
206
237
}
0 commit comments