Debian Codegraph @

third_party/python/aiohttp/vendor/llhttp/bench/index.ts

raw
1 import * as assert from "assert";
2 import { spawnSync } from "child_process";
3 import { existsSync } from "fs";
4 import { resolve } from "path";
5
6 function request(tpl: TemplateStringsArray): string {
7 return tpl.raw[0].replace(/^\s+/gm, '').replace(/\n/gm, '').replace(/\\r/gm, '\r').replace(/\\n/gm, '\n')
8 }
9
10 const urlExecutable = resolve(__dirname, "../test/tmp/url-url-c");
11 const httpExecutable = resolve(__dirname, "../test/tmp/http-request-c");
12
13 const httpRequests: Record<string, string> = {
14 "seanmonstar/httparse": request`
15 GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n
16 Host: www.kittyhell.com\r\n
17 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n
18 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
19 Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n
20 Accept-Encoding: gzip,deflate\r\n
21 Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n
22 Keep-Alive: 115\r\n
23 Connection: keep-alive\r\n
24 Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral\r\n\r\n
25 `,
26 "nodejs/http-parser": request`
27 POST /joyent/http-parser HTTP/1.1\r\n
28 Host: github.com\r\n
29 DNT: 1\r\n
30 Accept-Encoding: gzip, deflate, sdch\r\n
31 Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n
32 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1)
33 AppleWebKit/537.36 (KHTML, like Gecko)
34 Chrome/39.0.2171.65 Safari/537.36\r\n
35 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,
36 image/webp,*/*;q=0.8\r\n
37 Referer: https://github.com/joyent/http-parser\r\n
38 Connection: keep-alive\r\n
39 Transfer-Encoding: chunked\r\n
40 Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n
41 `
42 }
43 const urlRequest = "http://example.com/path/to/file?query=value#fragment";
44
45 if (!existsSync(urlExecutable) || !existsSync(urlExecutable)) {
46 console.error(
47 "\x1b[31m\x1b[1mPlease run npm test in order to create required executables."
48 );
49 process.exit(1);
50 }
51
52 if (process.argv[2] === "loop") {
53 const reqName = process.argv[3];
54 const request = httpRequests[reqName]!;
55
56 assert(request, `Unknown request name: "${reqName}"`);
57 spawnSync(httpExecutable, ["loop", request], { stdio: "inherit" });
58 process.exit(0);
59 }
60
61 if (!process.argv[2] || process.argv[2] === "url") {
62 console.log("url (C)");
63 spawnSync(urlExecutable, ["bench", urlRequest], { stdio: "inherit" });
64 }
65
66 if (!process.argv[2] || process.argv[2] === "http") {
67 for (const [name, request] of Object.entries(httpRequests)) {
68 console.log('http: "%s" (C)', name);
69 spawnSync(httpExecutable, ["bench", request], { stdio: "inherit" });
70 }
71 }