Skip to content

Commit

Permalink
Fix: README & Add: benches
Browse files Browse the repository at this point in the history
  • Loading branch information
kanarus committed Feb 12, 2024
1 parent 116af56 commit 7f3b847
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 15 deletions.
99 changes: 85 additions & 14 deletions benches/benches/response_headers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#![feature(test)]
extern crate test;


use ohkami::__internal__::ResponseHeaders;
use http::{HeaderMap, header, HeaderValue};
use ohkami_benches::fxmap::FxMap;




#[bench] fn insert_ohkami(b: &mut test::Bencher) {
let mut h = ResponseHeaders::init();

b.iter(|| {
h.set()
.AccessControlAllowCredentials(test::black_box("true"))
Expand All @@ -29,7 +32,6 @@ use http::{HeaderMap, header, HeaderValue};

#[bench] fn insert_http(b: &mut test::Bencher) {
let mut h = HeaderMap::new();

b.iter(|| {
h.insert(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, HeaderValue::from_static(test::black_box("true")));
h.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_static(test::black_box("X-Custom-Header,Upgrade-Insecure-Requests")));
Expand All @@ -48,10 +50,30 @@ use http::{HeaderMap, header, HeaderValue};
});
}

#[bench] fn insert_fxmap(b: &mut test::Bencher) {
let mut h = FxMap::new();
b.iter(|| {
h
.insert("Access-Control-Allow-Credentials", test::black_box("true"))
.insert("Access-Control-Allow-Headers", test::black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", test::black_box("https://foo.bar.org"))
.insert("Access-Control-Max-Age", test::black_box("86400"))
.insert("Vary", test::black_box("Origin"))
.insert("Server", test::black_box("ohkami"))
.insert("Connection", test::black_box("Keep-Alive"))
.insert("Date", test::black_box("Wed, 21 Oct 2015 07:28:00 GMT"))
.insert("Alt-Svc", test::black_box("h2=\":433\"; ma=2592000"))
.insert("Proxy-Authenticate", test::black_box("Basic realm=\"Access to the internal site\""))
.insert("Referer-Policy", test::black_box("same-origin"))
.insert("X-Frame-Options", test::black_box("DEBY"));
});
}




#[bench] fn remove_ohkami(b: &mut test::Bencher) {
let mut h = ResponseHeaders::init();

h.set()
.AccessControlAllowCredentials(test::black_box("true"))
.AccessControlAllowHeaders(test::black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
Expand Down Expand Up @@ -89,7 +111,6 @@ use http::{HeaderMap, header, HeaderValue};

#[bench] fn remove_http(b: &mut test::Bencher) {
let mut h = HeaderMap::new();

h.insert(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, HeaderValue::from_static(test::black_box("true")));
h.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_static(test::black_box("X-Custom-Header,Upgrade-Insecure-Requests")));
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static(test::black_box("https://foo.bar.org")));
Expand Down Expand Up @@ -123,10 +144,44 @@ use http::{HeaderMap, header, HeaderValue};
});
}

#[bench] fn remove_fxmap(b: &mut test::Bencher) {
let mut h = FxMap::new();
h
.insert("Access-Control-Allow-Credentials", test::black_box("true"))
.insert("Access-Control-Allow-Headers", test::black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", test::black_box("https://foo.bar.org"))
.insert("Access-Control-Max-Age", test::black_box("86400"))
.insert("Vary", test::black_box("Origin"))
.insert("Server", test::black_box("ohkami"))
.insert("Connection", test::black_box("Keep-Alive"))
.insert("Date", test::black_box("Wed, 21 Oct 2015 07:28:00 GMT"))
.insert("Alt-Svc", test::black_box("h2=\":433\"; ma=2592000"))
.insert("Proxy-Authenticate", test::black_box("Basic realm=\"Access to the internal site\""))
.insert("Referer-Policy", test::black_box("same-origin"))
.insert("X-Frame-Options", test::black_box("DEBY"));

b.iter(|| {
h
.remove(test::black_box("Access-Control-Allow-Credentials"))
.remove(test::black_box("Access-Control-Allow-Headers"))
.remove(test::black_box("Access-Control-Allow-Origin"))
.remove(test::black_box("Access-Control-Max-Age"))
.remove(test::black_box("Vary"))
.remove(test::black_box("Server"))
.remove(test::black_box("Connection"))
.remove(test::black_box("Date"))
.remove(test::black_box("Alt-Svc"))
.remove(test::black_box("Proxy-Authenticate"))
.remove(test::black_box("Referer-Policy"))
.remove(test::black_box("X-Frame-Options"));
});
}




#[bench] fn write_ohkami(b: &mut test::Bencher) {
let mut h = ResponseHeaders::init();

