|
1 | | -// NOTE: run `npm test` to build `./test/tmp/http-loose-request` |
| 1 | +import * as assert from "assert"; |
| 2 | +import { spawnSync } from "child_process"; |
| 3 | +import { existsSync } from "fs"; |
| 4 | +import { resolve } from "path"; |
2 | 5 |
|
3 | | -import * as assert from 'assert'; |
4 | | -import { spawnSync } from 'child_process'; |
5 | | -import { existsSync } from 'fs'; |
6 | | - |
7 | | -const isURL = !process.argv[2] || process.argv[2] === 'url'; |
8 | | -const isHTTP = !process.argv[2] || process.argv[2] === 'http'; |
| 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 | 9 |
|
10 | | -const requests: Map<string, string> = new Map(); |
| 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"; |
11 | 44 |
|
12 | | -if (!existsSync('./test/tmp/http-loose-request.c')) { |
13 | | - console.error('Run npm test to build ./test/tmp/http-loose-request'); |
| 45 | +if (!existsSync(urlExecutable) || !existsSync(urlExecutable)) { |
| 46 | + console.error( |
| 47 | + "\x1b[31m\x1b[1mPlease run npm test in order to create required executables." |
| 48 | + ); |
14 | 49 | process.exit(1); |
15 | 50 | } |
16 | 51 |
|
17 | | -requests.set('seanmonstar/httparse', |
18 | | - 'GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n' + |
19 | | - 'Host: www.kittyhell.com\r\n' + |
20 | | - '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' + |
21 | | - 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n' + |
22 | | - 'Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n' + |
23 | | - 'Accept-Encoding: gzip,deflate\r\n' + |
24 | | - 'Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n' + |
25 | | - 'Keep-Alive: 115\r\n' + |
26 | | - 'Connection: keep-alive\r\n' + |
27 | | - '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'); |
28 | | - |
29 | | -requests.set('nodejs/http-parser', |
30 | | - 'POST /joyent/http-parser HTTP/1.1\r\n' + |
31 | | - 'Host: github.com\r\n' + |
32 | | - 'DNT: 1\r\n' + |
33 | | - 'Accept-Encoding: gzip, deflate, sdch\r\n' + |
34 | | - 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n' + |
35 | | - 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' + |
36 | | - 'AppleWebKit/537.36 (KHTML, like Gecko) ' + |
37 | | - 'Chrome/39.0.2171.65 Safari/537.36\r\n' + |
38 | | - 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,' + |
39 | | - 'image/webp,*/*;q=0.8\r\n' + |
40 | | - 'Referer: https://github.com/joyent/http-parser\r\n' + |
41 | | - 'Connection: keep-alive\r\n' + |
42 | | - 'Transfer-Encoding: chunked\r\n' + |
43 | | - 'Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n'); |
44 | | - |
45 | | -if (process.argv[2] === 'loop') { |
| 52 | +if (process.argv[2] === "loop") { |
46 | 53 | const reqName = process.argv[3]; |
47 | | - assert(requests.has(reqName), `Unknown request name: "${reqName}"`); |
48 | | - |
49 | | - const request = requests.get(reqName)!; |
50 | | - spawnSync('./test/tmp/http-loose-request', [ |
51 | | - 'loop', |
52 | | - request |
53 | | - ], { stdio: 'inherit' }); |
| 54 | + const request = httpRequests[reqName]!; |
| 55 | + |
| 56 | + assert(request, `Unknown request name: "${reqName}"`); |
| 57 | + spawnSync(httpExecutable, ["loop", request], { stdio: "inherit" }); |
54 | 58 | process.exit(0); |
55 | 59 | } |
56 | 60 |
|
57 | | -if (isURL) { |
58 | | - console.log('url loose (C)'); |
59 | | - |
60 | | - spawnSync('./test/tmp/url-loose-url-c', [ |
61 | | - 'bench', |
62 | | - 'http://example.com/path/to/file?query=value#fragment' |
63 | | - ], { stdio: 'inherit' }); |
64 | | - |
65 | | - console.log('url strict (C)'); |
66 | | - |
67 | | - spawnSync('./test/tmp/url-strict-url-c', [ |
68 | | - 'bench', |
69 | | - 'http://example.com/path/to/file?query=value#fragment' |
70 | | - ], { stdio: 'inherit' }); |
| 61 | +if (!process.argv[2] || process.argv[2] === "url") { |
| 62 | + console.log("url (C)"); |
| 63 | + spawnSync(urlExecutable, ["bench", urlRequest], { stdio: "inherit" }); |
71 | 64 | } |
72 | 65 |
|
73 | | -if (isHTTP) { |
74 | | - for(const [name, request] of requests) { |
75 | | - console.log('http loose: "%s" (C)', name); |
76 | | - |
77 | | - spawnSync('./test/tmp/http-loose-request-c', [ |
78 | | - 'bench', |
79 | | - request |
80 | | - ], { stdio: 'inherit' }); |
81 | | - |
82 | | - console.log('http strict: "%s" (C)', name); |
83 | | - |
84 | | - spawnSync('./test/tmp/http-strict-request-c', [ |
85 | | - 'bench', |
86 | | - request |
87 | | - ], { stdio: 'inherit' }); |
| 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" }); |
88 | 70 | } |
89 | 71 | } |
0 commit comments