-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdep_graph.rs
More file actions
124 lines (112 loc) · 3.84 KB
/
Copy pathdep_graph.rs
File metadata and controls
124 lines (112 loc) · 3.84 KB
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
use criterion::{criterion_group, criterion_main, Criterion};
use dep_graph::{DepGraph, Node};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use std::hint::black_box;
use std::thread;
use std::time::Duration;
/// Create a layer of nodes that don't have any dependencies
fn root_layer(count: usize) -> Vec<Node<String>> {
(0..count)
.map(|i| Node::new(format!("node0_{}", i)))
.collect()
}
/// Utility function that adds a layer of nodes depending on the provided
/// layer and returns them.
fn add_layer(index: usize, count: usize) -> Vec<Node<String>> {
(0..count)
.map(|i| {
let mut node = Node::new(format!("node{}_{}", index, i));
// Mark the entire previous layer as a dependency of this node
for j in 0..count {
node.add_dep(format!("node{}_{}", index - 1, j));
}
node
})
.collect()
}
pub fn parallel_benchmark(c: &mut Criterion) {
const NUM_LAYERS: usize = 20;
#[cfg(feature = "parallel")]
fn par_no_op(nodes: &[Node<String>]) {
DepGraph::new(nodes)
.into_par_iter()
.for_each(|_node| thread::sleep(Duration::from_nanos(100)))
}
fn seq_no_op(nodes: &[Node<String>]) {
DepGraph::new(nodes)
.into_iter()
.for_each(|_node| thread::sleep(Duration::from_nanos(100)))
}
{
// Create a graph with the same number of nodes per layer as the number
// of cores.
let count = num_cpus::get();
let mut nodes = root_layer(count);
(1..NUM_LAYERS).for_each(|i| {
add_layer(i, count)
.iter()
.for_each(|node| nodes.push(node.clone()))
});
// Run the resolver
#[cfg(feature = "parallel")]
c.bench_function("par_same_nodes", |b| {
b.iter(|| par_no_op(black_box(&nodes)))
});
c.bench_function("seq_same_nodes", |b| {
b.iter(|| seq_no_op(black_box(&nodes)))
});
}
{
// Create a graph with a graph twice as broad but half as deep
let count = num_cpus::get();
let mut nodes = root_layer(count * 2);
(1..NUM_LAYERS / 2).for_each(|i| {
add_layer(i, count * 2)
.iter()
.for_each(|node| nodes.push(node.clone()))
});
// Run the resolver
#[cfg(feature = "parallel")]
c.bench_function("par_double_nodes", |b| {
b.iter(|| par_no_op(black_box(&nodes)))
});
c.bench_function("seq_double_nodes", |b| {
b.iter(|| seq_no_op(black_box(&nodes)))
});
}
{
// Create a graph with a graph half as broad but twice as deep
let count = num_cpus::get();
let mut nodes = root_layer(count / 2);
(1..NUM_LAYERS * 2).for_each(|i| {
add_layer(i, count / 2)
.iter()
.for_each(|node| nodes.push(node.clone()))
});
// Run the resolver
#[cfg(feature = "parallel")]
c.bench_function("par_half_nodes", |b| {
b.iter(|| par_no_op(black_box(&nodes)))
});
c.bench_function("seq_half_nodes", |b| {
b.iter(|| seq_no_op(black_box(&nodes)))
});
}
{
// Create a graph with 100 nodes per layer
let count = 100;
let mut nodes = root_layer(count);
(1..NUM_LAYERS).for_each(|i| {
add_layer(i, count)
.iter()
.for_each(|node| nodes.push(node.clone()))
});
// Run the resolver
#[cfg(feature = "parallel")]
c.bench_function("par_100_nodes", |b| b.iter(|| par_no_op(black_box(&nodes))));
c.bench_function("seq_100_nodes", |b| b.iter(|| seq_no_op(black_box(&nodes))));
}
}
criterion_group!(benches, parallel_benchmark);
criterion_main!(benches);