Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
398 changes: 397 additions & 1 deletion 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 @@ -14,3 +14,4 @@ bincode = "1"
cmz = "0.2"
serde = "1.0.219"
thiserror = "2.0.12"
criterion = { version = "0.5"}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ Open `ios/OoniAuthApp.xcodeproj` in Xcode.
Criterion benchmark (same flow):
```bash
cargo bench -p ooniauth-core
cargo bench -p ooniauth_py
```

To generate a flamegraph for ooniath_py benchmarks:
```bash
cargo bench -p ooniauth_py --bench bench_server -- --profile-time 5
```

The resulting report will be stored on `target/criterion/server.handle_submit_request_with_hash/profile/flamegraph.svg`
2 changes: 1 addition & 1 deletion ooniauth-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ hex = "0.4"
tracing = "0.1"

[dev-dependencies]
criterion = { version = "0.5"}
criterion = {workspace = true}
tracing-subscriber = { version = "0.3", features = ["env-filter", "registry"] }
tracing-forest = "0.1"

Expand Down
22 changes: 21 additions & 1 deletion ooniauth-core/benches/bench_server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use ooniauth_core::{ServerState, UserState};
use ooniauth_core::submit::submit_measurement_hash;
use rand::{rngs::ThreadRng, thread_rng};
use std::hint::black_box;
use rand::RngCore;

fn setup() -> (ThreadRng, UserState, ServerState) {
let mut rng = thread_rng();
Expand All @@ -12,6 +14,12 @@ fn setup() -> (ThreadRng, UserState, ServerState) {
(rng, user, server)
}

fn random_megabytes(mb: usize) -> Vec<u8> {
let mut buf = vec![0u8; mb * 1024 * 1024];
rand::thread_rng().fill_bytes(&mut buf);
buf
}

// For now we will only care about server functions, specially handling submit and
// registration, since those are the most important bottleneck for our backend
fn bench_registration(c: &mut Criterion) {
Expand Down Expand Up @@ -98,5 +106,17 @@ fn bench_update(c: &mut Criterion) {
});
}

criterion_group!(benches, bench_registration, bench_submit, bench_update);
fn bench_hash(c: &mut Criterion) {
c.bench_function("submit_measurement_hash", |b|{
b.iter_batched(|| {
// Measurement bodies are usually ~1mb
random_megabytes(1)
},
|bytes| submit_measurement_hash(bytes.as_ref()),
BatchSize::SmallInput);
});

}

criterion_group!(benches, bench_registration, bench_submit, bench_update, bench_hash);
criterion_main!(benches);
8 changes: 8 additions & 0 deletions ooniauth-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ serde = {workspace = true}
thiserror = {workspace = true}
base64 = "0.22.1"

[dev-dependencies]
criterion = {workspace = true}
pprof = { version = "0.13", features = ["flamegraph", "criterion"] }

[build-dependencies]
pyo3-build-config = "0.28.3"

[[bench]]
name = "bench_server"
harness = false
72 changes: 72 additions & 0 deletions ooniauth-py/benches/bench_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use ooniauth_py::{ServerState, UserState};
use pyo3::{Py, Python, types::PyString};
use rand::{distributions::Alphanumeric, Rng};
use pprof::criterion::{Output, PProfProfiler};

fn random_ascii_string_mb(mb: usize) -> String {
let byte_count = mb * 1024 * 1024;
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(byte_count)
.map(char::from)
.collect()
}

fn bench_submit(c: &mut Criterion) {
pyo3::Python::initialize();
Python::attach(|py| {
c.bench_function("server.handle_submit_request_with_hash", |b| {
b.iter_batched(|| {
let server = ServerState::new();
let mut client = UserState::new(py, server.get_public_parameters(py)).unwrap();
let req = client.make_registration_request(py).unwrap();
let reg_response = server.handle_registration_request(py, req).unwrap();
client
.handle_registration_response(py, reg_response)
.unwrap();

let cc: Py<PyString> = PyString::new(py, "VE").into();
let asn: Py<PyString> = PyString::new(py, "AS1234").into();
let msm_body = random_ascii_string_mb(1);
let measurement: Py<PyString> = PyString::new(py, msm_body.as_str()).into();
let today = ServerState::today();
let age_tuple = (today - 30, today + 1);
let min_msm = 0u32;

let submit_req = client
.make_submit_request_with_hash(
py,
cc.clone_ref(py),
asn.clone_ref(py),
measurement.clone_ref(py),
age_tuple,
min_msm,
)
.unwrap();

(server, submit_req, cc, asn, measurement, age_tuple, min_msm)
},
|(server, submit_req, cc, asn, measurement, age_tuple, min_msm)| {
server.handle_submit_request_with_hash(
py,
submit_req.nym,
submit_req.request,
cc,
asn,
measurement,
age_tuple,
min_msm
)
}, BatchSize::SmallInput);
});
});
}

criterion_group!{
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
targets = bench_submit
}

criterion_main!(benches);
14 changes: 7 additions & 7 deletions ooniauth-py/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ impl ServerState {
})
}

fn get_secret_key(&self, py: Python<'_>) -> Py<PyString> {
pub fn get_secret_key(&self, py: Python<'_>) -> Py<PyString> {
to_pystring(py, self.state.secret_key_ref())
}

fn get_public_parameters(&self, py: Python<'_>) -> Py<PyString> {
pub fn get_public_parameters(&self, py: Python<'_>) -> Py<PyString> {
to_pystring(py, self.state.public_parameters_ref())
}

fn handle_registration_request(
pub fn handle_registration_request(
&self,
py: Python<'_>,
registration_request: Py<PyString>,
Expand All @@ -111,7 +111,7 @@ impl ServerState {
}

#[staticmethod]
fn today() -> u32 {
pub fn today() -> u32 {
ooni::ServerState::today()
}

Expand Down Expand Up @@ -159,7 +159,7 @@ impl ServerState {
/// measurement. Computes the hash internally using the
/// [submit_measurement_hash] function.
#[allow(clippy::too_many_arguments)]
fn handle_submit_request_with_hash(
pub fn handle_submit_request_with_hash(
&self,
py: Python<'_>,
nym: Py<PyString>,
Expand Down Expand Up @@ -460,9 +460,9 @@ impl UserState {
#[pyclass]
pub struct SubmitRequest {
#[pyo3(get)]
nym: Py<PyString>,
pub nym: Py<PyString>,
#[pyo3(get)]
request: Py<PyString>,
pub request: Py<PyString>,
}

#[cfg(test)]
Expand Down