-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
68 lines (51 loc) · 1.63 KB
/
producer.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
'use strict';
var requestsPerSecond = process.env.REQS_PER_SECOND || 10;
var consumerURL = process.env.CONSUMER_URL || 'http://localhost:3000/compute';
var MathRandomizer = require('./producer/math-randomizer');
var agent = require('./producer/http-agent');
/**
* Perform the POST request with a random math expression
*/
var post = function() {
// NOTE: ordinarily, I'd use superagent to make this request, but I wrote it using vanilla Node
// in order to keep external dependencies low and remain true to the spirit of the test
agent.post({
url: consumerURL,
headers: {
'Content-Type': 'text/plain'
},
body: MathRandomizer.randomMathExpression()
});
};
// the loop
var loop;
module.exports = {
/**
* Starts the producer, making requests at X reqs/s
*/
start: function() {
// make one request immediately
post();
// start looping requests
loop = setInterval(post, 1000/requestsPerSecond);
console.log('---------------------------------');
console.log('PRODUCER STARTED');
console.log(' - URL: ' + consumerURL);
console.log(' - Reqs/sec: ' + requestsPerSecond);
console.log('---------------------------------');
},
/**
* Stops the producer
*/
stop: function() {
clearTimeout(loop);
console.log('');
console.log('---------------------------------');
console.log('PRODUCER STOPPED');
console.log('---------------------------------');
}
};
process.on('SIGINT', function() {
module.exports.stop();
});
module.exports.start();