ci: use crate for bench

This commit is contained in:
somehowchris 2022-12-07 15:06:23 +01:00
parent e73a76600c
commit c6857e907b
No known key found for this signature in database
GPG Key ID: 90442E48D883198E
3 changed files with 49 additions and 72 deletions

View File

@ -19,14 +19,14 @@ edition = "2018"
include = ["Cargo.toml", "LICENSE", "src/**/*"] include = ["Cargo.toml", "LICENSE", "src/**/*"]
[[bench]] [[bench]]
name="internal" name = "internal"
harness = false harness = false
[dependencies] [dependencies]
hyper = { version = "0.14.18", features = ["client"] } hyper = { version = "0.14.18", features = ["client"] }
lazy_static = "1.4.0"
tokio = { version = "1.17.0", features = ["io-util", "rt"] } tokio = { version = "1.17.0", features = ["io-util", "rt"] }
tracing = "0.1.34" tracing = "0.1.34"
visibility = { version = "0.0.1", optional = true }
[dev-dependencies] [dev-dependencies]
hyper = { version = "0.14.18", features = ["server"] } hyper = { version = "0.14.18", features = ["server"] }
@ -40,7 +40,7 @@ hyper-trust-dns = { version = "0.4.2", features = [
"rustls-http2", "rustls-http2",
"dnssec-ring", "dnssec-ring",
"dns-over-https-rustls", "dns-over-https-rustls",
"rustls-webpki" "rustls-webpki",
] } ] }
rand = "0.8.5" rand = "0.8.5"
tungstenite = "0.17" tungstenite = "0.17"
@ -49,4 +49,4 @@ criterion = "0.3.5"
[features] [features]
__bench=[] __bench = ["dep:visibility"]

View File

@ -4,7 +4,6 @@ use hyper::client::HttpConnector;
use hyper::header::HeaderName; use hyper::header::HeaderName;
use hyper::Uri; use hyper::Uri;
use hyper::{HeaderMap, Request, Response}; use hyper::{HeaderMap, Request, Response};
use hyper_reverse_proxy::benches as internal_benches;
use hyper_reverse_proxy::ReverseProxy; use hyper_reverse_proxy::ReverseProxy;
use rand::distributions::Alphanumeric; use rand::distributions::Alphanumeric;
use rand::prelude::*; use rand::prelude::*;
@ -31,7 +30,7 @@ fn create_proxied_response(b: &mut Criterion) {
*response.headers_mut().unwrap() = headers_map.clone(); *response.headers_mut().unwrap() = headers_map.clone();
internal_benches::create_proxied_response(black_box(response.body(()).unwrap())); hyper_reverse_proxy::create_proxied_response(black_box(response.body(()).unwrap()));
}) })
}); });
} }
@ -46,7 +45,7 @@ fn generate_string() -> String {
} }
fn build_headers() -> HeaderMap { fn build_headers() -> HeaderMap {
let mut headers_map: HeaderMap = (&*internal_benches::hop_headers()) let mut headers_map: HeaderMap = (&hyper_reverse_proxy::HOP_HEADERS)
.iter() .iter()
.map(|el: &'static HeaderName| (el.clone(), generate_string().parse().unwrap())) .map(|el: &'static HeaderName| (el.clone(), generate_string().parse().unwrap()))
.collect(); .collect();
@ -108,7 +107,7 @@ fn forward_url_with_str_ending_slash(b: &mut Criterion) {
b.iter(|| { b.iter(|| {
let request = Request::builder().uri(uri.clone()).body(()); let request = Request::builder().uri(uri.clone()).body(());
internal_benches::forward_uri(forward_url, &request.unwrap()); hyper_reverse_proxy::forward_uri(forward_url, &request.unwrap());
}) })
}); });
} }
@ -122,7 +121,7 @@ fn forward_url_with_str_ending_slash_and_query(b: &mut Criterion) {
t.iter(|| { t.iter(|| {
let request = Request::builder().uri(uri.clone()).body(()); let request = Request::builder().uri(uri.clone()).body(());
internal_benches::forward_uri(forward_url, &request.unwrap()); hyper_reverse_proxy::forward_uri(forward_url, &request.unwrap());
}) })
}); });
} }
@ -136,7 +135,7 @@ fn forward_url_no_ending_slash(b: &mut Criterion) {
t.iter(|| { t.iter(|| {
let request = Request::builder().uri(uri.clone()).body(()); let request = Request::builder().uri(uri.clone()).body(());
internal_benches::forward_uri(forward_url, &request.unwrap()); hyper_reverse_proxy::forward_uri(forward_url, &request.unwrap());
}) })
}); });
} }
@ -150,7 +149,7 @@ fn forward_url_with_query(b: &mut Criterion) {
t.iter(|| { t.iter(|| {
let request = Request::builder().uri(uri.clone()).body(()); let request = Request::builder().uri(uri.clone()).body(());
internal_benches::forward_uri(forward_url, &request.unwrap()); hyper_reverse_proxy::forward_uri(forward_url, &request.unwrap());
}) })
}); });
} }
@ -175,12 +174,13 @@ fn create_proxied_request_forwarded_for_occupied(b: &mut Criterion) {
*request.headers_mut().unwrap() = headers_map.clone(); *request.headers_mut().unwrap() = headers_map.clone();
internal_benches::create_proxied_request( hyper_reverse_proxy::create_proxied_request(
client_ip, client_ip,
forward_url, forward_url,
request.body(()).unwrap(), request.body(()).unwrap(),
None, None,
); )
.unwrap();
}) })
}); });
} }
@ -200,12 +200,13 @@ fn create_proxied_request_forwarded_for_vacant(b: &mut Criterion) {
*request.headers_mut().unwrap() = headers_map.clone(); *request.headers_mut().unwrap() = headers_map.clone();
internal_benches::create_proxied_request( hyper_reverse_proxy::create_proxied_request(
client_ip, client_ip,
forward_url, forward_url,
request.body(()).unwrap(), request.body(()).unwrap(),
None, None,
); )
.unwrap();
}) })
}); });
} }

