Skip to content

Commit 200c362

Browse files
authored
benches: move to criterion-rs and GitHub actions (#366)
This moves all benches to criterion-rs, in order to allow running benchmarks on a stable toolchain. This also moves benchmarking and linting jobs to a pinned toolchain, under GitHub actions. Signed-off-by: Luca BRUNO <[email protected]>
1 parent 8af4f51 commit 200c362

File tree

11 files changed

+313
-201
lines changed

11 files changed

+313
-201
lines changed

.github/workflows/rust.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
name: Rust
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
# Pinned toolchain for linting and benchmarks
12+
ACTIONS_LINTS_TOOLCHAIN: 1.47.0
13+
14+
jobs:
15+
linting:
16+
name: "Lints, pinned toolchain"
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
- name: Install toolchain
22+
uses: actions-rs/toolchain@v1
23+
with:
24+
toolchain: ${{ env['ACTIONS_LINTS_TOOLCHAIN'] }}
25+
default: true
26+
components: rustfmt, clippy
27+
- name: cargo fmt (check)
28+
run: cargo fmt --all -- --check -l
29+
- name: cargo clippy
30+
run: cargo clippy --all -- -D clippy
31+
criterion:
32+
name: "Benchmarks (criterion)"
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v2
37+
- name: Install toolchain
38+
uses: actions-rs/toolchain@v1
39+
with:
40+
toolchain: ${{ env['ACTIONS_LINTS_TOOLCHAIN'] }}
41+
default: true
42+
- name: cargo bench (prometheus)
43+
run: cargo bench -p prometheus
44+
- name: cargo bench (prometheus-static-metric)
45+
run: cargo bench -p prometheus-static-metric

.travis.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ matrix:
2929
env: FEATURES="nightly protobuf push process"
3030
- rust: beta
3131
- rust: stable
32-
install:
33-
- rustup component add clippy-preview
34-
- rustup component add rustfmt-preview
35-
before_script:
36-
- cargo fmt --all -- --check
37-
- cargo clippy --all -- -D clippy
3832
allow_failures:
3933
- rust: nightly
4034

@@ -44,8 +38,4 @@ script:
4438
if [ $TRAVIS_RUST_VERSION = 'nightly' ]; then
4539
cargo build -p prometheus-static-metric --examples --no-default-features --features="$FEATURES"
4640
cargo test -p prometheus-static-metric --no-default-features --features="$FEATURES"
47-
48-
# Run benchmarks.
49-
cargo bench --no-default-features --features="$FEATURES"
50-
cargo bench -p prometheus-static-metric --no-default-features --features="$FEATURES"
5141
fi

Cargo.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ thiserror = "^1.0"
3939
procfs = { version = "^0.8", optional = true, default-features = false }
4040

4141
[dev-dependencies]
42+
criterion = "0.3"
4243
getopts = "^0.2"
4344
hyper = "^0.13"
4445
tokio = { version = "^0.3", features = ["macros", "rt-multi-thread"] }
@@ -48,3 +49,27 @@ protobuf-codegen-pure = { version = "^2.0", optional = true }
4849

4950
[workspace]
5051
members = ["static-metric"]
52+
53+
[[bench]]
54+
name = "atomic"
55+
harness = false
56+
57+
[[bench]]
58+
name = "counter"
59+
harness = false
60+
61+
[[bench]]
62+
name = "desc"
63+
harness = false
64+
65+
[[bench]]
66+
name = "gauge"
67+
harness = false
68+
69+
[[bench]]
70+
name = "histogram"
71+
harness = false
72+
73+
[[bench]]
74+
name = "text_encoder"
75+
harness = false

benches/atomic.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,26 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
#![feature(test)]
14+
use criterion::{criterion_group, criterion_main, Criterion};
15+
use prometheus::core::*;
1516

16-
extern crate test;
17-
18-
#[path = "../src/atomic64.rs"]
19-
mod atomic64;
20-
21-
use crate::atomic64::*;
22-
use test::Bencher;
23-
24-
#[bench]
25-
fn bench_atomic_f64(b: &mut Bencher) {
17+
fn bench_atomic_f64(c: &mut Criterion) {
2618
let val = AtomicF64::new(0.0);
27-
b.iter(|| {
28-
val.inc_by(12.0);
19+
c.bench_function("atomic_f64", |b| {
20+
b.iter(|| {
21+
val.inc_by(12.0);
22+
})
2923
});
3024
}
3125

32-
#[bench]
33-
fn bench_atomic_i64(b: &mut Bencher) {
26+
fn bench_atomic_i64(c: &mut Criterion) {
3427
let val = AtomicI64::new(0);
35-
b.iter(|| {
36-
val.inc_by(12);
28+
c.bench_function("atomic_i64", |b| {
29+
b.iter(|| {
30+
val.inc_by(12);
31+
})
3732
});
3833
}
34+
35+
criterion_group!(benches, bench_atomic_f64, bench_atomic_i64);
36+
criterion_main!(benches);

benches/counter.rs

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,42 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
#![feature(test)]
15-
16-
extern crate test;
17-
14+
use criterion::{criterion_group, criterion_main, Criterion};
15+
use prometheus::{Counter, CounterVec, IntCounter, Opts};
1816
use std::collections::HashMap;
1917
use std::sync::{atomic, Arc};
2018
use std::thread;
2119

22-
use prometheus::{Counter, CounterVec, IntCounter, Opts};
23-
use test::Bencher;
24-
25-
#[bench]
26-
fn bench_counter_with_label_values(b: &mut Bencher) {
20+
fn bench_counter_with_label_values(c: &mut Criterion) {
2721
let counter = CounterVec::new(
2822
Opts::new("benchmark_counter", "A counter to benchmark it."),
2923
&["one", "two", "three"],
3024
)
3125
.unwrap();
32-
b.iter(|| counter.with_label_values(&["eins", "zwei", "drei"]).inc())
26+
c.bench_function("counter_with_label_values", |b| {
27+
b.iter(|| counter.with_label_values(&["eins", "zwei", "drei"]).inc())
28+
});
3329
}
3430

35-
#[bench]
36-
fn bench_counter_with_mapped_labels(b: &mut Bencher) {
31+
fn bench_counter_with_mapped_labels(c: &mut Criterion) {
3732
let counter = CounterVec::new(
3833
Opts::new("benchmark_counter", "A counter to benchmark it."),
3934
&["one", "two", "three"],
4035
)
4136
.unwrap();
4237

43-
b.iter(|| {
44-
let mut labels = HashMap::with_capacity(3);
45-
labels.insert("two", "zwei");
46-
labels.insert("one", "eins");
47-
labels.insert("three", "drei");
48-
counter.with(&labels).inc();
49-
})
38+
c.bench_function("counter_with_mapped_labels", |b| {
39+
b.iter(|| {
40+
let mut labels = HashMap::with_capacity(3);
41+
labels.insert("two", "zwei");
42+
labels.insert("one", "eins");
43+
labels.insert("three", "drei");
44+
counter.with(&labels).inc();
45+
})
46+
});
5047
}
5148

52-
#[bench]
53-
fn bench_counter_with_prepared_mapped_labels(b: &mut Bencher) {
49+
fn bench_counter_with_prepared_mapped_labels(c: &mut Criterion) {
5450
let counter = CounterVec::new(
5551
Opts::new("benchmark_counter", "A counter to benchmark it."),
5652
&["one", "two", "three"],
@@ -62,25 +58,24 @@ fn bench_counter_with_prepared_mapped_labels(b: &mut Bencher) {
6258
labels.insert("one", "eins");
6359
labels.insert("three", "drei");
6460

65-
b.iter(|| {
66-
counter.with(&labels).inc();
67-
})
61+
c.bench_function("counter_with_prepared_mapped_labels", |b| {
62+
b.iter(|| {
63+
counter.with(&labels).inc();
64+
})
65+
});
6866
}
6967

70-
#[bench]
71-
fn bench_counter_no_labels(b: &mut Bencher) {
68+
fn bench_counter_no_labels(c: &mut Criterion) {
7269
let counter = Counter::new("benchmark_counter", "A counter to benchmark.").unwrap();
73-
b.iter(|| counter.inc())
70+
c.bench_function("counter_no_labels", |b| b.iter(|| counter.inc()));
7471
}
7572

76-
#[bench]
77-
fn bench_int_counter_no_labels(b: &mut Bencher) {
73+
fn bench_int_counter_no_labels(c: &mut Criterion) {
7874
let counter = IntCounter::new("benchmark_int_counter", "A int_counter to benchmark.").unwrap();
79-
b.iter(|| counter.inc())
75+
c.bench_function("int_counter_no_labels", |b| b.iter(|| counter.inc()));
8076
}
8177

82-
#[bench]
83-
fn bench_counter_no_labels_concurrent_nop(b: &mut Bencher) {
78+
fn bench_counter_no_labels_concurrent_nop(c: &mut Criterion) {
8479
let signal_exit = Arc::new(atomic::AtomicBool::new(false));
8580
let counter = Counter::new("foo", "bar").unwrap();
8681

@@ -95,7 +90,9 @@ fn bench_counter_no_labels_concurrent_nop(b: &mut Bencher) {
9590
})
9691
.collect();
9792

98-
b.iter(|| counter.inc());
93+
c.bench_function("counter_no_labels_concurrent_nop", |b| {
94+
b.iter(|| counter.inc());
95+
});
9996

10097
// Wait for accompanying thread to exit.
10198
signal_exit.store(true, atomic::Ordering::Relaxed);
@@ -104,8 +101,7 @@ fn bench_counter_no_labels_concurrent_nop(b: &mut Bencher) {
104101
}
105102
}
106103

107-
#[bench]
108-
fn bench_counter_no_labels_concurrent_write(b: &mut Bencher) {
104+
fn bench_counter_no_labels_concurrent_write(c: &mut Criterion) {
109105
let signal_exit = Arc::new(atomic::AtomicBool::new(false));
110106
let counter = Counter::new("foo", "bar").unwrap();
111107

@@ -122,7 +118,9 @@ fn bench_counter_no_labels_concurrent_write(b: &mut Bencher) {
122118
})
123119
.collect();
124120

125-
b.iter(|| counter.inc());
121+
c.bench_function("counter_no_labels_concurrent_write", |b| {
122+
b.iter(|| counter.inc());
123+
});
126124

127125
// Wait for accompanying thread to exit.
128126
signal_exit.store(true, atomic::Ordering::Relaxed);
@@ -131,8 +129,7 @@ fn bench_counter_no_labels_concurrent_write(b: &mut Bencher) {
131129
}
132130
}
133131

134-
#[bench]
135-
fn bench_int_counter_no_labels_concurrent_write(b: &mut Bencher) {
132+
fn bench_int_counter_no_labels_concurrent_write(c: &mut Criterion) {
136133
let signal_exit = Arc::new(atomic::AtomicBool::new(false));
137134
let counter = IntCounter::new("foo", "bar").unwrap();
138135

@@ -149,7 +146,9 @@ fn bench_int_counter_no_labels_concurrent_write(b: &mut Bencher) {
149146
})
150147
.collect();
151148

152-
b.iter(|| counter.inc());
149+
c.bench_function("int_counter_no_labels_concurrent_write", |b| {
150+
b.iter(|| counter.inc());
151+
});
153152

154153
// Wait for accompanying thread to exit.
155154
signal_exit.store(true, atomic::Ordering::Relaxed);
@@ -158,8 +157,7 @@ fn bench_int_counter_no_labels_concurrent_write(b: &mut Bencher) {
158157
}
159158
}
160159

161-
#[bench]
162-
fn bench_counter_with_label_values_concurrent_write(b: &mut Bencher) {
160+
fn bench_counter_with_label_values_concurrent_write(c: &mut Criterion) {
163161
let signal_exit = Arc::new(atomic::AtomicBool::new(false));
164162
let counter = CounterVec::new(Opts::new("foo", "bar"), &["one", "two", "three"]).unwrap();
165163

@@ -175,11 +173,27 @@ fn bench_counter_with_label_values_concurrent_write(b: &mut Bencher) {
175173
})
176174
.collect();
177175

178-
b.iter(|| counter.with_label_values(&["eins", "zwei", "drei"]).inc());
176+
c.bench_function("counter_with_label_values_concurrent_write", |b| {
177+
b.iter(|| counter.with_label_values(&["eins", "zwei", "drei"]).inc());
178+
});
179179

180180
// Wait for accompanying thread to exit.
181181
signal_exit.store(true, atomic::Ordering::Relaxed);
182182
for h in thread_handles {
183183
h.join().unwrap();
184184
}
185185
}
186+
187+
criterion_group!(
188+
benches,
189+
bench_counter_no_labels,
190+
bench_counter_no_labels_concurrent_nop,
191+
bench_counter_no_labels_concurrent_write,
192+
bench_counter_with_label_values,
193+
bench_counter_with_label_values_concurrent_write,
194+
bench_counter_with_mapped_labels,
195+
bench_counter_with_prepared_mapped_labels,
196+
bench_int_counter_no_labels,
197+
bench_int_counter_no_labels_concurrent_write,
198+
);
199+
criterion_main!(benches);

benches/desc.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,21 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
#![feature(test)]
15-
16-
extern crate test;
17-
14+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1815
use prometheus::core::Desc;
1916

20-
use test::{black_box, Bencher};
21-
22-
#[bench]
23-
fn description_validation(b: &mut Bencher) {
24-
b.iter(|| {
25-
black_box(Desc::new(
26-
"api_http_requests_total".to_string(),
27-
"not empty help".to_string(),
28-
vec!["method".to_string(), "handler".to_string()],
29-
Default::default(),
30-
))
17+
fn description_validation(c: &mut Criterion) {
18+
c.bench_function("description_validation", |b| {
19+
b.iter(|| {
20+
black_box(Desc::new(
21+
"api_http_requests_total".to_string(),
22+
"not empty help".to_string(),
23+
vec!["method".to_string(), "handler".to_string()],
24+
Default::default(),
25+
))
26+
});
3127
});
3228
}
29+
30+
criterion_group!(benches, description_validation);
31+
criterion_main!(benches);

0 commit comments

Comments
 (0)