-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxtress.js
69 lines (55 loc) · 1.81 KB
/
xtress.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
const { performance } = require('perf_hooks');
const { Tree } = require('./lib/tree');
const axios = require('axios');
const util = require('./lib/util');
const Extress = {
tree: new Tree(),
map: app => {
function buildTree(stack) {
stack.forEach(endpoint => (endpoint.route ? Extress.tree.add(endpoint.route) : null));
}
buildTree(app._router.stack);
axios.post('http://localhost:4050/tree', Extress.tree); //put this onto client side
},
durationObj: {},
startTime: null,
reset: () => {
Extress.durationObj = {};
Extress.startTime = null;
},
routeTimer: (req, res, next) => {
const start = performance.now();
res.once('finish', () => {
//const performanceNode = Extress.tree.findBFS(req.originalUrl);
// Extress.tree.addPerformance(performanceNode, req.method.toLowerCase(), performance.now() - start);
const endTime = performance.now()
const testId = req.headers.testid
let duration = endTime - start;
if (Extress.durationObj[testId]) {
Extress.durationObj[testId].push(duration)
} else {
Extress.durationObj[testId] = [duration]
}
if (req.headers.xtressend) {
const { min, max, avg } = util.calculatePerfData(Extress.durationObj[testId]);
const testData = {
method: req.method,
route: req.originalUrl,
min,
max,
avg,
}
axios
.post('http://localhost:4050/finished', testData)
.then(() => {
console.log('Final request processed, sending post to Xtress server to rerender tree');
// Reset perfData before it performing another test...
Extress.reset();
})
.catch(error => console.error(error));
}
});
next();
}
};
module.exports = Extress;