-
Notifications
You must be signed in to change notification settings - Fork 3
Feat timing utils #43
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
Draft
dnlup
wants to merge
1
commit into
next
Choose a base branch
from
feat_timing_utils
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 'use strict'; | ||
|
|
||
| const { performance, PerformanceObserver } = require('perf_hooks'); | ||
|
|
||
| /** | ||
| * A map that associates metrics labels to timerified | ||
| * functions and send hooks. | ||
| */ | ||
| const map = new Map(); | ||
| let obs = null; | ||
|
|
||
| const registry = new FinalizationRegistry((label) => { | ||
| map.delete(label); | ||
| if (map.size === 0 && obs) { | ||
| obs.disconnect(); | ||
| obs = null; | ||
| } | ||
| }); | ||
|
|
||
| function wrap(fn, opts) { | ||
| // TODO: throw an error if a function woth the same name is already | ||
| // present? | ||
| const wrapped = performance.timerify(fn, opts.timerifyOptions); | ||
| if (!obs) { | ||
| obs = new PerformanceObserver((list) => { | ||
| for (const entry of list.getEntries()) { | ||
| const i = map.get(entry.name); | ||
| const name = i[0]; | ||
| const onSend = i[1]; | ||
| /* istanbul ignore else */ | ||
| if (name) { | ||
| onSend(name, entry); | ||
| } | ||
| } | ||
| }); | ||
| obs.observe({ entryTypes: ['function'] }); | ||
| } | ||
| map.set(fn.name, [opts.label, opts.onSend, wrapped]); | ||
| registry.register(wrapped, opts.label); | ||
| return wrapped; | ||
| } | ||
|
|
||
| function sendPerfEntry(name, entry) { | ||
| this.stats.timing(name, entry.duration); | ||
| } | ||
|
|
||
| // This is here just to simplify testing. | ||
| exports.clear = function () { | ||
| for (const [k, v] of map.entries()) { | ||
| registry.unregister(v[2]); | ||
| map.delete(k); | ||
| } | ||
| obs && obs.disconnect(); | ||
| obs = null; | ||
| }; | ||
|
|
||
| // Bind a timerify wrap factory to a specific fastify instance. | ||
| exports.timerifyWrap = function (fastify) { | ||
| const _onSend = sendPerfEntry.bind(fastify); | ||
| return function timerify(fn, opts) { | ||
| if (typeof fn !== 'function') { | ||
| throw new Error('You have to pass a function to timerify'); | ||
| } | ||
| const defaults = { | ||
| label: fn.name, | ||
| onSend: _onSend, | ||
| timerifyOptions: undefined, | ||
| }; | ||
| const options = Object.assign({}, defaults, opts); | ||
| if (typeof options.label !== 'string') { | ||
| throw new Error( | ||
| 'You have to pass a string to label the timerified function metric' | ||
| ); | ||
| } | ||
| if (typeof options.onSend !== 'function') { | ||
| throw new Error( | ||
| 'You have to pass a function to the custom onSend hook' | ||
| ); | ||
| } | ||
| if (options.onSend !== _onSend) { | ||
| // `this` refers to the fastify instance decorated with the timerify util. | ||
| options.onSend = options.onSend.bind(this); | ||
| } | ||
| return wrap(fn, options); | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 'use strict'; | ||
|
|
||
| exports.gte16 = Number(process.version.split('.')[0].replace('v', '')) >= 16; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| 'use strict'; | ||
|
|
||
| const defaultExclude = require('@istanbuljs/schema/default-exclude'); | ||
| const { gte16 } = require('./lib/utils'); | ||
|
|
||
| const onLt16 = ['lib/timerifyWrap.js', 'lib/sendPerfEntry.js']; | ||
|
|
||
| module.exports = { | ||
| exclude: ['*tests*'], | ||
| exclude: defaultExclude.concat(gte16 ? [] : onLt16), | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.