From eb91adf1a1f52ce37de9d479c792f6e2385f0026 Mon Sep 17 00:00:00 2001 From: aumetra Date: Sat, 17 Feb 2024 17:28:49 +0100 Subject: [PATCH] add construction bench --- lib/http-signatures/Cargo.toml | 4 ++ .../benches/build_cavage_signature_string.rs | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lib/http-signatures/benches/build_cavage_signature_string.rs diff --git a/lib/http-signatures/Cargo.toml b/lib/http-signatures/Cargo.toml index e9572ca9c..b191a3a01 100644 --- a/lib/http-signatures/Cargo.toml +++ b/lib/http-signatures/Cargo.toml @@ -5,6 +5,10 @@ edition.workspace = true version.workspace = true license = "MIT OR Apache-2.0" +[[bench]] +name = "build_cavage_signature_string" +harness = false + [[bench]] name = "parse_cavage_header" harness = false diff --git a/lib/http-signatures/benches/build_cavage_signature_string.rs b/lib/http-signatures/benches/build_cavage_signature_string.rs new file mode 100644 index 000000000..f1ced8dae --- /dev/null +++ b/lib/http-signatures/benches/build_cavage_signature_string.rs @@ -0,0 +1,38 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use http::{Method, Request, Uri}; +use std::hint::black_box; + +const CAVAGE_HEADER: &str = + r#"keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date",signature="qdx""#; + +fn build_cavage_signature_string1(c: &mut Criterion) { + let signature_header = http_signatures::cavage::parse(CAVAGE_HEADER).unwrap(); + let request = Request::builder() + .method(Method::GET) + .uri(Uri::from_static("/foo?param=value&pet=dog")) + .header("Host", "example.com") + .header("Date", "Sun, 05 Jan 2014 21:31:40 GMT") + .header("Content-Type", "application/json") + .header( + "Digest", + "SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=", + ) + .header("Content-Length", "18") + .body(()) + .unwrap(); + + c.bench_function("build_cavage_signature_string", |b| { + b.iter(|| { + let _ = black_box(http_signatures::cavage::signature_string::construct( + black_box(&request), + black_box(&signature_header), + )); + }); + }); +} + +criterion_group!( + build_cavage_signature_string, + build_cavage_signature_string1 +); +criterion_main!(build_cavage_signature_string);