-
Notifications
You must be signed in to change notification settings - Fork 30
/
measurePerformance.js
105 lines (85 loc) · 2.43 KB
/
measurePerformance.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
const randomAPI = require('ngraph.random').random;
const graphPath = require('ngraph.path');
const loadGraph = require('./lib/loadGraph');
loadGraph('./data/USA-road-d.NY.co', './data/USA-road-d.NY.gr')
.then(g => {
console.log('graph loaded: ' + g.getLinksCount() + ' links; ' + g.getNodesCount() + ' nodes')
runPerfTests(g);
})
function runPerfTests(graph) {
var V = graph.getNodesCount();
var testCount = 250;
var aStarBI = graphPath.aGreedy(graph, {
distance: distance,
heuristic: distance
});
runTests('A* greedy suboptimal', aStarBI, V, testCount);
var nbaGreedy = graphPath.nba(graph, {
quitFast: true,
distance: distance,
heuristic: distance
});
runTests('NBA* greedy suboptimal', nbaGreedy, V, testCount);
var aStarUni = graphPath.aStar(graph, {
distance: distance,
heuristic: distance
});
runTests('A* unidirectional', aStarUni, V, testCount);
var nba = graphPath.nba(graph, {
distance: distance,
heuristic: distance
});
runTests('NBA*', nba, V, testCount);
var dijkstra = graphPath.aStar(graph, {
distance: distance,
heuristic() { return 0; }
});
runTests('Dijkstra', dijkstra, V, testCount);
}
function runTests(name, pathFinder, V, testCount) {
var random = randomAPI(42);
var durations = []
for (var i = 0; i < testCount; ++i) {
var fromId = random.next(V);
var toId = random.next(V);
if (fromId === toId) {
i -= 1;
continue;
}
var start = clock();
pathFinder.find(fromId, toId);
var duration = clock(start);
durations.push(duration);
}
var stats = collectStats(durations);
console.log('Finished ' + name +'; Stats: ');
console.log(JSON.stringify(stats));
}
function collectStats(testResults) {
var sum = 0;
testResults.sort((a, b) => a - b);
for (var i = 0; i < testResults.length; ++i) {
sum += testResults[i];
}
var n = testResults.length;
return {
avg: sum/n,
p99: testResults[Math.floor(n * 0.99)],
p90: testResults[Math.floor(n * 0.9)],
p50: testResults[Math.floor(n * 0.5)],
min: testResults[0],
max: testResults[testResults.length - 1]
}
}
function distance(a, b) {
let aPos = a.data;
let bPos = b.data;
let dx = aPos.x - bPos.x;
let dy = aPos.y - bPos.y;
return Math.sqrt(dx * dx + dy * dy)
}
function clock(start) {
if ( !start ) return process.hrtime();
var end = process.hrtime(start);
return Math.round((end[0]*1000) + (end[1]/1000000));
}