-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
80 lines (65 loc) · 1.94 KB
/
index.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
'use strict'
var util = require('util')
var EventEmitter = require('events').EventEmitter
var xtend = require('xtend')
var request = require('request')
var weighted = require('weighted')
var maybe = require('mostly-working-hours')
var expand = require('brace-expansion')
var weekend = require('is-it-weekend')
var pkg = require('./package')
var USER_AGENT = pkg.name + '/' + pkg.version
module.exports = Workload
function Workload (opts) {
if (!(this instanceof Workload)) return new Workload(opts)
EventEmitter.call(this)
var self = this
var interval = Math.round(1000 / ((opts.max || 12) / 60)) // default to max 12 requests per minute
var filters = opts.filters || [opts.filter || function (_, cb) { cb() }]
this._defaultHeaders = opts.headers
var weights = opts.requests.map(function (req) {
return req.weight || 1
})
this._timer = setInterval(function () {
var req = xtend({}, weighted.select(opts.requests, weights))
iterator(req)
}, interval)
function iterator (req, n) {
if (!n) n = 0
var filter = filters[n]
if (!filter) return self._visit(req)
filter(req, function (modified) {
iterator(modified || req, ++n)
})
}
}
util.inherits(Workload, EventEmitter)
Workload.stdFilters = {
workdays: function (req, cb) {
var odds = weekend() ? 0.2 : 1
if (Math.random() <= odds) cb()
},
workingHours: function (req, cb) {
maybe(cb)
},
expand: function (req, cb) {
var urls = expand(req.url)
req.url = urls[Math.round(Math.random() * (urls.length - 1))]
cb()
}
}
Workload.prototype.stop = function stop () {
clearInterval(this._timer)
}
Workload.prototype._visit = function _visit (req) {
var self = this
req.headers = xtend({'user-agent': USER_AGENT}, this._defaultHeaders, req.headers)
request(req, function (err, res, body) {
if (err) return self.emit('error', err)
self.emit('visit', {
request: req,
response: res,
body: body
})
})
}