-
Notifications
You must be signed in to change notification settings - Fork 19
/
siege.js
342 lines (283 loc) · 6.79 KB
/
siege.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var util = require('util')
, child_process = require('child_process')
, http = require('http')
, path = require('path')
, fs = require('fs')
, siege_attack = require('./siege_attack')
, __slice = Array.prototype.slice
;
var Siege = function(path, options) {
if(!options) {
if(typeof path == 'object') {
options = path
path = undefined
} else {
options = {}
}
}
if(path) {
options.serve = path
}
options.tasks = []
this.tasks = []
this.options = options
this.current_task = null
var self = this
this.__defineGetter__('withCookie', function() {
self.options.enableCookie = true
return self
})
this.__defineGetter__('with304', function() {
self.options.enable304 = true
return self
})
this.__defineGetter__('withoutCookie', function() {
self.options.enableCookie = false
return self
})
this.__defineGetter__('without304', function() {
self.options.enable304 = false
return self
})
}
var siege = Siege.prototype
siege.on = function(port) {
if(/\d+/.test(port))
this.options.port = port
else
this.options.sockpath = port
return this
}
siege.host = function(host) {
this.options.host = host
return this
}
siege.hostname = function(hostname) {
this.options.hostname = hostname
return this
}
siege.rejectUnauthorized = function(rejectUnauthorized) {
this.options.rejectUnauthorized = rejectUnauthorized
return this
}
siege.requestCert = function(requestCert) {
this.options.requestCert = requestCert
return this
}
siege.agent = function(agent) {
this.options.agent = agent
return this
}
siege.sslProtocol = function(sslProtocol) {
this.options.sslProtocol = sslProtocol
return this
}
siege.concurrent = function(concurrent) {
this.options.concurrent = concurrent
return this
}
siege.describe = function(description) {
var task = this.current_task
if(!task || task.options.description) {
task = this._newtask()
}
task.options.description = description
return task
}
siege.wait = function(wait) {
this.options.wait = wait
return this
}
siege._newtask = function() {
var task = new Task(this)
this.tasks.push(task)
this.current_task = task
return task
}
siege.request = function(options) {
var task = this.current_task
if(!task || task.options.method) {
task = this._newtask()
}
task.request(options)
return task
}
siege.get = function(url, query) {
return this.request({
path: url
, method: 'GET'
, query: query
})
}
siege.post = function(url, body) {
return this.request({
path: url
, method: 'POST'
, body: body
})
}
siege.set = function(name, value) {
this.options.headers = this.options.headers || {}
this.options.headers[name] = value
return this
}
siege.for = function(num) {
return new For(this, num)
}
siege.report = function(options) {
this.options.report = options
return this
}
siege.attack = function() {
var options = this.options
this.tasks.forEach(function(task){
options.tasks.push(task.options)
})
if(!options.port && !options.sockpath) {
options.sockpath = '/tmp/siege.sock'
}
queueSiege(this.options)
}
function SiegeWrap(siege) {
this.siege = siege
}
var wrap = SiegeWrap.prototype
// default wrap
;['request', 'get', 'post', 'attack', 'describe'].forEach(function(name){
if(name[0] != '_') wrap[name] = function() {
var args = __slice.call(arguments)
return siege[name].apply(this.siege, args)
}
})
function Task(siege) {
this.siege = siege
this.options = {}
var self = this
this.__defineGetter__('withCookie', function() {
self.options.enableCookie = true
return self
})
this.__defineGetter__('with304', function() {
self.options.enable304 = true
return self
})
this.__defineGetter__('withoutCookie', function() {
self.options.enableCookie = false
return self
})
this.__defineGetter__('without304', function() {
self.options.enable304 = false
return self
})
}
util.inherits(Task, SiegeWrap)
var task = Task.prototype
task.for = function(num) {
return new For(this, num)
}
task.request = function(options) {
options.method = options.method || 'GET'
merge(this.options, options)
}
task.concurrent = function(concurrent) {
this.options.concurrent = concurrent
return this
}
/**
* options
* rps : true
* status: true
* timeout: true
* responseTime: ['max', 'min', 'max', 'most']
* weight: true
* graph: {
* dot: /path/to/dot.png
* line: /path/to/line.png // timebase
* c2mem: /path/to/c2mem.png // concurrent to memoryUsage
* }
* type: json | plain | color
*
*/
task.report = function(options) {
this.options.report = options
}
function For(main, num) {
this.__defineGetter__('seconds', function() {
main.options.duration = num * 1000
return main
})
this.__defineGetter__('times', function() {
main.options.repeat = num
return main
})
}
function merge(target, source) {
for(var name in source) {
target[name] = source[name]
}
}
var queue = [], started = false;
function queueSiege(options) {
queue.push(options)
if(!started) {
started = true
nextSiege()
}
}
function nextSiege() {
var options = queue.shift()
if(!options) {
process.exit()
}
startSiege(options, nextSiege)
}
function startSiege(options, callback) {
startServe(options, function(child){
options.taskIndex = -1
var attack = siege_attack(options, endProgram)
function endProgram(){
if(child) child.kill()
attack.halt()
callback()
}
process.on('SIGINT', endProgram)
process.on('uncaughtException', function(err) {
console.log(err.stack)
endProgram()
})
})
}
function startServe(options, callback) {
var serve = options.serve;
if(!serve) return callback();
console.log('starting ' + serve);
var cmd = serve
var args = []
try{
require(serve)
cmd = 'node'
args = [__dirname + '/siege_server.js', '--port', options.sockpath || options.port || '/tmp/siege.sock', serve]
} catch(e){
cmd = cmd.split(/\s+/g)
args = cmd.slice(1)
cmd = cmd[0]
}
var child = child_process.spawn(cmd, args)
var log = path.join(process.cwd(), options.serverlog || 'server.log')
var errlog = path.join(process.cwd(), options.servererrlog || 'server.error.log')
child.stdout.pipe(fs.createWriteStream(log))
child.stderr.pipe(process.stderr)
var wait = options.wait || 200
process.stdout.write('\033[?25l')
var timer = setInterval(function(){
process.stdout.write('.')
}, wait / 30)
setTimeout(function() {
clearInterval(timer)
process.stdout.write('\r\033[K')
callback(child)
}, wait);
}
var exports = module.exports = function(path, options) {
return new Siege(path, options)
}