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
|
|
|
|
|
|
|
|
#[macro_use]
|
2020-02-06 18:34:40 -05:00
|
|
|
extern crate derive_deref;
|
2019-02-26 17:36:05 -05:00
|
|
|
#[macro_use]
|
2020-02-06 18:34:40 -05:00
|
|
|
extern crate log;
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
use deno_core::Isolate as CoreIsolate;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2020-02-06 18:34:40 -05:00
|
|
|
use futures::future::poll_fn;
|
|
|
|
use futures::prelude::*;
|
|
|
|
use futures::task::Context;
|
|
|
|
use futures::task::Poll;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::convert::TryInto;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::env;
|
2020-02-06 18:34:40 -05:00
|
|
|
use std::fmt::Debug;
|
2019-10-28 20:42:44 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::ErrorKind;
|
2020-02-06 18:34:40 -05:00
|
|
|
use std::mem::size_of;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-06 18:34:40 -05:00
|
|
|
use std::ptr;
|
|
|
|
use std::rc::Rc;
|
2019-12-15 05:47:26 -05:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use tokio::io::AsyncWrite;
|
2020-02-06 18:34:40 -05:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
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()
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
println!("{} - {}", record.level(), record.args());
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
struct Record {
|
|
|
|
pub promise_id: u32,
|
|
|
|
pub rid: u32,
|
2019-03-11 17:57:36 -04:00
|
|
|
pub result: i32,
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
type RecordBuf = [u8; size_of::<Record>()];
|
2019-03-11 17:57:36 -04:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
impl From<&[u8]> for Record {
|
2020-02-06 18:34:40 -05:00
|
|
|
fn from(buf: &[u8]) -> Self {
|
|
|
|
assert_eq!(buf.len(), size_of::<RecordBuf>());
|
|
|
|
unsafe { *(buf as *const _ as *const RecordBuf) }.into()
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
impl From<RecordBuf> for Record {
|
|
|
|
fn from(buf: RecordBuf) -> Self {
|
|
|
|
unsafe {
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
ptr::read_unaligned(&buf as *const _ as *const Self)
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
impl From<Record> for RecordBuf {
|
|
|
|
fn from(record: Record) -> Self {
|
|
|
|
unsafe { ptr::read(&record as *const _ as *const Self) }
|
2019-05-01 18:22:32 -04:00
|
|
|
}
|
2019-03-11 17:57:36 -04:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
struct Isolate {
|
|
|
|
core_isolate: Box<CoreIsolate>, // Unclear why CoreIsolate::new() returns a box.
|
|
|
|
state: State,
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
#[derive(Clone, Default, Deref)]
|
|
|
|
struct State(Rc<RefCell<StateInner>>);
|
2019-10-28 20:42:44 -04:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct StateInner {
|
|
|
|
resource_table: ResourceTable,
|
|
|
|
}
|
2019-10-23 12:32:28 -04:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
impl Isolate {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
let startup_data = StartupData::Script(Script {
|
|
|
|
source: include_str!("http_bench.js"),
|
|
|
|
filename: "http_bench.js",
|
|
|
|
});
|
2019-10-23 12:32:28 -04:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
let mut isolate = Self {
|
|
|
|
core_isolate: CoreIsolate::new(startup_data, false),
|
|
|
|
state: Default::default(),
|
|
|
|
};
|
2019-10-23 12:32:28 -04:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
isolate.register_op("listen", op_listen);
|
|
|
|
isolate.register_op("accept", op_accept);
|
|
|
|
isolate.register_op("read", op_read);
|
|
|
|
isolate.register_op("write", op_write);
|
|
|
|
isolate.register_op("close", op_close);
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
isolate
|
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn register_op<F>(
|
|
|
|
&mut self,
|
|
|
|
name: &'static str,
|
|
|
|
handler: impl Fn(State, u32, Option<ZeroCopyBuf>) -> F + Copy + 'static,
|
|
|
|
) where
|
|
|
|
F: TryFuture,
|
|
|
|
F::Ok: TryInto<i32>,
|
|
|
|
<F::Ok as TryInto<i32>>::Error: Debug,
|
|
|
|
{
|
|
|
|
let state = self.state.clone();
|
|
|
|
let core_handler =
|
|
|
|
move |control_buf: &[u8], zero_copy_buf: Option<ZeroCopyBuf>| -> CoreOp {
|
|
|
|
let state = state.clone();
|
|
|
|
let record = Record::from(control_buf);
|
|
|
|
let is_sync = record.promise_id == 0;
|
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let op = handler(state, record.rid, zero_copy_buf);
|
|
|
|
let result = op
|
|
|
|
.map_ok(|r| r.try_into().expect("op result does not fit in i32"))
|
|
|
|
.unwrap_or_else(|_| -1)
|
|
|
|
.await;
|
|
|
|
Ok(RecordBuf::from(Record { result, ..record })[..].into())
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_sync {
|
|
|
|
Op::Sync(futures::executor::block_on(fut).unwrap())
|
|
|
|
} else {
|
|
|
|
Op::Async(fut.boxed_local())
|
|
|
|
}
|
|
|
|
};
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
self.core_isolate.register_op(name, core_handler);
|
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
impl Future for Isolate {
|
|
|
|
type Output = <CoreIsolate as Future>::Output;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
self.core_isolate.poll_unpin(cx)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_close(
|
|
|
|
state: State,
|
|
|
|
rid: u32,
|
|
|
|
_buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> impl TryFuture<Ok = u32, Error = Error> {
|
|
|
|
debug!("close rid={}", rid);
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let resource_table = &mut state.borrow_mut().resource_table;
|
|
|
|
resource_table
|
|
|
|
.close(rid)
|
|
|
|
.map(|_| 0)
|
|
|
|
.ok_or_else(bad_resource)
|
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
fn op_listen(
|
2020-02-06 18:34:40 -05:00
|
|
|
state: State,
|
|
|
|
_rid: u32,
|
|
|
|
_buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> impl TryFuture<Ok = u32, Error = Error> {
|
2019-03-11 17:57:36 -04:00
|
|
|
debug!("listen");
|
2020-02-06 18:34:40 -05:00
|
|
|
|
|
|
|
async move {
|
2019-12-15 05:47:26 -05:00
|
|
|
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
|
|
|
|
let listener = tokio::net::TcpListener::bind(&addr).await?;
|
2020-02-06 18:34:40 -05:00
|
|
|
let resource_table = &mut state.borrow_mut().resource_table;
|
|
|
|
let rid = resource_table.add("tcpListener", Box::new(listener));
|
|
|
|
Ok(rid)
|
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_accept(
|
|
|
|
state: State,
|
|
|
|
rid: u32,
|
|
|
|
_buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> impl TryFuture<Ok = u32, Error = Error> {
|
|
|
|
debug!("accept rid={}", rid);
|
|
|
|
|
|
|
|
poll_fn(move |cx| {
|
|
|
|
let resource_table = &mut state.borrow_mut().resource_table;
|
|
|
|
let listener = resource_table
|
|
|
|
.get_mut::<TcpListener>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
|
|
|
listener.poll_accept(cx).map_ok(|(stream, _addr)| {
|
|
|
|
resource_table.add("tcpStream", Box::new(stream))
|
|
|
|
})
|
|
|
|
})
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_read(
|
2020-02-06 18:34:40 -05:00
|
|
|
state: State,
|
|
|
|
rid: u32,
|
|
|
|
buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> impl TryFuture<Ok = usize, Error = Error> {
|
|
|
|
let mut buf = buf.unwrap();
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("read rid={}", rid);
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
poll_fn(move |cx| {
|
|
|
|
let resource_table = &mut state.borrow_mut().resource_table;
|
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
|
|
|
Pin::new(stream).poll_read(cx, &mut buf)
|
|
|
|
})
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_write(
|
|
|
|
state: State,
|
|
|
|
rid: u32,
|
|
|
|
buf: Option<ZeroCopyBuf>,
|
|
|
|
) -> impl TryFuture<Ok = usize, Error = Error> {
|
|
|
|
let buf = buf.unwrap();
|
|
|
|
debug!("write rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
poll_fn(move |cx| {
|
|
|
|
let resource_table = &mut state.borrow_mut().resource_table;
|
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
|
|
|
Pin::new(stream).poll_write(cx, &buf)
|
|
|
|
})
|
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn bad_resource() -> Error {
|
|
|
|
Error::new(ErrorKind::NotFound, "bad resource id")
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn main() {
|
|
|
|
log::set_logger(&Logger).unwrap();
|
|
|
|
log::set_max_level(
|
|
|
|
env::args()
|
|
|
|
.find(|a| a == "-D")
|
|
|
|
.map(|_| log::LevelFilter::Debug)
|
|
|
|
.unwrap_or(log::LevelFilter::Warn),
|
|
|
|
);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
// NOTE: `--help` arg will display V8 help and exit
|
|
|
|
deno_core::v8_set_flags(env::args().collect());
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
let isolate = Isolate::new();
|
|
|
|
let mut runtime = tokio::runtime::Builder::new()
|
|
|
|
.basic_scheduler()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
runtime.block_on(isolate).expect("unexpected isolate error");
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_record_from() {
|
|
|
|
let expected = Record {
|
|
|
|
promise_id: 1,
|
|
|
|
rid: 3,
|
|
|
|
result: 4,
|
|
|
|
};
|
|
|
|
let buf = RecordBuf::from(expected);
|
|
|
|
if cfg!(target_endian = "little") {
|
|
|
|
assert_eq!(buf, [1u8, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
let actual = Record::from(buf);
|
|
|
|
assert_eq!(actual, expected);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|