Skip to content

Commit

Permalink
feat: add sending data benchmark (#2905)
Browse files Browse the repository at this point in the history
* feat: add post benchmark

* fixup

* apply suggestions from code review

* apply suggestions from code review
  • Loading branch information
tsctx committed Mar 3, 2024
1 parent 2505e42 commit 071609f
Show file tree
Hide file tree
Showing 4 changed files with 478 additions and 11 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/bench.yml
Expand Up @@ -49,3 +49,46 @@ jobs:
- name: Run Benchmark
run: npm run bench
working-directory: ./benchmarks

benchmark_post_current:
name: benchmark (sending data) current
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
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-post
working-directory: ./benchmarks

benchmark_post_branch:
name: benchmark (sending data) branch
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
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-post
working-directory: ./benchmarks
19 changes: 9 additions & 10 deletions benchmarks/benchmark.js
Expand Up @@ -34,22 +34,16 @@ if (process.env.PORT) {
dest.socketPath = path.join(os.tmpdir(), 'undici.sock')
}

/** @type {http.RequestOptions} */
const httpBaseOptions = {
protocol: 'http:',
hostname: 'localhost',
method: 'GET',
path: '/',
query: {
frappucino: 'muffin',
goat: 'scone',
pond: 'moose',
foo: ['bar', 'baz', 'bal'],
bool: true,
numberKey: 256
},
...dest
}

/** @type {http.RequestOptions} */
const httpNoKeepAliveOptions = {
...httpBaseOptions,
agent: new http.Agent({
Expand All @@ -58,6 +52,7 @@ const httpNoKeepAliveOptions = {
})
}

/** @type {http.RequestOptions} */
const httpKeepAliveOptions = {
...httpBaseOptions,
agent: new http.Agent({
Expand Down Expand Up @@ -142,7 +137,11 @@ class SimpleRequest {
}

function makeParallelRequests (cb) {
return Promise.all(Array.from(Array(parallelRequests)).map(() => new Promise(cb)))
const promises = new Array(parallelRequests)
for (let i = 0; i < parallelRequests; ++i) {
promises[i] = new Promise(cb)
}
return Promise.all(promises)
}

function printResults (results) {
Expand Down Expand Up @@ -303,7 +302,7 @@ if (process.env.PORT) {

experiments.got = () => {
return makeParallelRequests(resolve => {
got.get(dest.url, null, { http: gotAgent }).then(res => {
got.get(dest.url, { agent: { http: gotAgent } }).then(res => {
res.pipe(new Writable({
write (chunk, encoding, callback) {
callback()
Expand Down
5 changes: 4 additions & 1 deletion benchmarks/package.json
Expand Up @@ -2,9 +2,12 @@
"name": "benchmarks",
"scripts": {
"bench": "PORT=3042 concurrently -k -s first npm:bench:server npm:bench:run",
"bench-post": "PORT=3042 concurrently -k -s first npm:bench:server npm:bench-post:run",
"bench:server": "node ./server.js",
"prebench:run": "node ./wait.js",
"bench:run": "SAMPLES=100 CONNECTIONS=50 node ./benchmark.js"
"bench:run": "SAMPLES=100 CONNECTIONS=50 node ./benchmark.js",
"prebench-post:run": "node ./wait.js",
"bench-post:run": "SAMPLES=100 CONNECTIONS=50 node ./post-benchmark.js"
},
"dependencies": {
"axios": "^1.6.7",
Expand Down

0 comments on commit 071609f

Please sign in to comment.