forked from hyperium/hyper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rs
149 lines (126 loc) · 4.49 KB
/
server.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![feature(test)]
#![deny(warnings)]
extern crate futures;
extern crate hyper;
extern crate test;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc;
use futures::Future;
use futures::sync::oneshot;
use hyper::header::{ContentLength, ContentType, TransferEncoding};
use hyper::server::{self, Service};
macro_rules! bench_server {
($b:ident, $handler:ident, $total_bytes:expr) => {
let (_until_tx, until_rx) = oneshot::channel();
let addr = {
let (addr_tx, addr_rx) = mpsc::channel();
::std::thread::spawn(move || {
let addr = "127.0.0.1:0".parse().unwrap();
let srv = hyper::server::Http::new().bind(&addr, || Ok($handler)).unwrap();
let addr = srv.local_addr().unwrap();
addr_tx.send(addr).unwrap();
srv.run_until(until_rx.map_err(|_| ())).unwrap();
});
addr_rx.recv().unwrap()
};
let mut tcp = TcpStream::connect(addr).unwrap();
let mut buf = [0u8; 8192];
$b.bytes = 35 + $total_bytes;
$b.iter(|| {
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap();
let mut sum = 0;
while sum < $total_bytes {
sum += tcp.read(&mut buf).unwrap();
}
assert_eq!(sum, $total_bytes);
})
}
}
#[bench]
fn bench_server_tcp_throughput_fixedsize_small_payload(b: &mut test::Bencher) {
bench_server!(b, FixedSizeSmallPayload, 130);
}
#[bench]
fn bench_server_tcp_throughput_chunked_small_payload(b: &mut test::Bencher) {
bench_server!(b, ChunkedSmallPayload, 148);
}
#[bench]
fn bench_server_tcp_throughput_chunked_large_payload(b: &mut test::Bencher) {
bench_server!(b, ChunkedLargePayload, 13_000_140);
}
#[bench]
fn bench_raw_tcp_throughput_small_payload(b: &mut test::Bencher) {
let (tx, rx) = mpsc::channel();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
::std::thread::spawn(move || {
let mut sock = listener.accept().unwrap().0;
let mut buf = [0u8; 8192];
while rx.try_recv().is_err() {
sock.read(&mut buf).unwrap();
sock.write_all(b"\
HTTP/1.1 200 OK\r\n\
Content-Length: 13\r\n\
Content-Type: text/plain; charset=utf-8\r\n\
Date: Fri, 12 May 2017 18:21:45 GMT\r\n\
\r\n\
Hello, World!\
").unwrap();
}
});
let mut tcp = TcpStream::connect(addr).unwrap();
let mut buf = [0u8; 4096];
b.bytes = 130 + 35;
b.iter(|| {
tcp.write_all(b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n").unwrap();
let n = tcp.read(&mut buf).unwrap();
assert_eq!(n, 130);
});
tx.send(()).unwrap();
}
struct FixedSizeSmallPayload;
impl Service for FixedSizeSmallPayload {
type Request = server::Request;
type Response = server::Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Self::Response, hyper::Error>;
fn call(&self, _req: Self::Request) -> Self::Future {
::futures::finished(
server::Response::new()
.with_header(ContentLength("Hello, World!".len() as u64))
.with_header(ContentType::plaintext())
.with_body("Hello, World!")
)
}
}
struct ChunkedSmallPayload;
impl Service for ChunkedSmallPayload {
type Request = server::Request;
type Response = server::Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Self::Response, hyper::Error>;
fn call(&self, _req: Self::Request) -> Self::Future {
::futures::finished(
server::Response::new()
.with_header(TransferEncoding::chunked())
.with_header(ContentType::plaintext())
.with_body("Hello, World!")
)
}
}
struct ChunkedLargePayload;
impl Service for ChunkedLargePayload {
type Request = server::Request;
type Response = server::Response;
type Error = hyper::Error;
type Future = ::futures::Finished<Self::Response, hyper::Error>;
fn call(&self, _req: Self::Request) -> Self::Future {
::futures::finished(
server::Response::new()
.with_header(TransferEncoding::chunked())
.with_header(ContentType::plaintext())
.with_body((0..1_000_000).flat_map(|_| "Hello, World!".bytes()).collect::<Vec<u8>>())
)
}
}