|
3 | 3 | const common = require('../common.js'); |
4 | 4 | const protocols = process.versions.openssl ? ['http', 'https'] : ['http']; |
5 | 5 |
|
6 | | -const configs = { |
7 | | - sameTurn: [{ |
8 | | - type: ['bytes', 'buffer', 'uint8array'], |
9 | | - len: [64, 1024], |
10 | | - chunks: [1, 2, 4, 16], |
11 | | - mode: ['auto', 'explicit'], |
12 | | - transfer: ['chunked', 'length'], |
13 | | - protocol: protocols, |
14 | | - producer: ['sync'], |
15 | | - callback: [0], |
16 | | - c: [50], |
17 | | - duration: 5, |
18 | | - }], |
19 | | - streaming: [{ |
20 | | - type: ['bytes', 'buffer', 'uint8array'], |
21 | | - len: [64, 1024], |
22 | | - chunks: [4], |
23 | | - mode: ['auto'], |
24 | | - transfer: ['chunked'], |
25 | | - protocol: protocols, |
26 | | - producer: ['nextTick', 'microtask', 'immediate'], |
27 | | - callback: [0], |
28 | | - c: [50], |
29 | | - duration: 5, |
30 | | - }], |
31 | | - callbacks: [{ |
32 | | - type: ['bytes', 'buffer', 'uint8array'], |
33 | | - len: [64], |
34 | | - chunks: [4, 16], |
35 | | - mode: ['auto', 'explicit'], |
36 | | - transfer: ['chunked'], |
37 | | - protocol: protocols, |
38 | | - producer: ['sync'], |
39 | | - callback: [1], |
40 | | - c: [50], |
41 | | - duration: 5, |
42 | | - }], |
43 | | - fixedBody: [{ |
44 | | - type: ['bytes', 'buffer', 'uint8array'], |
45 | | - total: [64 * 1024], |
46 | | - chunks: [1, 4, 16, 128], |
47 | | - mode: ['auto', 'explicit'], |
48 | | - transfer: ['chunked'], |
49 | | - protocol: protocols, |
50 | | - producer: ['sync'], |
51 | | - callback: [0], |
52 | | - c: [50], |
53 | | - duration: 5, |
54 | | - }], |
55 | | - largeChunks: [{ |
56 | | - type: ['bytes', 'buffer', 'uint8array'], |
57 | | - len: [4 * 1024, 8 * 1024, 16 * 1024, 64 * 1024], |
58 | | - chunks: [1, 4], |
59 | | - mode: ['auto'], |
60 | | - transfer: ['chunked'], |
61 | | - protocol: protocols, |
62 | | - producer: ['sync'], |
63 | | - callback: [0], |
64 | | - c: [50], |
65 | | - duration: 5, |
66 | | - }], |
67 | | - concurrency: [{ |
68 | | - type: ['bytes'], |
69 | | - len: [64], |
70 | | - chunks: [4], |
71 | | - mode: ['auto'], |
72 | | - transfer: ['chunked'], |
73 | | - protocol: protocols, |
74 | | - producer: ['sync'], |
75 | | - callback: [0], |
76 | | - c: [1, 50, 500], |
77 | | - duration: 5, |
78 | | - }], |
| 6 | +const scenarios = { |
| 7 | + 'end-64': { |
| 8 | + len: 64, |
| 9 | + chunks: 1, |
| 10 | + endChunk: true, |
| 11 | + }, |
| 12 | + 'end-1024': { |
| 13 | + len: 1024, |
| 14 | + chunks: 1, |
| 15 | + endChunk: true, |
| 16 | + }, |
| 17 | + 'end-1025': { |
| 18 | + len: 1025, |
| 19 | + chunks: 1, |
| 20 | + endChunk: true, |
| 21 | + }, |
| 22 | + 'auto-4': { |
| 23 | + len: 64, |
| 24 | + chunks: 4, |
| 25 | + }, |
| 26 | + 'auto-16': { |
| 27 | + len: 64, |
| 28 | + chunks: 16, |
| 29 | + }, |
| 30 | + 'explicit-16': { |
| 31 | + len: 64, |
| 32 | + chunks: 16, |
| 33 | + explicit: true, |
| 34 | + }, |
| 35 | + 'content-length-16': { |
| 36 | + len: 64, |
| 37 | + chunks: 16, |
| 38 | + contentLength: true, |
| 39 | + }, |
| 40 | + 'next-tick-4': { |
| 41 | + len: 64, |
| 42 | + chunks: 4, |
| 43 | + schedule: process.nextTick, |
| 44 | + }, |
| 45 | + 'callbacks-16': { |
| 46 | + len: 64, |
| 47 | + chunks: 16, |
| 48 | + callbacks: true, |
| 49 | + }, |
| 50 | + 'fixed-body-128': { |
| 51 | + len: 512, |
| 52 | + chunks: 128, |
| 53 | + }, |
| 54 | + 'large-16k': { |
| 55 | + len: 16 * 1024, |
| 56 | + chunks: 4, |
| 57 | + }, |
79 | 58 | }; |
80 | 59 |
|
81 | | -const bench = common.createBenchmark(main, configs, { byGroups: true }); |
| 60 | +const bench = common.createBenchmark(main, { |
| 61 | + type: ['string', 'buffer', 'uint8array'], |
| 62 | + scenario: Object.keys(scenarios), |
| 63 | + protocol: protocols, |
| 64 | + c: [50], |
| 65 | + duration: [5], |
| 66 | +}); |
82 | 67 |
|
83 | | -function main({ |
84 | | - type, |
85 | | - len, |
86 | | - chunks, |
87 | | - mode, |
88 | | - transfer, |
89 | | - protocol, |
90 | | - producer, |
91 | | - callback, |
92 | | - c, |
93 | | - duration, |
94 | | - total, |
95 | | -}) { |
| 68 | +function main({ type, scenario, protocol, c, duration }) { |
| 69 | + const { |
| 70 | + callbacks, |
| 71 | + chunks, |
| 72 | + contentLength, |
| 73 | + endChunk, |
| 74 | + explicit, |
| 75 | + len, |
| 76 | + schedule, |
| 77 | + } = scenarios[scenario]; |
96 | 78 | const transport = require(protocol); |
97 | | - len ??= total / chunks; |
98 | | - const chunk = type === 'bytes' ? 'a'.repeat(len) : |
| 79 | + const chunk = type === 'string' ? 'a'.repeat(len) : |
99 | 80 | type === 'buffer' ? Buffer.alloc(len, 'a') : |
100 | 81 | new Uint8Array(len).fill(0x61); |
101 | | - const writeCallback = callback ? (err) => { |
| 82 | + const writeCallback = callbacks ? (err) => { |
102 | 83 | if (err) throw err; |
103 | 84 | } : undefined; |
104 | 85 |
|
105 | | - const schedule = producer === 'nextTick' ? process.nextTick : |
106 | | - producer === 'microtask' ? queueMicrotask : setImmediate; |
107 | | - |
108 | 86 | const onRequest = (req, res) => { |
109 | | - if (transfer === 'length') { |
| 87 | + if (contentLength) { |
110 | 88 | res.setHeader('Content-Length', len * chunks); |
111 | 89 | } |
112 | | - if (mode === 'explicit') { |
| 90 | + if (explicit) { |
113 | 91 | res.cork(); |
114 | 92 | } |
| 93 | + if (endChunk) { |
| 94 | + res.end(chunk, writeCallback); |
| 95 | + return; |
| 96 | + } |
115 | 97 |
|
116 | | - if (producer === 'sync') { |
| 98 | + if (schedule === undefined) { |
117 | 99 | for (let i = 0; i < chunks; i++) { |
118 | 100 | res.write(chunk, writeCallback); |
119 | 101 | } |
|
0 commit comments