h.set()
.AccessControlAllowCredentials(test::black_box("true"))
.AccessControlAllowHeaders(test::black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
Expand All @@ -143,18 +198,14 @@ use http::{HeaderMap, header, HeaderValue};
.ReferrerPolicy(test::black_box("same-origin"))
.XFrameOptions(test::black_box("DENY"));

let mut buf = Vec::new();
b.iter(|| {
let mut buf = Vec::new();

h.write_ref_to(&mut buf);

println!("{buf:?}")
});
}

#[bench] fn write_http(b: &mut test::Bencher) {
let mut h = HeaderMap::new();

h.insert(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, HeaderValue::from_static(test::black_box("true")));
h.insert(header::ACCESS_CONTROL_ALLOW_HEADERS, HeaderValue::from_static(test::black_box("X-Custom-Header,Upgrade-Insecure-Requests")));
h.insert(header::ACCESS_CONTROL_ALLOW_ORIGIN, HeaderValue::from_static(test::black_box("https://foo.bar.org")));
Expand All @@ -170,17 +221,37 @@ use http::{HeaderMap, header, HeaderValue};
h.insert(header::REFERRER_POLICY, HeaderValue::from_static("same-origin"));
h.insert(header::X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));

let mut buf = Vec::new();
b.iter(|| {
let mut buf = Vec::new();

for (k, v) in h.iter() {
buf.extend_from_slice(k.as_str().as_bytes());
buf.extend(b": ");
buf.extend(v.as_bytes());
buf.extend(b"\r\n");
}
buf.extend(b"\r\n");
});
}

#[bench] fn write_fxmap(b: &mut test::Bencher) {
let mut h = FxMap::new();
h
.insert("Access-Control-Allow-Credentials", test::black_box("true"))
.insert("Access-Control-Allow-Headers", test::black_box("X-Custom-Header,Upgrade-Insecure-Requests"))
.insert("Access-Control-Allow-Origin", test::black_box("https://foo.bar.org"))
.insert("Access-Control-Max-Age", test::black_box("86400"))
.insert("Vary", test::black_box("Origin"))
.insert("Server", test::black_box("ohkami"))
.insert("Connection", test::black_box("Keep-Alive"))
.insert("Date", test::black_box("Wed, 21 Oct 2015 07:28:00 GMT"))
.insert("Alt-Svc", test::black_box("h2=\":433\"; ma=2592000"))
.insert("Proxy-Authenticate", test::black_box("Basic realm=\"Access to the internal site\""))
.insert("Referer-Policy", test::black_box("same-origin"))
.insert("X-Frame-Options", test::black_box("DEBY"));

println!("{buf:?}")
let mut buf = Vec::new();
b.iter(|| {
h.write_to(&mut buf);
});
}
}

64 changes: 63 additions & 1 deletion benches/src/fxmap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
use std::borrow::Cow;

pub struct FxMap(rustc_hash::FxHashMap<Cow<'static, str>, Cow<'static, str>>);
pub struct FxMap {
map: rustc_hash::FxHashMap<Cow<'static, str>, Cow<'static, str>>,
size: usize,
}
impl FxMap {
pub fn new() -> Self {
Self {
map: rustc_hash::FxHashMap::default(),
size: 2/* "\r\n".len() */
}
}

pub fn insert(
&mut self,
key: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> &mut Self {
let (key, value) = (key.into(), value.into());
let key_len = key.len();

self.size += value.len();
if let Some(old) = self.map.insert(key, value) {
self.size -= old.len();
} else {
self.size += key_len + 2/* ": ".len() */ + 2/* "\r\n".len() */;
}
self
}

pub fn remove(&mut self, key: impl Into<Cow<'static, str>>) -> &mut Self {
let key = key.into();
if let Some(old) = self.map.remove(&key) {
self.size -= key.len() + 2/* ": ".len() */ + old.len() + 2/* "\r\n".len() */;
}
self
}

pub fn write_to(&self, buf: &mut Vec<u8>) {
macro_rules! push {
($buf:ident <- $bytes:expr) => {
unsafe {
let (buf_len, bytes_len) = ($buf.len(), $bytes.len());
std::ptr::copy_nonoverlapping(
$bytes.as_ptr(),
<[u8]>::as_mut_ptr($buf).add(buf_len),
bytes_len
);
$buf.set_len(buf_len + bytes_len);
}
};
}

buf.reserve(self.size);

for (k, v) in &self.map {
push!(buf <- k.as_bytes());
push!(buf <- b": ");
push!(buf <- v.as_bytes());
push!(buf <- b"\r\n");
}
push!(buf <- b"\r\n")
}
}

0 comments on commit 7f3b847

Please sign in to comment.