-
Notifications
You must be signed in to change notification settings - Fork 3
feat: performance measurement APIs #20
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
Open
BobdenOs
wants to merge
3
commits into
cap-js:main
Choose a base branch
from
BobdenOs:feat/performance
base: main
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.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| const cds = require('@sap/cds') | ||
| const { LIGHT_GRAY: GREEN, DIMMED, RESET } = require('@sap/cds/lib/utils/colors') | ||
|
|
||
| const DEFAULTS = { | ||
| warmup: { | ||
| duration: '1s', | ||
| }, | ||
| duration: '3s', | ||
| connections: 3, | ||
| } | ||
|
|
||
| class Performance { | ||
| constructor(test) { | ||
| this._test = test | ||
| this._reports = 0 | ||
| } | ||
|
|
||
| get autocannon() { | ||
| const autocannon = require('autocannon') | ||
| super._histUtil = require('autocannon/lib/histUtil') | ||
| super._aggregateResult = require('autocannon/lib/aggregateResult') | ||
| super._timestring = require('timestring') | ||
| return super.autocannon = autocannon | ||
| } | ||
| fn(..._) { return this._run(this._args('FN', _)) } | ||
| get(..._) { return this.autocannon(this._args('GET', _)) } | ||
| put(..._) { return this.autocannon(...this._args('PUT', _)) } | ||
| post(..._) { return this.autocannon(...this._args('POST', _)) } | ||
| patch(..._) { return this.autocannon(...this._args('PATCH', _)) } | ||
| delete(..._) { return this.autocannon(...this._args('DELETE', _)) } | ||
| options(..._) { return this.autocannon(...this._args('OPTIONS', _)) } | ||
|
|
||
| /** @type typeof _.options */ get FN() { return this.fn.bind(this) } | ||
| /** @type typeof _.get */ get GET() { return this.get.bind(this) } | ||
| /** @type typeof _.put */ get PUT() { return this.put.bind(this) } | ||
| /** @type typeof _.post */ get POST() { return this.post.bind(this) } | ||
| /** @type typeof _.patch */ get PATCH() { return this.patch.bind(this) } | ||
| /** @type typeof _.delete */ get DELETE() { return this.delete.bind(this) } | ||
| /** @type typeof _.delete */ get DEL() { return this.delete.bind(this) } //> to avoid conflicts with cds.ql.DELETE | ||
| /** @type typeof _.options */ get OPTIONS() { return this.options.bind(this) } | ||
|
|
||
| _args(METHOD, args) { | ||
| const first = args[0], last = args[args.length - 1] | ||
| if (first.raw) { | ||
| if (first[first.length - 1] === '' && typeof last === 'object') | ||
| return this._defaults(METHOD, last, { url: String.raw(...args.slice(0, -1)) }) | ||
| return this._defaults(METHOD, { url: String.raw(...args) }) | ||
| } | ||
| else if (typeof first === 'string') args[0] = { url: first } | ||
| else if (typeof first === 'function') args[0] = { fn: first, title: first.name } | ||
| else if (typeof first !== 'string') | ||
| throw new Error(`Argument path is expected to be a string or function but got ${typeof first}`) | ||
| return this._defaults(METHOD, ...args) | ||
| } | ||
|
|
||
| _defaults(method = 'GET', ...opts) { | ||
| let fn | ||
| if (typeof method === 'function') fn = method | ||
|
|
||
| const o = Object.assign({ fn, method }, DEFAULTS, ...opts) | ||
| if (o.url) { | ||
| o.title ??= o.url | ||
| const { auth } = this._test.axios.defaults | ||
| o.headers ??= {} | ||
| if (auth) { | ||
| o.headers.authorization = `Basic ${btoa(`${auth.username}:${auth.password}`)}` | ||
| } | ||
| const { baseURL } = this._test.axios.defaults || '' | ||
| const sep = baseURL.at(-1) !== '/' && o.url?.[0] !== '/' ? '/' : '' | ||
| o.url = /^https?:/.test(o.url) ? o.url : `${baseURL}${sep}${o.url}` | ||
| } | ||
| return o | ||
| } | ||
|
|
||
| async _run(opts) { | ||
| this.autocannon | ||
|
|
||
| let { fn, args } = opts | ||
| if (args) fn = fn.bind(null, ...args) | ||
|
|
||
| if (opts.warmup) await this._run({ ...opts, fn, args: undefined, ...opts.warmup, warmup: undefined }) | ||
|
|
||
| const { getHistograms, encodeHist } = this._histUtil | ||
|
|
||
| const histograms = getHistograms(opts.histograms) | ||
| const { latencies, requests, throughput } = histograms | ||
|
|
||
| const statusCodeStats = {} | ||
|
|
||
| let stop = false | ||
| let count = 0 | ||
| let errors = 0 | ||
| let nextTrack | ||
| let totalRequests = 0 | ||
| let totalCompletedRequests = 0 | ||
|
|
||
| const runners = new Array(opts.connections) | ||
|
|
||
| const startTime = process.hrtime.bigint() | ||
| const endTime = startTime + BigInt((typeof opts.duration === 'string' ? this._timestring(opts.duration) : opts.duration) * 1e9) | ||
|
|
||
| for (let r = 0; r < runners.length; r++) { | ||
| runners[r] = run() | ||
| } | ||
| await Promise.all(runners) | ||
|
|
||
| const result = { | ||
| latencies: encodeHist(latencies), | ||
| requests: encodeHist(requests), | ||
| throughput: encodeHist(throughput), | ||
| totalCompletedRequests, | ||
| totalRequests, | ||
| totalBytes: 0, | ||
| samples: Math.floor(Number(process.hrtime.bigint() - startTime) / 1e9), | ||
| errors, | ||
| timeouts: 0, | ||
| mismatches: 0, | ||
| non2xx: Object.keys(statusCodeStats).reduce((l, c) => l + (c[0] === '2' ? 0 : statusCodeStats[c]), 0), | ||
| statusCodeStats, | ||
| resets: 0, | ||
| duration: Number(process.hrtime.bigint() - startTime) / 1e9, | ||
| start: new Date(Number(startTime)), | ||
| finish: new Date(), | ||
| '1xx': 0, | ||
| '2xx': statusCodeStats['200']?.count || 0, | ||
| '3xx': 0, | ||
| '4xx': 0, | ||
| '5xx': statusCodeStats['500']?.count || 0, | ||
| } | ||
|
|
||
| return this._aggregateResult(result, opts, histograms) | ||
|
|
||
| async function run() { | ||
| while (!stop) { | ||
| const now = process.hrtime.bigint() | ||
| if (!nextTrack) nextTrack = now + BigInt(1e9) | ||
| if (now >= nextTrack) { | ||
| nextTrack = now + BigInt(1e9) | ||
| requests.recordValue(count) | ||
| count = 0 | ||
| } | ||
|
|
||
| if (now >= endTime) { | ||
| stop = true | ||
| break | ||
| } | ||
|
|
||
| totalRequests++ | ||
| try { | ||
| const s = process.hrtime.bigint() | ||
|
|
||
| const ret = fn() | ||
| if (ret?.then) await ret; | ||
|
|
||
| const d = process.hrtime.bigint() - s | ||
| latencies.recordValue(Number(d) / 1000000) | ||
|
|
||
| count++ | ||
| totalCompletedRequests++ | ||
| (statusCodeStats['200'] ??= { count: 0 }).count++ | ||
| } catch { | ||
| errors++ | ||
| (statusCodeStats['500'] ??= { count: 0 }).count++ | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async _report(result, options = {}) { | ||
| let { requests, latency, throughput, title = `${this._reports++}` } = result | ||
|
|
||
| // Collect the result into a file for further processing later | ||
| if (options.store) { | ||
| result.file = cds.utils.path.relative(process.cwd(), require.main.filename) | ||
patricebender marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const stack = {} | ||
| Error.captureStackTrace(stack) | ||
| result.line = /:(\d*:\d*)\)/.exec(stack.stack.split('\n').find(l => l.indexOf(result.file) > -1))?.[1] | ||
|
|
||
| const benchmark = `${result.file}:${title}` | ||
| cds.utils.fs.writeFileSync(cds.utils.path.resolve(process.cwd(), 'results.bench'), `${JSON.stringify({ [benchmark]: result })}\n`, { flag: 'a' }) | ||
patricebender marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // TODO: determine a good default report format of the available measured information | ||
| console.log( // eslint-disable-line no-console | ||
| title.padEnd(50), | ||
| GREEN + (requests.average >>> 0).toLocaleString().padStart(5), DIMMED + 'req/s' + RESET, | ||
| GREEN + (throughput.average / 1024 / 1024 >>> 0).toLocaleString().padStart(5), DIMMED + 'MiB/s' + RESET, | ||
| GREEN + (latency.average >>> 0).toLocaleString().padStart(5), DIMMED + 'ms' + RESET, | ||
| ) | ||
| } | ||
| /** @type typeof _._report */ get report() { return this._report.bind(this) } | ||
|
|
||
| } | ||
|
|
||
| // ? const _ = Performance.prototype // eslint-disable-line no-unused-vars | ||
| module.exports = Performance | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just out of curiosity, whats the advantage of this over:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is more precise then
performance.nowand I got this from thenoderepository benchmark code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know
hrtimeis more precise, I was just wondering why we need thebigint()API here. Also the diff calculation can be done by the function itself (but it yields a[seconds, nanoseconds]tuple).Smt like this: