Skip to content

Commit

Permalink
perf(runtime): short-circuit queue_async_op for Poll::Ready (denola…
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Sep 6, 2022
1 parent c0a684c commit d2a408f
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 167 deletions.
19 changes: 19 additions & 0 deletions cli/bench/async_ops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
const queueMicrotask = globalThis.queueMicrotask || process.nextTick;
let [total, count] = typeof Deno !== "undefined"
? Deno.args
: [process.argv[2], process.argv[3]];

total = total ? parseInt(total, 0) : 50;
count = count ? parseInt(count, 10) : 100000;

async function bench(fun) {
const start = Date.now();
for (let i = 0; i < count; i++) await fun();
const elapsed = Date.now() - start;
const rate = Math.floor(count / (elapsed / 1000));
console.log(`time ${elapsed} ms rate ${rate}`);
if (--total) queueMicrotask(() => bench(fun));
}

bench(() => Deno.core.opAsync("op_void_async"));
22 changes: 22 additions & 0 deletions cli/bench/tcp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

const listener = Deno.listen({ port: 4500 });
const response = new TextEncoder().encode(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
);

// Accept a connection and write packets as fast as possible.
async function acceptWrite() {
const conn = await listener.accept();
try {
while (true) {
await conn.write(response);
}
} catch {
// Pass
}
conn.close();
}

await acceptWrite();
await acceptWrite();
4 changes: 2 additions & 2 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ async fn test_resolve_dns() {
let out = String::from_utf8_lossy(&output.stdout);
assert!(!output.status.success());
assert!(err.starts_with("Check file"));
assert!(err.contains(r#"error: Uncaught (in promise) PermissionDenied: Requires net access to "127.0.0.1:4553""#));
assert!(err.contains(r#"error: Uncaught PermissionDenied: Requires net access to "127.0.0.1:4553""#));
assert!(out.is_empty());
}

Expand All @@ -950,7 +950,7 @@ async fn test_resolve_dns() {
let out = String::from_utf8_lossy(&output.stdout);
assert!(!output.status.success());
assert!(err.starts_with("Check file"));
assert!(err.contains(r#"error: Uncaught (in promise) PermissionDenied: Requires net access to "127.0.0.1:4553""#));
assert!(err.contains(r#"error: Uncaught PermissionDenied: Requires net access to "127.0.0.1:4553""#));
assert!(out.is_empty());
}

Expand Down
2 changes: 1 addition & 1 deletion cli/tests/testdata/044_bad_resource.ts.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[WILDCARD]error: Uncaught (in promise) BadResource: Bad resource ID
[WILDCARD]error: Uncaught [WILDCARD] BadResource: Bad resource ID
[WILDCARD]
2 changes: 1 addition & 1 deletion cli/tests/testdata/wasm_unreachable.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: Uncaught (in promise) RuntimeError: unreachable
error: Uncaught [WILDCARD] RuntimeError: unreachable
at <anonymous> (wasm:https://wasm/d1c677ea:1:41)
at [WILDCARD]/wasm_unreachable.js:[WILDCARD]
57 changes: 27 additions & 30 deletions cli/tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,47 +1202,44 @@ Deno.test({}, function fetchWritableRespProps() {
assertEquals(new_.headers.get("x-deno"), "foo");
});

function returnHostHeaderServer(addr: string): Deno.Listener {
const [hostname, port] = addr.split(":");
const listener = Deno.listen({
hostname,
port: Number(port),
}) as Deno.Listener;

listener.accept().then(async (conn: Deno.Conn) => {
const httpConn = Deno.serveHttp(conn);

await httpConn.nextRequest()
.then(async (requestEvent: Deno.RequestEvent | null) => {
const hostHeader = requestEvent?.request.headers.get("Host");
const headersToReturn = hostHeader ? { "Host": hostHeader } : undefined;

await requestEvent?.respondWith(
new Response("", {
status: 200,
headers: headersToReturn,
}),
);
});

httpConn.close();
});

return listener;
}

Deno.test(
{ permissions: { net: true } },
async function fetchFilterOutCustomHostHeader(): Promise<
void
> {
const addr = "127.0.0.1:4511";
const listener = returnHostHeaderServer(addr);
const [hostname, port] = addr.split(":");
const listener = Deno.listen({
hostname,
port: Number(port),
}) as Deno.Listener;

let httpConn: Deno.HttpConn;
listener.accept().then(async (conn: Deno.Conn) => {
httpConn = Deno.serveHttp(conn);

await httpConn.nextRequest()
.then(async (requestEvent: Deno.RequestEvent | null) => {
const hostHeader = requestEvent?.request.headers.get("Host");
const headersToReturn = hostHeader
? { "Host": hostHeader }
: undefined;

await requestEvent?.respondWith(
new Response("", {
status: 200,
headers: headersToReturn,
}),
);
});
});

const response = await fetch(`https://${addr}/`, {
headers: { "Host": "example.com" },
});
await response.text();
listener.close();
httpConn!.close();

assertEquals(response.headers.get("Host"), addr);
},
Expand Down
Loading

0 comments on commit d2a408f

Please sign in to comment.