22
33/* eslint handle-callback-err: 0 */
44
5- var intercept = require ( 'intercept-stdout ' )
6- var Benchmark = require ( 'benchmark' )
7- var suite = new Benchmark . Suite ( )
8- var Parser = require ( './../ ' )
5+ const Benchmark = require ( 'benchmark ' )
6+ const suite = new Benchmark . Suite ( )
7+ const Parser = require ( './../' )
8+ const HiredisParser = require ( '../test/hiredis ' )
99
1010function returnError ( error ) { }
1111function checkReply ( error , res ) { }
12- function shuffle ( array ) {
13- var currentIndex = array . length
14- var temporaryValue
15- var randomIndex
16-
17- // While there remain elements to shuffle...
18- while ( currentIndex !== 0 ) {
19- // Pick a remaining element...
20- randomIndex = Math . floor ( Math . random ( ) * currentIndex )
21- currentIndex -= 1
22-
23- // And swap it with the current element.
24- temporaryValue = array [ currentIndex ]
25- array [ currentIndex ] = array [ randomIndex ]
26- array [ randomIndex ] = temporaryValue
27- }
28-
29- return array
30- }
3112
32- // Suppress hiredis warnings
33- intercept ( function ( ) { } , function ( ) { return '' } )
34-
35- var startBuffer = new Buffer ( '$100\r\nabcdefghij' )
36- var chunkBuffer = new Buffer ( 'abcdefghijabcdefghijabcdefghij' )
37- var stringBuffer = new Buffer ( '+testing a simple string\r\n' )
38- var integerBuffer = new Buffer ( ':1237884\r\n' )
39- var bigIntegerBuffer = new Buffer ( ':184467440737095516171234567890\r\n' ) // 2^64 + 1
40- var errorBuffer = new Buffer ( '-Error ohnoesitbroke\r\n' )
41- var endBuffer = new Buffer ( '\r\n' )
42- var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
13+ const startBuffer = new Buffer ( '$100\r\nabcdefghij' )
14+ const chunkBuffer = new Buffer ( 'abcdefghijabcdefghijabcdefghij' )
15+ const stringBuffer = new Buffer ( '+testing a simple string\r\n' )
16+ const integerBuffer = new Buffer ( ':1237884\r\n' )
17+ const bigIntegerBuffer = new Buffer ( ':184467440737095516171234567890\r\n' ) // 2^64 + 1
18+ const errorBuffer = new Buffer ( '-Error ohnoesitbroke\r\n' )
19+ const endBuffer = new Buffer ( '\r\n' )
20+ const lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, ' +
4321 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ' +
4422 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ' +
4523 'ut aliquip ex ea commodo consequat. Duis aute irure dolor in' // 256 chars
46- var bigStringArray = ( new Array ( Math . pow ( 2 , 16 ) / lorem . length ) . join ( lorem + ' ' ) ) . split ( ' ' ) // Math.pow(2, 16) chars long
47- var startBigBuffer = new Buffer ( '$' + ( 4 * 1024 * 1024 ) + '\r\n' )
24+ const bigStringArray = ( new Array ( Math . pow ( 2 , 16 ) / lorem . length ) . join ( lorem + ' ' ) ) . split ( ' ' ) // Math.pow(2, 16) chars long
25+ const startBigBuffer = new Buffer ( '$' + ( 4 * 1024 * 1024 ) + '\r\n' )
4826
49- var chunks = new Array ( 64 )
27+ const chunks = new Array ( 64 )
5028for ( var i = 0 ; i < 64 ; i ++ ) {
51- chunks [ i ] = new Buffer ( shuffle ( bigStringArray ) . join ( ' ' ) + '.' ) // Math.pow(2, 16) chars long
29+ chunks [ i ] = new Buffer ( bigStringArray . join ( ' ' ) + '.' ) // Math.pow(2, 16) chars long
5230}
5331
54- var arraySize = 100
32+ const arraySize = 100
5533var array = '*' + arraySize + '\r\n'
5634var size = 0
5735for ( i = 0 ; i < arraySize ; i ++ ) {
@@ -60,10 +38,10 @@ for (i = 0; i < arraySize; i++) {
6038 array += size + '\r\n' + lorem . slice ( 0 , size ) + '\r\n'
6139}
6240
63- var arrayBuffer = new Buffer ( array )
41+ const arrayBuffer = new Buffer ( array )
6442
65- var bigArraySize = 160
66- var bigArrayChunks = [ new Buffer ( '*1\r\n*1\r\n*' + bigArraySize ) ]
43+ const bigArraySize = 160
44+ const bigArrayChunks = [ new Buffer ( '*1\r\n*1\r\n*' + bigArraySize ) ]
6745for ( i = 0 ; i < bigArraySize ; i ++ ) {
6846 // A chunk has a maximum size of 2^16 bytes.
6947 size = 65000 + i
@@ -76,28 +54,24 @@ for (i = 0; i < bigArraySize; i++) {
7654}
7755bigArrayChunks . push ( new Buffer ( '\r\n' ) )
7856
79- var chunkedStringPart1 = new Buffer ( '+foobar' )
80- var chunkedStringPart2 = new Buffer ( 'bazEND\r\n' )
57+ const chunkedStringPart1 = new Buffer ( '+foobar' )
58+ const chunkedStringPart2 = new Buffer ( 'bazEND\r\n' )
8159
82- var options = {
60+ const options = {
8361 returnReply : checkReply ,
8462 returnError : returnError ,
8563 returnFatalError : returnError
8664}
87- var parser = new Parser ( options )
65+ const parser = new Parser ( options )
66+ const parserHiRedis = new HiredisParser ( options )
8867
8968options . returnBuffers = true
90- var parserBuffer = new Parser ( options )
91-
92- options . name = 'hiredis'
93- var parserHiRedisBuffer = new Parser ( options )
69+ const parserBuffer = new Parser ( options )
70+ const parserHiRedisBuffer = new HiredisParser ( options )
9471
9572delete options . returnBuffers
96- var parserHiRedis = new Parser ( options )
97-
98- delete options . name
9973options . stringNumbers = true
100- var parserStr = new Parser ( options )
74+ const parserStr = new Parser ( options )
10175
10276// BULK STRINGS
10377
@@ -109,14 +83,6 @@ suite.add('HIREDIS: $ multiple chunks in a bulk string', function () {
10983 parserHiRedis . execute ( endBuffer )
11084} )
11185
112- suite . add ( 'HIREDIS BUF: $ multiple chunks in a bulk string' , function ( ) {
113- parserHiRedisBuffer . execute ( startBuffer )
114- parserHiRedisBuffer . execute ( chunkBuffer )
115- parserHiRedisBuffer . execute ( chunkBuffer )
116- parserHiRedisBuffer . execute ( chunkBuffer )
117- parserHiRedisBuffer . execute ( endBuffer )
118- } )
119-
12086suite . add ( 'JS PARSER: $ multiple chunks in a bulk string' , function ( ) {
12187 parser . execute ( startBuffer )
12288 parser . execute ( chunkBuffer )
@@ -125,6 +91,14 @@ suite.add('JS PARSER: $ multiple chunks in a bulk string', function () {
12591 parser . execute ( endBuffer )
12692} )
12793
94+ suite . add ( 'HIREDIS BUF: $ multiple chunks in a bulk string' , function ( ) {
95+ parserHiRedisBuffer . execute ( startBuffer )
96+ parserHiRedisBuffer . execute ( chunkBuffer )
97+ parserHiRedisBuffer . execute ( chunkBuffer )
98+ parserHiRedisBuffer . execute ( chunkBuffer )
99+ parserHiRedisBuffer . execute ( endBuffer )
100+ } )
101+
128102suite . add ( 'JS PARSER BUF: $ multiple chunks in a bulk string' , function ( ) {
129103 parserBuffer . execute ( startBuffer )
130104 parserBuffer . execute ( chunkBuffer )
@@ -140,16 +114,16 @@ suite.add('\nHIREDIS: + multiple chunks in a string', function () {
140114 parserHiRedis . execute ( chunkedStringPart2 )
141115} )
142116
143- suite . add ( 'HIREDIS BUF: + multiple chunks in a string' , function ( ) {
144- parserHiRedisBuffer . execute ( chunkedStringPart1 )
145- parserHiRedisBuffer . execute ( chunkedStringPart2 )
146- } )
147-
148117suite . add ( 'JS PARSER: + multiple chunks in a string' , function ( ) {
149118 parser . execute ( chunkedStringPart1 )
150119 parser . execute ( chunkedStringPart2 )
151120} )
152121
122+ suite . add ( 'HIREDIS BUF: + multiple chunks in a string' , function ( ) {
123+ parserHiRedisBuffer . execute ( chunkedStringPart1 )
124+ parserHiRedisBuffer . execute ( chunkedStringPart2 )
125+ } )
126+
153127suite . add ( 'JS PARSER BUF: + multiple chunks in a string' , function ( ) {
154128 parserBuffer . execute ( chunkedStringPart1 )
155129 parserBuffer . execute ( chunkedStringPart2 )
@@ -165,14 +139,6 @@ suite.add('\nHIREDIS: $ 4mb bulk string', function () {
165139 parserHiRedis . execute ( endBuffer )
166140} )
167141
168- suite . add ( 'HIREDIS BUF: $ 4mb bulk string' , function ( ) {
169- parserHiRedisBuffer . execute ( startBigBuffer )
170- for ( var i = 0 ; i < 64 ; i ++ ) {
171- parserHiRedisBuffer . execute ( chunks [ i ] )
172- }
173- parserHiRedisBuffer . execute ( endBuffer )
174- } )
175-
176142suite . add ( 'JS PARSER: $ 4mb bulk string' , function ( ) {
177143 parser . execute ( startBigBuffer )
178144 for ( var i = 0 ; i < 64 ; i ++ ) {
@@ -181,6 +147,14 @@ suite.add('JS PARSER: $ 4mb bulk string', function () {
181147 parser . execute ( endBuffer )
182148} )
183149
150+ suite . add ( 'HIREDIS BUF: $ 4mb bulk string' , function ( ) {
151+ parserHiRedisBuffer . execute ( startBigBuffer )
152+ for ( var i = 0 ; i < 64 ; i ++ ) {
153+ parserHiRedisBuffer . execute ( chunks [ i ] )
154+ }
155+ parserHiRedisBuffer . execute ( endBuffer )
156+ } )
157+
184158suite . add ( 'JS PARSER BUF: $ 4mb bulk string' , function ( ) {
185159 parserBuffer . execute ( startBigBuffer )
186160 for ( var i = 0 ; i < 64 ; i ++ ) {
@@ -195,14 +169,14 @@ suite.add('\nHIREDIS: + simple string', function () {
195169 parserHiRedis . execute ( stringBuffer )
196170} )
197171
198- suite . add ( 'HIREDIS BUF: + simple string' , function ( ) {
199- parserHiRedisBuffer . execute ( stringBuffer )
200- } )
201-
202172suite . add ( 'JS PARSER: + simple string' , function ( ) {
203173 parser . execute ( stringBuffer )
204174} )
205175
176+ suite . add ( 'HIREDIS BUF: + simple string' , function ( ) {
177+ parserHiRedisBuffer . execute ( stringBuffer )
178+ } )
179+
206180suite . add ( 'JS PARSER BUF: + simple string' , function ( ) {
207181 parserBuffer . execute ( stringBuffer )
208182} )
@@ -241,14 +215,14 @@ suite.add('\nHIREDIS: * array', function () {
241215 parserHiRedis . execute ( arrayBuffer )
242216} )
243217
244- suite . add ( 'HIREDIS BUF: * array' , function ( ) {
245- parserHiRedisBuffer . execute ( arrayBuffer )
246- } )
247-
248218suite . add ( 'JS PARSER: * array' , function ( ) {
249219 parser . execute ( arrayBuffer )
250220} )
251221
222+ suite . add ( 'HIREDIS BUF: * array' , function ( ) {
223+ parserHiRedisBuffer . execute ( arrayBuffer )
224+ } )
225+
252226suite . add ( 'JS PARSER BUF: * array' , function ( ) {
253227 parserBuffer . execute ( arrayBuffer )
254228} )
@@ -261,15 +235,15 @@ suite.add('\nHIREDIS: * big nested array', function () {
261235 }
262236} )
263237
264- suite . add ( 'HIREDIS BUF : * big nested array' , function ( ) {
238+ suite . add ( 'JS PARSER : * big nested array' , function ( ) {
265239 for ( var i = 0 ; i < bigArrayChunks . length ; i ++ ) {
266- parserHiRedisBuffer . execute ( bigArrayChunks [ i ] )
240+ parser . execute ( bigArrayChunks [ i ] )
267241 }
268242} )
269243
270- suite . add ( 'JS PARSER : * big nested array' , function ( ) {
244+ suite . add ( 'HIREDIS BUF : * big nested array' , function ( ) {
271245 for ( var i = 0 ; i < bigArrayChunks . length ; i ++ ) {
272- parser . execute ( bigArrayChunks [ i ] )
246+ parserHiRedisBuffer . execute ( bigArrayChunks [ i ] )
273247 }
274248} )
275249
0 commit comments