2019-02-26 17:36:05 -05:00
|
|
|
/// To run this benchmark:
|
|
|
|
///
|
|
|
|
/// > DENO_BUILD_MODE=release ./tools/build.py && \
|
|
|
|
/// ./target/release/deno_core_http_bench --multi-thread
|
2020-01-05 11:56:18 -05:00
|
|
|
extern crate deno_core;
|
2019-02-26 17:36:05 -05:00
|
|
|
extern crate futures;
|
|
|
|
extern crate libc;
|
2019-12-15 05:47:26 -05:00
|
|
|
extern crate num_cpus;
|
2019-02-26 17:36:05 -05:00
|
|
|
extern crate tokio;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-12-15 05:47:26 -05:00
|
|
|
use futures::future::Future;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-12-15 05:47:26 -05:00
|
|
|
use futures::task::{Context, Poll};
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::env;
|
2019-10-28 20:42:44 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::ErrorKind;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::sync::Mutex;
|
2019-10-23 12:32:28 -04:00
|
|
|
use std::sync::MutexGuard;
|
2019-12-15 05:47:26 -05:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use tokio::io::AsyncWrite;
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
static LOGGER: Logger = Logger;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
struct Logger;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
impl log::Log for Logger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
|
|
metadata.level() <= log::max_level()
|
|
|
|
}
|
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
println!("{} - {}", record.level(), record.args());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2019-03-11 17:57:36 -04:00
|
|
|
pub struct Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
pub promise_id: i32,
|
2019-03-11 17:57:36 -04:00
|
|
|
pub arg: i32,
|
|
|
|
pub result: i32,
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
impl Into<Buf> for Record {
|
|
|
|
fn into(self) -> Buf {
|
2019-08-07 14:02:29 -04:00
|
|
|
let buf32 = vec![self.promise_id, self.arg, self.result].into_boxed_slice();
|
|
|
|
let ptr = Box::into_raw(buf32) as *mut [u8; 3 * 4];
|
2019-03-14 19:17:52 -04:00
|
|
|
unsafe { Box::from_raw(ptr) }
|
|
|
|
}
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
impl From<&[u8]> for Record {
|
|
|
|
fn from(s: &[u8]) -> Record {
|
2019-04-17 09:25:51 -04:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-03-14 19:17:52 -04:00
|
|
|
let ptr = s.as_ptr() as *const i32;
|
2019-08-07 14:02:29 -04:00
|
|
|
let ints = unsafe { std::slice::from_raw_parts(ptr, 3) };
|
2019-03-14 19:17:52 -04:00
|
|
|
Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
promise_id: ints[0],
|
2019-08-07 14:02:29 -04:00
|
|
|
arg: ints[1],
|
|
|
|
result: ints[2],
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
impl From<Buf> for Record {
|
|
|
|
fn from(buf: Buf) -> Record {
|
2019-08-07 14:02:29 -04:00
|
|
|
assert_eq!(buf.len(), 3 * 4);
|
2019-04-17 09:25:51 -04:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-08-07 14:02:29 -04:00
|
|
|
let ptr = Box::into_raw(buf) as *mut [i32; 3];
|
2019-03-14 19:17:52 -04:00
|
|
|
let ints: Box<[i32]> = unsafe { Box::from_raw(ptr) };
|
2019-08-07 14:02:29 -04:00
|
|
|
assert_eq!(ints.len(), 3);
|
2019-03-14 19:17:52 -04:00
|
|
|
Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
promise_id: ints[0],
|
2019-08-07 14:02:29 -04:00
|
|
|
arg: ints[1],
|
|
|
|
result: ints[2],
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
#[test]
|
|
|
|
fn test_record_from() {
|
|
|
|
let r = Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
promise_id: 1,
|
2019-03-14 19:17:52 -04:00
|
|
|
arg: 3,
|
|
|
|
result: 4,
|
|
|
|
};
|
|
|
|
let expected = r.clone();
|
|
|
|
let buf: Buf = r.into();
|
|
|
|
#[cfg(target_endian = "little")]
|
|
|
|
assert_eq!(
|
|
|
|
buf,
|
2019-08-07 14:02:29 -04:00
|
|
|
vec![1u8, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0].into_boxed_slice()
|
2019-03-14 19:17:52 -04:00
|
|
|
);
|
|
|
|
let actual = Record::from(buf);
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
// TODO test From<&[u8]> for Record
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
pub type HttpOp = dyn Future<Output = Result<i32, std::io::Error>> + Send;
|
2019-04-23 18:58:00 -04:00
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
pub type HttpOpHandler =
|
2019-11-16 19:17:47 -05:00
|
|
|
fn(record: Record, zero_copy_buf: Option<PinnedBuf>) -> Pin<Box<HttpOp>>;
|
2019-09-30 14:59:44 -04:00
|
|
|
|
|
|
|
fn http_op(
|
|
|
|
handler: HttpOpHandler,
|
|
|
|
) -> impl Fn(&[u8], Option<PinnedBuf>) -> CoreOp {
|
|
|
|
move |control: &[u8], zero_copy_buf: Option<PinnedBuf>| -> CoreOp {
|
|
|
|
let record = Record::from(control);
|
|
|
|
let is_sync = record.promise_id == 0;
|
|
|
|
let op = handler(record.clone(), zero_copy_buf);
|
2019-12-23 09:59:44 -05:00
|
|
|
let mut record_a = record;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
match op.await {
|
|
|
|
Ok(result) => record_a.result = result,
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("unexpected err {}", err);
|
|
|
|
record_a.result = -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(record_a.into())
|
|
|
|
};
|
2019-05-01 18:22:32 -04:00
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
if is_sync {
|
2019-11-16 19:17:47 -05:00
|
|
|
Op::Sync(futures::executor::block_on(fut).unwrap())
|
2019-09-30 14:59:44 -04:00
|
|
|
} else {
|
2019-11-16 19:17:47 -05:00
|
|
|
Op::Async(fut.boxed())
|
2019-09-30 14:59:44 -04:00
|
|
|
}
|
2019-05-01 18:22:32 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
fn main() {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
2019-04-21 11:34:18 -04:00
|
|
|
// NOTE: `--help` arg will display V8 help and exit
|
2020-01-05 11:56:18 -05:00
|
|
|
let args = deno_core::v8_set_flags(args);
|
2019-04-07 22:40:58 -04:00
|
|
|
|
|
|
|
log::set_logger(&LOGGER).unwrap();
|
|
|
|
log::set_max_level(if args.iter().any(|a| a == "-D") {
|
|
|
|
log::LevelFilter::Debug
|
|
|
|
} else {
|
|
|
|
log::LevelFilter::Warn
|
|
|
|
});
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
let js_source = include_str!("http_bench.js");
|
|
|
|
|
|
|
|
let startup_data = StartupData::Script(Script {
|
|
|
|
source: js_source,
|
|
|
|
filename: "http_bench.js",
|
|
|
|
});
|
|
|
|
|
2020-01-05 11:56:18 -05:00
|
|
|
let isolate = deno_core::Isolate::new(startup_data, false);
|
2019-11-16 19:17:47 -05:00
|
|
|
isolate.register_op("listen", http_op(op_listen));
|
|
|
|
isolate.register_op("accept", http_op(op_accept));
|
|
|
|
isolate.register_op("read", http_op(op_read));
|
|
|
|
isolate.register_op("write", http_op(op_write));
|
|
|
|
isolate.register_op("close", http_op(op_close));
|
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
let multi_thread = args.iter().any(|a| a == "--multi-thread");
|
2019-11-16 19:17:47 -05:00
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
println!(
|
|
|
|
"num cpus; logical: {}; physical: {}",
|
|
|
|
num_cpus::get(),
|
|
|
|
num_cpus::get_physical()
|
|
|
|
);
|
|
|
|
let mut builder = tokio::runtime::Builder::new();
|
|
|
|
let builder = if multi_thread {
|
2019-02-26 17:36:05 -05:00
|
|
|
println!("multi-thread");
|
2019-12-15 05:47:26 -05:00
|
|
|
builder.threaded_scheduler()
|
2019-02-26 17:36:05 -05:00
|
|
|
} else {
|
|
|
|
println!("single-thread");
|
2019-12-15 05:47:26 -05:00
|
|
|
builder.basic_scheduler()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut runtime = builder
|
|
|
|
.enable_io()
|
|
|
|
.build()
|
|
|
|
.expect("Unable to create tokio runtime");
|
|
|
|
let result = runtime.block_on(isolate.boxed());
|
|
|
|
js_check(result);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:42:44 -04:00
|
|
|
pub fn bad_resource() -> Error {
|
|
|
|
Error::new(ErrorKind::NotFound, "bad resource id")
|
|
|
|
}
|
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
struct TcpListener(tokio::net::TcpListener);
|
2019-10-23 12:32:28 -04:00
|
|
|
|
2019-11-06 12:17:28 -05:00
|
|
|
impl Resource for TcpListener {}
|
2019-10-23 12:32:28 -04:00
|
|
|
|
|
|
|
struct TcpStream(tokio::net::TcpStream);
|
|
|
|
|
2019-11-06 12:17:28 -05:00
|
|
|
impl Resource for TcpStream {}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
|
|
|
lazy_static! {
|
2019-10-23 12:32:28 -04:00
|
|
|
static ref RESOURCE_TABLE: Mutex<ResourceTable> =
|
|
|
|
Mutex::new(ResourceTable::default());
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 12:32:28 -04:00
|
|
|
fn lock_resource_table<'a>() -> MutexGuard<'a, ResourceTable> {
|
|
|
|
RESOURCE_TABLE.lock().unwrap()
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
struct Accept {
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Accept {
|
|
|
|
type Output = Result<(tokio::net::TcpStream, SocketAddr), std::io::Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
|
|
|
|
let mut table = lock_resource_table();
|
|
|
|
match table.get_mut::<TcpListener>(inner.rid) {
|
|
|
|
None => Poll::Ready(Err(bad_resource())),
|
|
|
|
Some(listener) => {
|
|
|
|
let listener = &mut listener.0;
|
|
|
|
listener.poll_accept(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_accept(
|
|
|
|
record: Record,
|
|
|
|
_zero_copy_buf: Option<PinnedBuf>,
|
|
|
|
) -> Pin<Box<HttpOp>> {
|
2019-10-23 12:32:28 -04:00
|
|
|
let rid = record.arg as u32;
|
|
|
|
debug!("accept {}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let (stream, addr) = Accept { rid }.await?;
|
2019-10-23 12:32:28 -04:00
|
|
|
debug!("accept success {}", addr);
|
|
|
|
let mut table = lock_resource_table();
|
2019-11-06 12:17:28 -05:00
|
|
|
let rid = table.add("tcpStream", Box::new(TcpStream(stream)));
|
2019-12-15 05:47:26 -05:00
|
|
|
Ok(rid as i32)
|
|
|
|
};
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fut.boxed()
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
fn op_listen(
|
|
|
|
_record: Record,
|
|
|
|
_zero_copy_buf: Option<PinnedBuf>,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> Pin<Box<HttpOp>> {
|
2019-03-11 17:57:36 -04:00
|
|
|
debug!("listen");
|
2019-12-15 05:47:26 -05:00
|
|
|
let fut = async {
|
|
|
|
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
|
|
|
|
let listener = tokio::net::TcpListener::bind(&addr).await?;
|
|
|
|
let mut table = lock_resource_table();
|
|
|
|
let rid = table.add("tcpListener", Box::new(TcpListener(listener)));
|
|
|
|
Ok(rid as i32)
|
|
|
|
};
|
|
|
|
|
|
|
|
fut.boxed()
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_close(
|
|
|
|
record: Record,
|
|
|
|
_zero_copy_buf: Option<PinnedBuf>,
|
|
|
|
) -> Pin<Box<HttpOp>> {
|
2019-03-11 17:57:36 -04:00
|
|
|
debug!("close");
|
2019-12-15 05:47:26 -05:00
|
|
|
let fut = async move {
|
|
|
|
let rid = record.arg as u32;
|
|
|
|
let mut table = lock_resource_table();
|
|
|
|
match table.close(rid) {
|
|
|
|
Some(_) => Ok(0),
|
|
|
|
None => Err(bad_resource()),
|
|
|
|
}
|
2019-10-23 12:32:28 -04:00
|
|
|
};
|
2019-11-16 19:17:47 -05:00
|
|
|
fut.boxed()
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
struct Read {
|
|
|
|
rid: ResourceId,
|
|
|
|
buf: PinnedBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Read {
|
|
|
|
type Output = Result<usize, std::io::Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
let mut table = lock_resource_table();
|
|
|
|
|
|
|
|
match table.get_mut::<TcpStream>(inner.rid) {
|
|
|
|
None => Poll::Ready(Err(bad_resource())),
|
|
|
|
Some(stream) => {
|
|
|
|
let pinned_stream = Pin::new(&mut stream.0);
|
|
|
|
pinned_stream.poll_read(cx, &mut inner.buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_read(
|
|
|
|
record: Record,
|
|
|
|
zero_copy_buf: Option<PinnedBuf>,
|
|
|
|
) -> Pin<Box<HttpOp>> {
|
2019-10-23 12:32:28 -04:00
|
|
|
let rid = record.arg as u32;
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("read rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
let zero_copy_buf = zero_copy_buf.unwrap();
|
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let nread = Read {
|
|
|
|
rid,
|
|
|
|
buf: zero_copy_buf,
|
|
|
|
}
|
|
|
|
.await?;
|
2019-11-26 21:07:40 -05:00
|
|
|
debug!("read success {}", nread);
|
2019-12-15 05:47:26 -05:00
|
|
|
Ok(nread as i32)
|
|
|
|
};
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fut.boxed()
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-12-15 05:47:26 -05:00
|
|
|
struct Write {
|
|
|
|
rid: ResourceId,
|
|
|
|
buf: PinnedBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for Write {
|
|
|
|
type Output = Result<usize, std::io::Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
let mut table = lock_resource_table();
|
|
|
|
|
|
|
|
match table.get_mut::<TcpStream>(inner.rid) {
|
|
|
|
None => Poll::Ready(Err(bad_resource())),
|
|
|
|
Some(stream) => {
|
|
|
|
let pinned_stream = Pin::new(&mut stream.0);
|
|
|
|
pinned_stream.poll_write(cx, &inner.buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_write(
|
|
|
|
record: Record,
|
|
|
|
zero_copy_buf: Option<PinnedBuf>,
|
|
|
|
) -> Pin<Box<HttpOp>> {
|
2019-10-23 12:32:28 -04:00
|
|
|
let rid = record.arg as u32;
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("write rid={}", rid);
|
2019-04-28 15:31:10 -04:00
|
|
|
let zero_copy_buf = zero_copy_buf.unwrap();
|
2019-12-15 05:47:26 -05:00
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let nwritten = Write {
|
|
|
|
rid,
|
|
|
|
buf: zero_copy_buf,
|
|
|
|
}
|
|
|
|
.await?;
|
2019-11-26 21:07:40 -05:00
|
|
|
debug!("write success {}", nwritten);
|
2019-12-15 05:47:26 -05:00
|
|
|
Ok(nwritten as i32)
|
|
|
|
};
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fut.boxed()
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-07-10 18:53:48 -04:00
|
|
|
fn js_check(r: Result<(), ErrBox>) {
|
2019-02-26 17:36:05 -05:00
|
|
|
if let Err(e) = r {
|
|
|
|
panic!(e.to_string());
|
|
|
|
}
|
|
|
|
}
|