forked from dagrejs/graphlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.js
executable file
·170 lines (139 loc) · 4.13 KB
/
bench.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
var Benchmark = require("benchmark"),
seedrandom = require("seedrandom"),
sprintf = require("sprintf").sprintf;
var seed = process.env.SEED;
seedrandom(seed, { global: true });
if (seed) {
console.log("SEED: %s (%d)", seed, Math.random());
}
var Graph = require("..").Graph,
alg = require("..").alg;
var NODE_SIZES = [100],
EDGE_DENSITY = 0.2,
KEY_SIZE = 10;
function runBenchmark(name, fn) {
var options = {};
options.onComplete = function(bench) {
var target = bench.target,
hz = target.hz,
stats = target.stats,
rme = stats.rme,
samples = stats.sample.length,
msg = sprintf(" %25s: %13s ops/sec \xb1 %s%% (%3d run(s) sampled)",
target.name,
Benchmark.formatNumber(hz.toFixed(2)),
rme.toFixed(2),
samples);
console.log(msg);
};
options.onError = function(bench) {
console.error(" " + bench.target.error);
};
options.setup = function() {
this.count = Math.random() * 1000;
this.nextInt = function(range) {
return Math.floor(this.count++ % range );
};
};
new Benchmark(name, fn, options).run();
}
function keys(count) {
var ks = [],
k;
for (var i = 0; i < count; ++i) {
k = "";
for (var j = 0; j < KEY_SIZE; ++j) {
k += String.fromCharCode(97 + Math.floor(Math.random() * 26));
}
ks.push(k);
}
return ks;
}
function buildGraph(numNodes, edgeDensity) {
var g = new Graph(),
numEdges = numNodes * numNodes * edgeDensity,
ks = keys(numNodes);
ks.forEach(function(k) { g.setNode(k); });
for (var i = 0; i < numEdges; ++i) {
var v, w;
do {
v = ks[Math.floor(Math.random() * ks.length)];
w = ks[Math.floor(Math.random() * ks.length)];
} while (g.hasEdge(v, w));
g.setEdge(v, w);
}
return g;
}
NODE_SIZES.forEach(function(size) {
var g = buildGraph(size, EDGE_DENSITY),
nodes = g.nodes(),
edges = g.edges(),
nameSuffix = "(" + size + "," + EDGE_DENSITY + ")";
runBenchmark("nodes" + nameSuffix, function() {
g.nodes();
});
runBenchmark("sources" + nameSuffix, function() {
g.sources();
});
runBenchmark("sinks" + nameSuffix, function() {
g.sinks();
});
runBenchmark("filterNodes all" + nameSuffix, function() {
g.filterNodes(function() { return true; });
});
runBenchmark("filterNodes none" + nameSuffix, function() {
g.filterNodes(function() { return false; });
});
runBenchmark("setNode" + nameSuffix, function() {
g.setNode("key", "label");
});
runBenchmark("node" + nameSuffix, function() {
g.node(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("set + removeNode" + nameSuffix, function() {
g.setNode("key");
g.removeNode("key");
});
runBenchmark("predecessors" + nameSuffix, function() {
g.predecessors(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("successors" + nameSuffix, function() {
g.successors(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("neighbors" + nameSuffix, function() {
g.neighbors(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("edges" + nameSuffix, function() {
g.edges();
});
runBenchmark("setPath" + nameSuffix, function() {
g.setPath(["a", "b", "c", "d", "e"]);
});
runBenchmark("setEdge" + nameSuffix, function() {
g.setEdge("from", "to", "label");
});
runBenchmark("edge" + nameSuffix, function() {
var edge = edges[this.nextInt(edges.length)];
g.edge(edge);
});
runBenchmark("set + removeEdge" + nameSuffix, function() {
g.setEdge("from", "to");
g.removeEdge("from", "to");
});
runBenchmark("inEdges" + nameSuffix, function() {
g.inEdges(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("outEdges" + nameSuffix, function() {
g.outEdges(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("nodeEdges" + nameSuffix, function() {
g.nodeEdges(nodes[this.nextInt(nodes.length)]);
});
runBenchmark("components" + nameSuffix, function() {
alg.components(g);
});
runBenchmark("dijkstraAll" + nameSuffix, function() {
alg.dijkstraAll(g);
});
});