summaryrefslogtreecommitdiff
path: root/tools/http_benchmark.py
blob: c7c7a83b12d7fd7aad52e73105e3e71a1a2f4526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os
import sys
import util
import time
import subprocess

# 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"

ADDR = "127.0.0.1:4544"
DURATION = "10s"


def deno_tcp(deno_exe):
    deno_cmd = [deno_exe, "run", "--allow-net", "tools/deno_tcp.ts", ADDR]
    print "http_benchmark testing DENO."
    return run(deno_cmd)


def deno_http(deno_exe):
    deno_cmd = [
        deno_exe, "run", "--allow-net",
        "js/deps/https/deno.land/std/http/http_bench.ts", ADDR
    ]
    print "http_benchmark testing DENO using net/http."
    return run(
        deno_cmd,
        merge_env={
            # Load from //js/deps/https/deno.land/net/ submodule.
            "DENO_DIR": os.path.join(util.root_path, "js")
        })


def deno_core_single(exe):
    print "http_benchmark testing deno_core_single"
    return run([exe, "--single-thread"])


def deno_core_multi(exe):
    print "http_benchmark testing deno_core_multi"
    return run([exe, "--multi-thread"])


def node_http():
    node_cmd = ["node", "tools/node_http.js", ADDR.split(":")[1]]
    print "http_benchmark testing NODE."
    return run(node_cmd)


def node_tcp():
    node_cmd = ["node", "tools/node_tcp.js", ADDR.split(":")[1]]
    print "http_benchmark testing node_tcp.js"
    return run(node_cmd)


def hyper_http(hyper_hello_exe):
    hyper_cmd = [hyper_hello_exe, ADDR.split(":")[1]]
    print "http_benchmark testing RUST hyper."
    return run(hyper_cmd)


def http_benchmark(build_dir):
    hyper_hello_exe = os.path.join(build_dir, "hyper_hello")
    core_http_bench_exe = os.path.join(build_dir, "deno_core_http_bench")
    deno_exe = os.path.join(build_dir, "deno")
    return {
        # "deno_tcp" was once called "deno"
        "deno_tcp": deno_tcp(deno_exe),
        # "deno_http" was once called "deno_net_http"
        "deno_http": deno_http(deno_exe),
        "deno_core_single": deno_core_single(core_http_bench_exe),
        "deno_core_multi": deno_core_multi(core_http_bench_exe),
        # "node_http" was once called "node"
        "node_http": node_http(),
        "node_tcp": node_tcp(),
        "hyper": hyper_http(hyper_hello_exe)
    }


def run(server_cmd, merge_env=None):
    # Run deno echo server in the background.
    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)

    server = subprocess.Popen(server_cmd, env=env)

    time.sleep(5)  # wait for server to wake up. TODO racy.

    try:
        cmd = "third_party/wrk/%s/wrk -d %s http://%s/" % (util.platform(),
                                                           DURATION, ADDR)
        print cmd
        output = subprocess.check_output(cmd, shell=True)
        stats = util.parse_wrk_output(output)
        print output
        return stats
    finally:
        server.kill()


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage ./tools/http_benchmark.py target/debug/deno"
        sys.exit(1)
    deno_http(sys.argv[1])