Skip to content

Commit

Permalink
refactor: add benchmark for response header
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Mar 24, 2024
1 parent e563c5c commit d324e7f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
41 changes: 39 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use bytes::Bytes;
use criterion::{criterion_group, criterion_main, Criterion};
use http::{HeaderName, HeaderValue};
use http::{HeaderName, HeaderValue, StatusCode};
use pingap::cache::{convert_headers, HttpResponse};
use pingora::http::ResponseHeader;
use std::time::{SystemTime, UNIX_EPOCH};

fn insert_bytes_header(c: &mut Criterion) {
c.bench_function("bytes header", |b| {
Expand Down Expand Up @@ -60,5 +62,40 @@ fn insert_header_name(c: &mut Criterion) {
});
}

criterion_group!(benches, insert_bytes_header, insert_header_name,);
fn get_response_header(c: &mut Criterion) {
c.bench_function("get response header for http response", |b| {
let resp = HttpResponse {
status: StatusCode::OK,
body: Bytes::from("Hello world!"),
max_age: Some(3600),
created_at: Some(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
- 10,
),
private: Some(true),
headers: Some(
convert_headers(&[
"Contont-Type: application/json".to_string(),
"Content-Encoding: gzip".to_string(),
])
.unwrap(),
),
};

b.iter(|| {
let value = resp.clone();
value.get_response_header().unwrap();
});
});
}

criterion_group!(
benches,
insert_bytes_header,
insert_header_name,
get_response_header
);
criterion_main!(benches);
4 changes: 2 additions & 2 deletions src/cache/http_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use http::StatusCode;
use pingora::{http::ResponseHeader, proxy::Session};
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Default, Debug)]
#[derive(Default, Debug, Clone)]
pub struct HttpResponse {
pub status: StatusCode,
pub body: Bytes,
Expand All @@ -16,7 +16,7 @@ pub struct HttpResponse {
}

impl HttpResponse {
fn get_response_header(&self) -> pingora::Result<ResponseHeader> {
pub fn get_response_header(&self) -> pingora::Result<ResponseHeader> {
let fix_size = 3;
let size = self
.headers
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod cache;
pub mod config;
pub mod proxy;
pub mod utils;

0 comments on commit d324e7f

Please sign in to comment.