From 6bb909fa835ad194ee47f73c9a2737d0f0d5cd2c Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Fri, 12 Apr 2024 14:41:33 +0200 Subject: [PATCH] bench: enable benchmarks for h2 (#3100) * refactor: h2 refactoring * test: add test for servername changed * chore: adjust h2 benchmark * chore: enable benchmarks for h2 upon PR --- .github/workflows/bench.yml | 43 +++++++++++++++++++++++++++++++++++ benchmarks/benchmark-http2.js | 30 ++++++++++++++++++++++++ benchmarks/package.json | 3 +++ benchmarks/server-http2.js | 16 +++++++++---- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml index a8bbc5e3d90..9e1e0186500 100644 --- a/.github/workflows/bench.yml +++ b/.github/workflows/bench.yml @@ -92,3 +92,46 @@ jobs: - name: Run Benchmark run: npm run bench-post working-directory: ./benchmarks + + benchmark_current_h2: + name: benchmark current + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + with: + persist-credentials: false + ref: ${{ github.base_ref }} + - name: Setup Node + uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 + with: + node-version: lts/* + - name: Install Modules for undici + run: npm i --ignore-scripts --omit=dev + - name: Install Modules for Benchmarks + run: npm i + working-directory: ./benchmarks + - name: Run Benchmark + run: npm run bench:h2 + working-directory: ./benchmarks + + benchmark_branch_h2: + name: benchmark branch + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2 + with: + persist-credentials: false + - name: Setup Node + uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 + with: + node-version: lts/* + - name: Install Modules for undici + run: npm i --ignore-scripts --omit=dev + - name: Install Modules for Benchmarks + run: npm i + working-directory: ./benchmarks + - name: Run Benchmark + run: npm run bench:h2 + working-directory: ./benchmarks diff --git a/benchmarks/benchmark-http2.js b/benchmarks/benchmark-http2.js index f89aacb993c..46ac89f97bf 100644 --- a/benchmarks/benchmark-http2.js +++ b/benchmarks/benchmark-http2.js @@ -2,6 +2,7 @@ const os = require('node:os') const path = require('node:path') +const http2 = require('node:http2') const { readFileSync } = require('node:fs') const { Writable } = require('node:stream') const { isMainThread } = require('node:worker_threads') @@ -53,6 +54,10 @@ const undiciOptions = { bodyTimeout } +const http2NativeClient = http2.connect(httpsBaseOptions.url, { + rejectUnauthorized: false +}) + const Class = connections > 1 ? Pool : Client const dispatcher = new Class(httpsBaseOptions.url, { allowH2: true, @@ -153,6 +158,30 @@ function printResults (results) { } const experiments = { + 'native - http2' () { + return makeParallelRequests(resolve => { + const stream = http2NativeClient.request({ + [http2.constants.HTTP2_HEADER_PATH]: httpsBaseOptions.path, + [http2.constants.HTTP2_HEADER_METHOD]: httpsBaseOptions.method + }) + + stream.end().on('response', () => { + stream.pipe( + new Writable({ + write (chunk, encoding, callback) { + callback() + } + }) + ) + .on('error', (err) => { + console.log('http2 - request - response - error', err) + }) + .on('finish', () => { + resolve() + }) + }) + }) + }, 'undici - pipeline' () { return makeParallelRequests(resolve => { dispatcher @@ -242,6 +271,7 @@ async function main () { printResults(results) dispatcher.destroy() + http2NativeClient.close() } ) } diff --git a/benchmarks/package.json b/benchmarks/package.json index a834377135f..d930fb9c4a3 100644 --- a/benchmarks/package.json +++ b/benchmarks/package.json @@ -2,10 +2,13 @@ "name": "benchmarks", "scripts": { "bench": "PORT=3042 concurrently -k -s first npm:bench:server npm:bench:run", + "bench:h2": "PORT=3052 concurrently -k -s first npm:bench:server:h2 npm:bench:run:h2", "bench-post": "PORT=3042 concurrently -k -s first npm:bench:server npm:bench-post:run", "bench:server": "node ./server.js", + "bench:server:h2": "node ./server-http2.js", "prebench:run": "node ./wait.js", "bench:run": "SAMPLES=100 CONNECTIONS=50 node ./benchmark.js", + "bench:run:h2": "SAMPLES=100 CONNECTIONS=50 node ./benchmark-http2.js", "prebench-post:run": "node ./wait.js", "bench-post:run": "SAMPLES=100 CONNECTIONS=50 node ./post-benchmark.js" }, diff --git a/benchmarks/server-http2.js b/benchmarks/server-http2.js index f8183b2fa1a..6e0424d4ea4 100644 --- a/benchmarks/server-http2.js +++ b/benchmarks/server-http2.js @@ -35,14 +35,20 @@ if (cluster.isPrimary) { cert, allowHTTP1: true, sessionTimeout - }, - (req, res) => { - setTimeout(() => { - res.end(buf) - }, timeout) } ) + server.on('stream', (stream) => { + setTimeout(() => { + stream.respond({ + 'content-type': 'text/plain; charset=utf-8', + ':status': 200 + }) + + stream.setEncoding('utf-8').end(buf) + }, timeout) + }) + server.keepAliveTimeout = 600e3 server.listen(port)