@@ -10,28 +10,28 @@ suite.test('pipeline mode basic functionality', (cb) => {
10
10
const client = new pg . Client ( helper . config )
11
11
client . connect ( ( err ) => {
12
12
if ( err ) return cb ( err )
13
-
13
+
14
14
// Initially not in pipeline mode
15
15
assert . equal ( client . pipelineStatus ( ) , 'PIPELINE_OFF' )
16
16
assert . equal ( client . pipelining , false )
17
-
17
+
18
18
// Enable pipeline mode
19
19
client . pipelining = true
20
20
assert . equal ( client . pipelineStatus ( ) , 'PIPELINE_ON' )
21
21
assert . equal ( client . pipelining , true )
22
-
22
+
23
23
// Disable pipeline mode
24
24
client . pipelining = false
25
25
assert . equal ( client . pipelineStatus ( ) , 'PIPELINE_OFF' )
26
26
assert . equal ( client . pipelining , false )
27
-
27
+
28
28
client . end ( cb )
29
29
} )
30
30
} )
31
31
32
32
suite . test ( 'cannot enable pipeline before connection' , ( cb ) => {
33
33
const client = new pg . Client ( helper . config )
34
-
34
+
35
35
try {
36
36
client . pipelining = true
37
37
cb ( new Error ( 'Should have thrown error' ) )
@@ -45,34 +45,34 @@ suite.test('pipeline mode with multiple queries', (cb) => {
45
45
const client = new pg . Client ( helper . config )
46
46
client . connect ( ( err ) => {
47
47
if ( err ) return cb ( err )
48
-
48
+
49
49
client . pipelining = true
50
-
50
+
51
51
let results = [ ]
52
52
let completed = 0
53
-
53
+
54
54
// Send multiple queries in pipeline mode
55
55
client . query ( 'SELECT 1 as num' , ( err , res ) => {
56
56
if ( err ) return cb ( err )
57
57
results [ 0 ] = res . rows [ 0 ] . num
58
58
completed ++
59
59
if ( completed === 3 ) checkResults ( )
60
60
} )
61
-
61
+
62
62
client . query ( 'SELECT 2 as num' , ( err , res ) => {
63
63
if ( err ) return cb ( err )
64
64
results [ 1 ] = res . rows [ 0 ] . num
65
65
completed ++
66
66
if ( completed === 3 ) checkResults ( )
67
67
} )
68
-
68
+
69
69
client . query ( 'SELECT 3 as num' , ( err , res ) => {
70
70
if ( err ) return cb ( err )
71
71
results [ 2 ] = res . rows [ 0 ] . num
72
72
completed ++
73
73
if ( completed === 3 ) checkResults ( )
74
74
} )
75
-
75
+
76
76
function checkResults ( ) {
77
77
assert . equal ( results [ 0 ] , 1 )
78
78
assert . equal ( results [ 1 ] , 2 )
@@ -86,9 +86,9 @@ suite.test('pipeline mode rejects simple query protocol', (cb) => {
86
86
const client = new pg . Client ( helper . config )
87
87
client . connect ( ( err ) => {
88
88
if ( err ) return cb ( err )
89
-
89
+
90
90
client . pipelining = true
91
-
91
+
92
92
try {
93
93
client . query ( 'SELECT 1' , ( err , res ) => {
94
94
// This should not be called
@@ -105,9 +105,9 @@ suite.test('pipeline mode rejects multiple SQL commands', (cb) => {
105
105
const client = new pg . Client ( helper . config )
106
106
client . connect ( ( err ) => {
107
107
if ( err ) return cb ( err )
108
-
108
+
109
109
client . pipelining = true
110
-
110
+
111
111
try {
112
112
client . query ( { text : 'SELECT 1; SELECT 2;' } , ( err , res ) => {
113
113
// This should not be called
@@ -124,27 +124,27 @@ suite.test('pipeline mode with parameterized queries', (cb) => {
124
124
const client = new pg . Client ( helper . config )
125
125
client . connect ( ( err ) => {
126
126
if ( err ) return cb ( err )
127
-
127
+
128
128
client . pipelining = true
129
-
129
+
130
130
let results = [ ]
131
131
let completed = 0
132
-
132
+
133
133
// Send parameterized queries in pipeline mode
134
134
client . query ( { text : 'SELECT $1::int as num' , values : [ 10 ] } , ( err , res ) => {
135
135
if ( err ) return cb ( err )
136
136
results [ 0 ] = res . rows [ 0 ] . num
137
137
completed ++
138
138
if ( completed === 2 ) checkResults ( )
139
139
} )
140
-
140
+
141
141
client . query ( { text : 'SELECT $1::text as str' , values : [ 'hello' ] } , ( err , res ) => {
142
142
if ( err ) return cb ( err )
143
143
results [ 1 ] = res . rows [ 0 ] . str
144
144
completed ++
145
145
if ( completed === 2 ) checkResults ( )
146
146
} )
147
-
147
+
148
148
function checkResults ( ) {
149
149
assert . equal ( results [ 0 ] , 10 )
150
150
assert . equal ( results [ 1 ] , 'hello' )
@@ -157,9 +157,9 @@ suite.test('pipeline mode performance benefit', (cb) => {
157
157
const client = new pg . Client ( helper . config )
158
158
client . connect ( ( err ) => {
159
159
if ( err ) return cb ( err )
160
-
160
+
161
161
const numQueries = 10
162
-
162
+
163
163
// Test without pipeline mode
164
164
const startNormal = Date . now ( )
165
165
let normalCompleted = 0
@@ -176,29 +176,29 @@ suite.test('pipeline mode performance benefit', (cb) => {
176
176
} )
177
177
}
178
178
}
179
-
179
+
180
180
function runPipelineQueries ( normalTime ) {
181
181
client . pipelining = true
182
182
const startPipeline = Date . now ( )
183
183
let pipelineCompleted = 0
184
-
184
+
185
185
for ( let i = 0 ; i < numQueries ; i ++ ) {
186
186
client . query ( { text : 'SELECT $1::int as num' , values : [ i ] } , ( err , res ) => {
187
187
if ( err ) return cb ( err )
188
188
pipelineCompleted ++
189
189
if ( pipelineCompleted === numQueries ) {
190
190
const pipelineTime = Date . now ( ) - startPipeline
191
-
191
+
192
192
// Pipeline should be faster or at least not significantly slower
193
193
// In real network conditions with latency, pipeline would show more benefit
194
194
console . log ( `Normal mode: ${ normalTime } ms, Pipeline mode: ${ pipelineTime } ms` )
195
-
195
+
196
196
client . end ( cb )
197
197
}
198
198
} )
199
199
}
200
200
}
201
-
201
+
202
202
runNormalQueries ( )
203
203
} )
204
204
} )
0 commit comments