Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documents #88

Merged
merged 3 commits into from
Feb 12, 2024
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
29 changes: 13 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

```toml
# This sample uses `tokio` runtime.
# You can choose `async-std` instead by feature "rt_async-std".
# `async-std` is available by feature "rt_async-std".

[dependencies]
ohkami = { version = "0.14", features = ["rt_tokio"] }
Expand Down Expand Up @@ -131,15 +131,6 @@ ohkami's middlewares are called "**fang**s".
```rust,no_run
use ohkami::prelude::*;

struct AppendHeaders;
impl BackFang for AppendHeaders {
async fn bite(&self, res: &mut Response, _req: &Request) -> Result<(), Response> {
res.headers.set()
.Server("ohkami");
Ok(())
}
}

struct LogRequest;
impl FrontFang for LogRequest {
async fn bite(&self, req: &mut Request) -> Result<(), Response> {
Expand All @@ -148,13 +139,21 @@ impl FrontFang for LogRequest {
}
}

struct AppendServer;
impl BackFang for AppendServer {
async fn bite(&self, res: &mut Response, _req: &Request) -> Result<(), Response> {
res.headers.set()
.Server("ohkami");
Ok(())
}
}

#[tokio::main]
async fn main() {
Ohkami::with((AppendHeaders, LogRequest), (
Ohkami::with((AppendServer, LogRequest), (
"/".GET(|| async {"Hello!"})
)).howl("localhost:8080").await
}

```

<br>
Expand Down Expand Up @@ -204,9 +203,7 @@ use ohkami::testing::*; // <--

fn hello_ohkami() -> Ohkami {
Ohkami::new((
"/hello".GET(|| async move {
"Hello, world!"
}),
"/hello".GET(|| async {"Hello, world!"}),
))
}

Expand Down Expand Up @@ -236,7 +233,7 @@ async fn test_my_ohkami() {
- [ ] WebSocket

## MSRV (Minimum Supported rustc Version)
Latest stable at that time.
Latest stable

## License
ohkami is licensed under MIT LICENSE ([LICENSE](https://github.com/kana-rus/ohkami/blob/main/LICENSE) or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)).
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")
}
}
Loading