1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-14 16:33:45 -05:00
denoland-deno/tools/http_benchmark.py

209 lines
5.9 KiB
Python
Raw Normal View History

2018-10-15 16:44:35 -04:00
#!/usr/bin/env python
2019-01-21 14:03:30 -05:00
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
2018-10-15 16:44:35 -04:00
import os
import sys
import time
import subprocess
import util
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"
2018-10-15 16:44:35 -04:00
DURATION = "10s"
LAST_PORT = 4544
def get_addr(port=None):
global LAST_PORT
if port is None:
port = LAST_PORT
LAST_PORT = LAST_PORT + 1
return ("127.0.0.1:%d" % (port))
2018-10-15 16:44:35 -04:00
2019-05-14 15:22:50 -04:00
def deno_tcp(deno_exe):
addr = get_addr()
deno_cmd = [deno_exe, "run", "--allow-net", "tools/deno_tcp.ts", addr]
print "http_benchmark testing DENO tcp."
return run(deno_cmd, addr)
2018-10-15 16:44:35 -04:00
2019-07-31 11:02:20 -04:00
def deno_tcp_current_thread(deno_exe):
addr = get_addr()
deno_cmd = [
deno_exe, "run", "--current-thread", "--allow-net",
"tools/deno_tcp.ts", addr
]
print "http_benchmark testing DENO tcp (single-thread)."
2019-07-31 11:02:20 -04:00
return run(deno_cmd, addr)
2019-05-14 15:22:50 -04:00
def deno_http(deno_exe):
addr = get_addr()
2018-12-07 16:36:16 -05:00
deno_cmd = [
2019-05-03 17:15:16 -04:00
deno_exe, "run", "--allow-net",
"js/deps/https/deno.land/std/http/http_bench.ts", addr
2018-12-07 16:36:16 -05:00
]
print "http_benchmark testing DENO using net/http."
return run(deno_cmd, addr)
2018-12-07 16:36:16 -05:00
def deno_tcp_proxy(deno_exe, hyper_hello_exe):
addr = get_addr()
origin_addr = get_addr()
deno_cmd = [
deno_exe, "run", "--allow-net", "tools/deno_tcp_proxy.ts", addr,
origin_addr
]
print "http_proxy_benchmark testing DENO using net/tcp."
return run(
deno_cmd,
addr,
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
2019-06-06 12:44:35 -04:00
def deno_http_proxy(deno_exe, hyper_hello_exe):
addr = get_addr()
origin_addr = get_addr()
2019-06-06 12:44:35 -04:00
deno_cmd = [
deno_exe, "run", "--allow-net", "tools/deno_http_proxy.ts", addr,
origin_addr
2019-06-06 12:44:35 -04:00
]
print "http_proxy_benchmark testing DENO using net/http."
return run(
deno_cmd,
addr,
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
2019-06-06 12:44:35 -04:00
def deno_core_single(exe):
print "http_benchmark testing deno_core_single"
return run([exe, "--single-thread"], "127.0.0.1:4544")
def deno_core_multi(exe):
print "http_benchmark testing deno_core_multi"
return run([exe, "--multi-thread"], "127.0.0.1:4544")
2019-05-14 15:22:50 -04:00
def node_http():
addr = get_addr()
node_cmd = ["node", "tools/node_http.js", addr.split(":")[1]]
2018-10-15 16:44:35 -04:00
print "http_benchmark testing NODE."
return run(node_cmd, addr)
2019-06-06 12:44:35 -04:00
def node_http_proxy(hyper_hello_exe):
addr = get_addr()
origin_addr = get_addr()
node_cmd = [
"node", "tools/node_http_proxy.js",
addr.split(":")[1],
origin_addr.split(":")[1]
]
2019-06-06 12:44:35 -04:00
print "http_proxy_benchmark testing NODE."
return run(node_cmd, addr, None,
http_proxy_origin(hyper_hello_exe, origin_addr))
2019-06-06 12:44:35 -04:00
def node_tcp_proxy(hyper_hello_exe):
addr = get_addr()
origin_addr = get_addr()
node_cmd = [
"node", "tools/node_tcp_proxy.js",
addr.split(":")[1],
origin_addr.split(":")[1]
]
print "http_proxy_benchmark testing NODE tcp."
return run(node_cmd, addr, None,
http_proxy_origin(hyper_hello_exe, origin_addr))
2019-05-14 15:22:50 -04:00
def node_tcp():
addr = get_addr()
node_cmd = ["node", "tools/node_tcp.js", addr.split(":")[1]]
print "http_benchmark testing node_tcp.js"
return run(node_cmd, addr)
def http_proxy_origin(hyper_hello_exe, addr):
return [hyper_hello_exe, addr.split(":")[1]]
2019-06-06 12:44:35 -04:00
2019-05-14 15:22:50 -04:00
def hyper_http(hyper_hello_exe):
addr = get_addr()
hyper_cmd = [hyper_hello_exe, addr.split(":")[1]]
print "http_benchmark testing RUST hyper."
return run(hyper_cmd, addr)
2018-10-15 16:44:35 -04:00
def http_benchmark(build_dir):
hyper_hello_exe = os.path.join(build_dir, "hyper_hello")
core_http_bench_exe = os.path.join(build_dir,
"examples/deno_core_http_bench")
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),
"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),
"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
def run(server_cmd, addr, merge_env=None, origin_cmd=None):
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
# 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)
2018-12-07 16:36:16 -05:00
server = subprocess.Popen(server_cmd, env=env)
time.sleep(10) # wait for server to wake up. TODO racy.
2018-10-15 16:44:35 -04:00
try:
cmd = "third_party/wrk/%s/wrk -d %s --latency http://%s/" % (
util.platform(), DURATION, addr)
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:
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])