-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·146 lines (129 loc) · 4.42 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
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
var events = require('events')
var path = require('path')
var io = require('socket.io-client')
var defaults = require('./defaults')
var _intervals = []
var done
var emitter = new events.EventEmitter()
var clientsAttempted = 0
/**
* Create a websocket client and set it off at the server
* @method createClient
* @private
* @param {string} host Hostname
* @param {integer} port Port number
* @param {integer} concurrent Number of clients to spin up
* @param {integer} frequency Amount of time, in ms, to wait between emitting messages
* @param {integer} duration Amoutn of time, in ms, to run each client for
* @param {object} gen The generator object, required and parse
* @param {integer} iteration What client number you're on
* @returns {object} undefined
*/
function createClient (host, port, concurrent, frequency, duration, gen, iteration) {
var auth = defaults.auth
var getMessage = defaults.getMessage
emitter.emit('start')
if (typeof gen.authenticate === 'function') {
auth = gen.authenticate
}
if (typeof gen.clientIterateMessage === 'function') {
getMessage = gen.clientIterateMessage
}
/**
* Once auth is complete, this actually initiates the client
* @method postAuth
* @async
* @private
* @param {object} err Error from auth, if any
* @param {object} cookies Any cookies to pass through to the socket object
* @param {string} user The username used to login
* @param {string} pass The password used to login
* @returns {object} undefined
*/
var postAuth = function (err, cookies, user, pass) {
++clientsAttempted
if (err) {
emitter.emit('error', err)
if (clientsAttempted === concurrent && _intervals.length === 0) {
emitter.emit('end')
}
return
}
var socketUrl = gen.getSocketURL(host, port, cookies, user, pass) || host + ':' + port
var socket = io(socketUrl, { multiplex: false })
.on('connect', function () {
emitter.emit('client-connected')
if (typeof gen.events.connect === 'function') {
gen.events.connect('connect', cookies, user, pass, {}, socket, emitter)
}
Object.keys(gen.events).forEach(function (eventName) {
socket.on(eventName, function (data) {
gen.events[eventName].call(null, eventName, cookies, user, pass, data, socket, emitter)
})
})
var sendMessage = function () {
var message = getMessage(cookies, user, pass)
if (!Array.isArray(message)) {
message = [message]
}
for (var i = 0, len = message.length; i < len; i++) {
if (message[i]) {
socket.json.send(message[i])
emitter.emit('message', message[i])
}
}
}
_intervals.push(setInterval(sendMessage, frequency))
setTimeout(function () {
clearInterval(_intervals.pop())
socket.emit('disconnect')
emitter.emit('disconnect')
socket.close()
if (_intervals.length === 0) {
done()
}
}, duration)
})
.on('connect_error', function (err) {
emitter.emit('error', err)
if (clientsAttempted === concurrent && _intervals.length === 0) {
emitter.emit('end')
}
})
}
auth(host, port, iteration, postAuth)
}
/**
* Generate a swarm of socket clients
* @method exports
* @param {string} host hostname to swarm
* @param {integer} port port to swarm
* @param {integer} concurrent number of clients to stat
* @param {integer} frequency amount of time, in ms, to wait between sending sockets
* @param {integer} duration amount of time, in ms, to run the each client for
* @param {string} generator The path to the file containing the generator exports
* @returns {object} event emitter
*/
module.exports = function (host, port, concurrent, frequency, duration, generator, cb) {
clientsAttempted = 0
done = function () {
emitter.emit('end')
if (typeof cb === 'function') {
cb()
}
}
var gen = generator || {}
if (typeof generator === 'string') {
try {
gen = require(path.resolve(process.cwd(), generator))
} catch (e) {
console.log('Could not load generator', generator)
console.log(e)
process.exit(1)
}
}
for (var i = 0; i < concurrent; i++) {
createClient(host, port, concurrent, frequency, duration, gen, i)
}
return emitter
}