2018-10-15 16:44:35 -04:00
|
|
|
#!/usr/bin/env python
|
2020-01-02 15:13:47 -05:00
|
|
|
# Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2018-10-15 16:44:35 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import subprocess
|
2019-09-11 16:47:42 -04:00
|
|
|
import util
|
2019-10-28 22:04:24 -04:00
|
|
|
import third_party
|
2018-10-15 16:44:35 -04:00
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
# Some of the benchmarks in this file have been renamed. In case the history
|
|
|
|
# somehow gets messed up:
|
|
|
|
# "node_http" was once called "node"
|
|
|
|
# "deno_tcp" was once called "deno"
|
|
|
|
# "deno_http" was once called "deno_net_http"
|
|
|
|
|
2020-01-13 07:34:07 -05:00
|
|
|
DURATION = "20s"
|
2018-10-15 16:44:35 -04:00
|
|
|
|
2019-06-07 15:36:26 -04:00
|
|
|
LAST_PORT = 4544
|
|
|
|
|
|
|
|
|
2019-09-30 12:35:48 -04:00
|
|
|
def server_addr(port):
|
|
|
|
return "0.0.0.0:%s" % port
|
|
|
|
|
|
|
|
|
|
|
|
def get_port(port=None):
|
2019-06-07 15:36:26 -04:00
|
|
|
global LAST_PORT
|
|
|
|
if port is None:
|
|
|
|
port = LAST_PORT
|
|
|
|
LAST_PORT = LAST_PORT + 1
|
2019-09-30 12:35:48 -04:00
|
|
|
# Return port as str because all usages below are as a str and having it an
|
|
|
|
# integer just adds complexity.
|
|
|
|
return str(port)
|
2019-06-07 15:36:26 -04:00
|
|
|
|
2018-10-15 16:44:35 -04:00
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
def deno_tcp(deno_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
deno_cmd = [
|
2020-01-15 19:21:35 -05:00
|
|
|
deno_exe, "run", "--allow-net", "tools/deno_tcp.ts",
|
2019-09-30 12:35:48 -04:00
|
|
|
server_addr(port)
|
|
|
|
]
|
2019-08-26 07:48:40 -04:00
|
|
|
print "http_benchmark testing DENO tcp."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(deno_cmd, port)
|
2018-10-16 21:02:16 -04:00
|
|
|
|
2018-10-15 16:44:35 -04:00
|
|
|
|
2019-07-31 11:02:20 -04:00
|
|
|
def deno_tcp_current_thread(deno_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
2019-07-31 11:02:20 -04:00
|
|
|
deno_cmd = [
|
|
|
|
deno_exe, "run", "--current-thread", "--allow-net",
|
2020-01-15 19:21:35 -05:00
|
|
|
"tools/deno_tcp.ts",
|
2019-09-30 12:35:48 -04:00
|
|
|
server_addr(port)
|
2019-07-31 11:02:20 -04:00
|
|
|
]
|
2019-08-26 07:48:40 -04:00
|
|
|
print "http_benchmark testing DENO tcp (single-thread)."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(deno_cmd, port)
|
2019-07-31 11:02:20 -04:00
|
|
|
|
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
def deno_http(deno_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
2018-12-07 16:36:16 -05:00
|
|
|
deno_cmd = [
|
2020-01-15 19:21:35 -05:00
|
|
|
deno_exe, "run", "--allow-net", "std/http/http_bench.ts",
|
2019-09-30 12:35:48 -04:00
|
|
|
server_addr(port)
|
2018-12-07 16:36:16 -05:00
|
|
|
]
|
|
|
|
print "http_benchmark testing DENO using net/http."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(deno_cmd, port)
|
2018-12-07 16:36:16 -05:00
|
|
|
|
|
|
|
|
2019-06-06 22:46:18 -04:00
|
|
|
def deno_tcp_proxy(deno_exe, hyper_hello_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
origin_port = get_port()
|
2019-06-06 22:46:18 -04:00
|
|
|
deno_cmd = [
|
2020-01-15 19:21:35 -05:00
|
|
|
deno_exe, "run", "--allow-net", "tools/deno_tcp_proxy.ts",
|
2019-09-30 12:35:48 -04:00
|
|
|
server_addr(port),
|
|
|
|
server_addr(origin_port)
|
2019-06-06 22:46:18 -04:00
|
|
|
]
|
|
|
|
print "http_proxy_benchmark testing DENO using net/tcp."
|
|
|
|
return run(
|
|
|
|
deno_cmd,
|
2019-09-30 12:35:48 -04:00
|
|
|
port,
|
|
|
|
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_port))
|
2019-06-06 22:46:18 -04:00
|
|
|
|
|
|
|
|
2019-06-06 12:44:35 -04:00
|
|
|
def deno_http_proxy(deno_exe, hyper_hello_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
origin_port = get_port()
|
2019-06-06 12:44:35 -04:00
|
|
|
deno_cmd = [
|
2020-01-15 19:21:35 -05:00
|
|
|
deno_exe, "run", "--allow-net", "tools/deno_http_proxy.ts",
|
2019-09-30 12:35:48 -04:00
|
|
|
server_addr(port),
|
|
|
|
server_addr(origin_port)
|
2019-06-06 12:44:35 -04:00
|
|
|
]
|
|
|
|
print "http_proxy_benchmark testing DENO using net/http."
|
|
|
|
return run(
|
|
|
|
deno_cmd,
|
2019-09-30 12:35:48 -04:00
|
|
|
port,
|
|
|
|
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_port))
|
2019-06-06 12:44:35 -04:00
|
|
|
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
def deno_core_single(exe):
|
|
|
|
print "http_benchmark testing deno_core_single"
|
2019-09-30 12:35:48 -04:00
|
|
|
return run([exe, "--single-thread"], 4544)
|
2019-02-26 17:36:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
def deno_core_multi(exe):
|
|
|
|
print "http_benchmark testing deno_core_multi"
|
2019-09-30 12:35:48 -04:00
|
|
|
return run([exe, "--multi-thread"], 4544)
|
2019-02-26 17:36:05 -05:00
|
|
|
|
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
def node_http():
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
node_cmd = ["node", "tools/node_http.js", port]
|
2018-10-15 16:44:35 -04:00
|
|
|
print "http_benchmark testing NODE."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(node_cmd, port)
|
2018-10-16 21:02:16 -04:00
|
|
|
|
|
|
|
|
2019-06-06 12:44:35 -04:00
|
|
|
def node_http_proxy(hyper_hello_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
origin_port = get_port()
|
|
|
|
node_cmd = ["node", "tools/node_http_proxy.js", port, origin_port]
|
2019-06-06 12:44:35 -04:00
|
|
|
print "http_proxy_benchmark testing NODE."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(node_cmd, port, None,
|
|
|
|
http_proxy_origin(hyper_hello_exe, origin_port))
|
2019-06-06 12:44:35 -04:00
|
|
|
|
|
|
|
|
2019-06-06 22:46:18 -04:00
|
|
|
def node_tcp_proxy(hyper_hello_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
origin_port = get_port()
|
|
|
|
node_cmd = ["node", "tools/node_tcp_proxy.js", port, origin_port]
|
2019-06-06 22:46:18 -04:00
|
|
|
print "http_proxy_benchmark testing NODE tcp."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(node_cmd, port, None,
|
|
|
|
http_proxy_origin(hyper_hello_exe, origin_port))
|
2019-06-06 22:46:18 -04:00
|
|
|
|
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
def node_tcp():
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
node_cmd = ["node", "tools/node_tcp.js", port]
|
2018-10-23 18:02:30 -04:00
|
|
|
print "http_benchmark testing node_tcp.js"
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(node_cmd, port)
|
2018-10-23 18:02:30 -04:00
|
|
|
|
|
|
|
|
2019-09-30 12:35:48 -04:00
|
|
|
def http_proxy_origin(hyper_hello_exe, port):
|
|
|
|
return [hyper_hello_exe, port]
|
2019-06-06 12:44:35 -04:00
|
|
|
|
|
|
|
|
2019-05-14 15:22:50 -04:00
|
|
|
def hyper_http(hyper_hello_exe):
|
2019-09-30 12:35:48 -04:00
|
|
|
port = get_port()
|
|
|
|
hyper_cmd = [hyper_hello_exe, port]
|
2018-10-20 22:56:16 -04:00
|
|
|
print "http_benchmark testing RUST hyper."
|
2019-09-30 12:35:48 -04:00
|
|
|
return run(hyper_cmd, port)
|
2018-10-20 22:56:16 -04:00
|
|
|
|
2018-10-15 16:44:35 -04:00
|
|
|
|
2019-04-17 10:52:36 -04:00
|
|
|
def http_benchmark(build_dir):
|
|
|
|
hyper_hello_exe = os.path.join(build_dir, "hyper_hello")
|
2019-09-06 20:32:58 -04:00
|
|
|
core_http_bench_exe = os.path.join(build_dir,
|
|
|
|
"examples/deno_core_http_bench")
|
2019-04-17 10:52:36 -04:00
|
|
|
deno_exe = os.path.join(build_dir, "deno")
|
2019-03-24 23:36:27 -04:00
|
|
|
return {
|
2019-05-14 15:22:50 -04:00
|
|
|
# "deno_tcp" was once called "deno"
|
|
|
|
"deno_tcp": deno_tcp(deno_exe),
|
2019-07-31 11:02:20 -04:00
|
|
|
"deno_tcp_current_thread": deno_tcp_current_thread(deno_exe),
|
2019-05-14 15:22:50 -04:00
|
|
|
# "deno_http" was once called "deno_net_http"
|
|
|
|
"deno_http": deno_http(deno_exe),
|
2019-06-06 12:44:35 -04:00
|
|
|
"deno_proxy": deno_http_proxy(deno_exe, hyper_hello_exe),
|
2019-06-06 22:46:18 -04:00
|
|
|
"deno_proxy_tcp": deno_tcp_proxy(deno_exe, hyper_hello_exe),
|
2019-03-24 23:36:27 -04:00
|
|
|
"deno_core_single": deno_core_single(core_http_bench_exe),
|
|
|
|
"deno_core_multi": deno_core_multi(core_http_bench_exe),
|
2019-05-14 15:22:50 -04:00
|
|
|
# "node_http" was once called "node"
|
|
|
|
"node_http": node_http(),
|
2019-06-06 12:44:35 -04:00
|
|
|
"node_proxy": node_http_proxy(hyper_hello_exe),
|
2019-06-06 22:46:18 -04:00
|
|
|
"node_proxy_tcp": node_tcp_proxy(hyper_hello_exe),
|
2019-05-14 15:22:50 -04:00
|
|
|
"node_tcp": node_tcp(),
|
|
|
|
"hyper": hyper_http(hyper_hello_exe)
|
2019-03-24 23:36:27 -04:00
|
|
|
}
|
2018-10-15 16:44:35 -04:00
|
|
|
|
|
|
|
|
2019-09-30 12:35:48 -04:00
|
|
|
def run(server_cmd, port, merge_env=None, origin_cmd=None):
|
2019-06-07 15:36:26 -04:00
|
|
|
|
2018-10-15 16:44:35 -04:00
|
|
|
# Run deno echo server in the background.
|
2018-12-07 16:36:16 -05:00
|
|
|
if merge_env is None:
|
|
|
|
env = None
|
|
|
|
else:
|
|
|
|
env = os.environ.copy()
|
|
|
|
for key, value in merge_env.iteritems():
|
|
|
|
env[key] = value
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
# Wait for port 4544 to become available.
|
|
|
|
# TODO Need to use SO_REUSEPORT with tokio::net::TcpListener.
|
|
|
|
time.sleep(5)
|
|
|
|
|
2019-06-06 12:44:35 -04:00
|
|
|
origin = None
|
|
|
|
if origin_cmd is not None:
|
|
|
|
origin = subprocess.Popen(origin_cmd, env=env)
|
|
|
|
|
2019-09-30 12:35:48 -04:00
|
|
|
print server_cmd
|
2018-12-07 16:36:16 -05:00
|
|
|
server = subprocess.Popen(server_cmd, env=env)
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-09-30 12:35:48 -04:00
|
|
|
time.sleep(5) # wait for server to wake up. TODO racy.
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2018-10-15 16:44:35 -04:00
|
|
|
try:
|
2019-10-28 22:04:24 -04:00
|
|
|
wrk = third_party.get_prebuilt_tool_path("wrk")
|
|
|
|
assert os.path.exists(wrk)
|
|
|
|
cmd = "%s -d %s --latency http://127.0.0.1:%s/" % (wrk, DURATION, port)
|
2018-10-15 16:44:35 -04:00
|
|
|
print cmd
|
|
|
|
output = subprocess.check_output(cmd, shell=True)
|
2019-03-24 23:36:27 -04:00
|
|
|
stats = util.parse_wrk_output(output)
|
2018-10-15 16:44:35 -04:00
|
|
|
print output
|
2019-03-24 23:36:27 -04:00
|
|
|
return stats
|
2018-10-15 16:44:35 -04:00
|
|
|
finally:
|
|
|
|
server.kill()
|
2019-06-06 12:44:35 -04:00
|
|
|
if origin is not None:
|
|
|
|
origin.kill()
|
2018-10-15 16:44:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) < 2:
|
2018-11-08 13:38:20 -05:00
|
|
|
print "Usage ./tools/http_benchmark.py target/debug/deno"
|
2018-10-15 16:44:35 -04:00
|
|
|
sys.exit(1)
|
2019-05-14 15:22:50 -04:00
|
|
|
deno_http(sys.argv[1])
|