View File

@ -8,31 +8,28 @@ use hyper::http::header::{InvalidHeaderValue, ToStrError};
use hyper::http::uri::InvalidUri; use hyper::http::uri::InvalidUri;
use hyper::upgrade::OnUpgrade; use hyper::upgrade::OnUpgrade;
use hyper::{Body, Client, Error, Request, Response, StatusCode}; use hyper::{Body, Client, Error, Request, Response, StatusCode};
use lazy_static::lazy_static;
use std::net::IpAddr; use std::net::IpAddr;
use tokio::io::copy_bidirectional; use tokio::io::copy_bidirectional;
lazy_static! { static TE_HEADER: HeaderName = HeaderName::from_static("te");
static ref TE_HEADER: HeaderName = HeaderName::from_static("te"); static CONNECTION_HEADER: HeaderName = HeaderName::from_static("connection");
static ref CONNECTION_HEADER: HeaderName = HeaderName::from_static("connection"); static UPGRADE_HEADER: HeaderName = HeaderName::from_static("upgrade");
static ref UPGRADE_HEADER: HeaderName = HeaderName::from_static("upgrade"); static TRAILERS_HEADER: HeaderName = HeaderName::from_static("trailers");
static ref TRAILER_HEADER: HeaderName = HeaderName::from_static("trailer"); static X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
static ref TRAILERS_HEADER: HeaderName = HeaderName::from_static("trailers");
// A list of the headers, using hypers actual HeaderName comparison // A list of the headers, using hypers actual HeaderName comparison
static ref HOP_HEADERS: [HeaderName; 9] = [ #[cfg_attr(feature = "__bench", visibility::make(pub))]
CONNECTION_HEADER.clone(), static HOP_HEADERS: [HeaderName; 9] = [
TE_HEADER.clone(), HeaderName::from_static("connection"),
TRAILER_HEADER.clone(), HeaderName::from_static("te"),
HeaderName::from_static("trailer"),
HeaderName::from_static("keep-alive"), HeaderName::from_static("keep-alive"),
HeaderName::from_static("proxy-connection"), HeaderName::from_static("proxy-connection"),
HeaderName::from_static("proxy-authenticate"), HeaderName::from_static("proxy-authenticate"),
HeaderName::from_static("proxy-authorization"), HeaderName::from_static("proxy-authorization"),
HeaderName::from_static("transfer-encoding"), HeaderName::from_static("transfer-encoding"),
HeaderName::from_static("upgrade"), HeaderName::from_static("upgrade"),
]; ];
static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
}
#[derive(Debug)] #[derive(Debug)]
pub enum ProxyError { pub enum ProxyError {
@ -69,7 +66,7 @@ impl From<InvalidHeaderValue> for ProxyError {
fn remove_hop_headers(headers: &mut HeaderMap) { fn remove_hop_headers(headers: &mut HeaderMap) {
debug!("Removing hop headers"); debug!("Removing hop headers");
for header in &*HOP_HEADERS { for header in &HOP_HEADERS {
headers.remove(header); headers.remove(header);
} }
} }
@ -77,17 +74,17 @@ fn remove_hop_headers(headers: &mut HeaderMap) {
fn get_upgrade_type(headers: &HeaderMap) -> Option<String> { fn get_upgrade_type(headers: &HeaderMap) -> Option<String> {
#[allow(clippy::blocks_in_if_conditions)] #[allow(clippy::blocks_in_if_conditions)]
if headers if headers
.get(&*CONNECTION_HEADER) .get(&CONNECTION_HEADER)
.map(|value| { .map(|value| {
value value
.to_str() .to_str()
.unwrap() .unwrap()
.split(',') .split(',')
.any(|e| e.trim() == *UPGRADE_HEADER) .any(|e| e.trim() == UPGRADE_HEADER)
}) })
.unwrap_or(false) .unwrap_or(false)
{ {
if let Some(upgrade_value) = headers.get(&*UPGRADE_HEADER) { if let Some(upgrade_value) = headers.get(&UPGRADE_HEADER) {
debug!( debug!(
"Found upgrade header with value: {}", "Found upgrade header with value: {}",
upgrade_value.to_str().unwrap().to_owned() upgrade_value.to_str().unwrap().to_owned()
@ -101,10 +98,10 @@ fn get_upgrade_type(headers: &HeaderMap) -> Option<String> {
} }
fn remove_connection_headers(headers: &mut HeaderMap) { fn remove_connection_headers(headers: &mut HeaderMap) {
if headers.get(&*CONNECTION_HEADER).is_some() { if headers.get(&CONNECTION_HEADER).is_some() {
debug!("Removing connection headers"); debug!("Removing connection headers");
let value = headers.get(&*CONNECTION_HEADER).cloned().unwrap(); let value = headers.get(&CONNECTION_HEADER).cloned().unwrap();
for name in value.to_str().unwrap().split(',') { for name in value.to_str().unwrap().split(',') {
if !name.trim().is_empty() { if !name.trim().is_empty() {
@ -114,6 +111,7 @@ fn remove_connection_headers(headers: &mut HeaderMap) {
} }
} }
#[cfg_attr(feature = "__bench", visibility::make(pub))]
fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> { fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> {
info!("Creating proxied response"); info!("Creating proxied response");
@ -123,6 +121,7 @@ fn create_proxied_response<B>(mut response: Response<B>) -> Response<B> {
response response
} }
#[cfg_attr(feature = "__bench", visibility::make(pub))]
fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String { fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String {
debug!("Building forward uri"); debug!("Building forward uri");
@ -201,6 +200,7 @@ fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> String {
url.parse().unwrap() url.parse().unwrap()
} }
#[cfg_attr(feature = "__bench", visibility::make(pub))]
fn create_proxied_request<B>( fn create_proxied_request<B>(
client_ip: IpAddr, client_ip: IpAddr,
forward_url: &str, forward_url: &str,
@ -211,13 +211,13 @@ fn create_proxied_request<B>(
let contains_te_trailers_value = request let contains_te_trailers_value = request
.headers() .headers()
.get(&*TE_HEADER) .get(&TE_HEADER)
.map(|value| { .map(|value| {
value value
.to_str() .to_str()
.unwrap() .unwrap()
.split(',') .split(',')
.any(|e| e.trim() == *TRAILERS_HEADER) .any(|e| e.trim() == TRAILERS_HEADER)
}) })
.unwrap_or(false); .unwrap_or(false);
@ -239,7 +239,7 @@ fn create_proxied_request<B>(
request request
.headers_mut() .headers_mut()
.insert(&*TE_HEADER, HeaderValue::from_static("trailers")); .insert(&TE_HEADER, HeaderValue::from_static("trailers"));
} }
if let Some(value) = upgrade_type { if let Some(value) = upgrade_type {
@ -247,14 +247,14 @@ fn create_proxied_request<B>(
request request
.headers_mut() .headers_mut()
.insert(&*UPGRADE_HEADER, value.parse().unwrap()); .insert(&UPGRADE_HEADER, value.parse().unwrap());
request request
.headers_mut() .headers_mut()
.insert(&*CONNECTION_HEADER, HeaderValue::from_static("UPGRADE")); .insert(&CONNECTION_HEADER, HeaderValue::from_static("UPGRADE"));
} }
// Add forwarding information in the headers // Add forwarding information in the headers
match request.headers_mut().entry(&*X_FORWARDED_FOR) { match request.headers_mut().entry(&X_FORWARDED_FOR) {
hyper::header::Entry::Vacant(entry) => { hyper::header::Entry::Vacant(entry) => {
debug!("X-Fowraded-for header was vacant"); debug!("X-Fowraded-for header was vacant");
entry.insert(client_ip.to_string().parse()?); entry.insert(client_ip.to_string().parse()?);
@ -362,27 +362,3 @@ impl<T: hyper::client::connect::Connect + Clone + Send + Sync + 'static> Reverse
call::<T>(client_ip, forward_uri, request, &self.client).await call::<T>(client_ip, forward_uri, request, &self.client).await
} }
} }
#[cfg(feature = "__bench")]
pub mod benches {
pub fn hop_headers() -> &'static [crate::HeaderName] {
&*super::HOP_HEADERS
}
pub fn create_proxied_response<T>(response: crate::Response<T>) {
super::create_proxied_response(response);
}
pub fn forward_uri<B>(forward_url: &str, req: &crate::Request<B>) {
super::forward_uri(forward_url, req);
}
pub fn create_proxied_request<B>(
client_ip: crate::IpAddr,
forward_url: &str,
request: crate::Request<B>,
upgrade_type: Option<&String>,
) {
super::create_proxied_request(client_ip, forward_url, request, upgrade_type).unwrap();
}
}