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

perf_hooks: improve http2 import #29419

Closed
wants to merge 1 commit into from

Conversation

fanatid
Copy link
Contributor

@fanatid fanatid commented Sep 3, 2019

Checklist
  • make -j4 test (UNIX) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Nearly ~119ns for each collectHttp2Stats call.

const http2 = internalBinding('http2'); => 20001: 36.200362ms
if (http2 === undefined) http2 = internalBinding('http2'); => 20001: 33.814263ms
(36.200362 - 33.814263) * 1e6 / 20001 = 119

Same lazy require in net module: https://github.com/nodejs/node/blob/v12.9.1/lib/net.js#L940

Benchmarked with:

const http2 = require('http2')
const perf_hooks = require('perf_hooks')

let firstImport = true
let sum = 0
let cnt = 0
process.once('beforeExit', () => console.log(`${cnt}: ${sum.toFixed(6)}ms`))

const obs = new perf_hooks.PerformanceObserver((items) => {
  const entry = items.getEntries()[0]
  if (entry.entryType !== 'measure') return

  if (firstImport) {
    firstImport = false
    return
  }

  sum += entry.duration
  cnt += 1
})
obs.observe({ entryTypes: ['measure', 'http2'] })

const server = http2.createServer({}, (req, res) => res.end('ok')).listen(8000)
server.once('listening', async () => {
  const client = http2.connect('http://localhost:8000')
  await Promise.all(new Array(1e4).fill(null).map(() => {
    return new Promise((resolve) => client.request().on('data', () => {}).once('close', resolve).end())
  }))

  client.close()
  server.close()
})

Code in perf_hooks.js was changed to:

  performance.mark('collectHttp2StatsImportStart');
  // const http2 = internalBinding('http2');
  if (http2 === undefined) http2 = internalBinding('http2');
  performance.mark('collectHttp2StatsImportEnd');
  performance.measure('collectHttp2Stats', 'collectHttp2StatsImportStart', 'collectHttp2StatsImportEnd');

Import http2 internal bindings on each collectHttp2Stats call is not
required.
@nodejs-github-bot nodejs-github-bot added the perf_hooks Issues and PRs related to the implementation of the Performance Timing API. label Sep 3, 2019
@trivikr
Copy link
Member

trivikr commented Sep 3, 2019

@trivikr trivikr added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Sep 3, 2019
@nodejs-github-bot
Copy link
Collaborator

@Trott
Copy link
Member

Trott commented Sep 6, 2019

Landed in 6186302

@Trott Trott closed this Sep 6, 2019
Trott pushed a commit that referenced this pull request Sep 6, 2019
Import http2 internal bindings on each collectHttp2Stats call is not
required.

PR-URL: #29419
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
@fanatid fanatid deleted the perfhooks-improve-http2 branch September 6, 2019 04:05
targos pushed a commit that referenced this pull request Sep 20, 2019
Import http2 internal bindings on each collectHttp2Stats call is not
required.

PR-URL: #29419
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
@BridgeAR BridgeAR mentioned this pull request Sep 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. perf_hooks Issues and PRs related to the implementation of the Performance Timing API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants