Skip to content

Commit

Permalink
Add a new crate for benchmarks
Browse files Browse the repository at this point in the history
Currently it only contains one very simplistic benchmark:
Setting 2 metrics and submitting a custom ping.

Why an additional crate?
This way we don't add any new (dev) dependencies to the crates that get released.
  • Loading branch information
badboy committed Feb 4, 2020
1 parent f0e5ab2 commit e427b15
Show file tree
Hide file tree
Showing 8 changed files with 818 additions and 0 deletions.
313 changes: 313 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"glean-core",
"glean-core/ffi",
"glean-core/preview",
"glean-core/benchmark",
]

[profile.release]
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ test-python: build-python ## Run all Python tests

.PHONY: test test-rust test-rust-with-logs test-kotlin test-swift test-ios-sample

# Benchmarks

bench-rust: ## Run Rust benchmarks
cargo bench -p benchmark

.PHONY: bench-rust

# Linting

lint: clippy
Expand Down
24 changes: 24 additions & 0 deletions glean-core/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "benchmark"
version = "0.1.0"
authors = ["Jan-Erik Rediger <jrediger@mozilla.com>"]
description = "Benchmark the glean-core crate"
readme = "README.md"
license = "MPL-2.0"
edition = "2018"
publish = false

[lib]
bench = false

[dependencies]
# No version specified, this crate never gets published
glean-core = { path = ".." }

[dev-dependencies]
tempfile = "3.1.0"
criterion = "0.3"

[[bench]]
name = "bench_basic"
harness = false
373 changes: 373 additions & 0 deletions glean-core/benchmark/LICENSE

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions glean-core/benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Glean Benchmarks

The `Glean SDK` is a modern approach for a Telemetry library and is part of the [Glean project](https://docs.telemetry.mozilla.org/concepts/glean/glean.html).

## Benchmarks

This crates provides simple benchmarks for Glean, based on the [criterion benchmark framework](https://bheisler.github.io/criterion.rs/book/criterion_rs.html).
The library itself does not contain additional code.

### Available benchmarks

* [`benches/bench_basic.rs`](benches/bench_basic.rs) - Setting metrics and submitting a custom ping

### How to run the benchmarks

From the top-level directory of the repository run:

```
cargo bench -p benchmark
```

This is also available as a `make` task:

```
make bench-rust
```

Any additional run compares results to the preceding run.

### Results

After running the benchmarks, an HTML-rendered report can be found in `target/criterion/report/index.html`.
We do not provide any results here as the absolute numbers are unreliable.
Benchmark timings across code changes are more important.

### Why an additional crate?

This way we don't add any new (dev) dependencies to the crates that get released.
48 changes: 48 additions & 0 deletions glean-core/benchmark/benches/bench_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use benchmark::glean_core::{metrics::*, CommonMetricData, Configuration, Glean};
use criterion::{criterion_group, criterion_main, Criterion};

/// Set metrics and submit a custom ping.
///
/// Glean, the metrics and the custom ping are instantiated before benchmarking the set/submit
/// functionality.
pub fn criterion_benchmark(c: &mut Criterion) {
let data_dir = tempfile::tempdir().unwrap();
let tmpname = data_dir.path().display().to_string();
let cfg = Configuration {
upload_enabled: true,
data_path: tmpname,
application_id: "glean.bench".into(),
max_events: None,
delay_ping_lifetime_io: false,
};

let mut glean = Glean::new(cfg).unwrap();

let ping = PingType::new("sample", true, false);
glean.register_ping_type(&ping);

let call_counter: CounterMetric = CounterMetric::new(CommonMetricData {
name: "calls".into(),
category: "local".into(),
send_in_pings: vec!["sample".into()],
..Default::default()
});

let string: StringMetric = StringMetric::new(CommonMetricData {
name: "string".into(),
category: "local".into(),
send_in_pings: vec!["sample".into()],
..Default::default()
});

c.bench_function("glean set and submit", |b| {
b.iter(|| {
call_counter.add(&glean, 1);
string.set(&glean, "hello world");
glean.submit_ping(&ping).unwrap();
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
14 changes: 14 additions & 0 deletions glean-core/benchmark/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! # Glean benchmarks
//!
//! Benchmarking the glean-core crate. Find benchmarks in the `benches/` directory.
//!
//! All documentation for Glean can be found online:
//!
//! ## [The Glean SDK Book](https://mozilla.github.io/glean)
// Re-export glean_core in total.
pub use glean_core;

0 comments on commit e427b15

Please sign in to comment.