Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench: enable benchmarks for h2 #3100

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 30 additions & 0 deletions benchmarks/benchmark-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -242,6 +271,7 @@ async function main () {

printResults(results)
dispatcher.destroy()
http2NativeClient.close()
}
)
}
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
16 changes: 11 additions & 5 deletions benchmarks/server-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down