diff --git a/bin/run-bench.js b/bin/run-bench.js old mode 100644 new mode 100755 index bc2f0e7dda..82127faae5 --- a/bin/run-bench.js +++ b/bin/run-bench.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node /* * Copyright 2022 New Relic Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 diff --git a/test/benchmark/Readme.md b/test/benchmark/Readme.md new file mode 100644 index 0000000000..b87843c0f0 --- /dev/null +++ b/test/benchmark/Readme.md @@ -0,0 +1,18 @@ +## Running Benchmarks + +The easiest way to run all benchmarks is by using the npm script: + +```sh +> npm run bench +``` + +If you need to run a single benchmark suite, for example the sql parser +benchmarks, it is easiest to run and view the output by: + +```sh +> ./bin/run-bench.js lib/db/query-parsers/sql.bench.js && \ + cat benchmark_results/$(ls -1rt benchmark_results | tail -n 1) +``` + +Notice that we do not specify the leading "test/benchmark/" when providing +the benchmark file we want to run. diff --git a/test/benchmark/lib/db/query-parsers/sql.bench.js b/test/benchmark/lib/db/query-parsers/sql.bench.js new file mode 100644 index 0000000000..2f162fdee5 --- /dev/null +++ b/test/benchmark/lib/db/query-parsers/sql.bench.js @@ -0,0 +1,78 @@ +/* + * Copyright 2024 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const parseSql = require('../../../../../lib/db/query-parsers/sql') +const benchmark = require('../../../../lib/benchmark') +const suite = benchmark.createBenchmark({ name: 'parseSql', runs: 200_000 }) + +const tests = [ + { + name: 'leading-multi-line-comment-single-line', + fn: leadingMultiLineCommentSingleLine + }, + { + name: 'leading-multi-line-comment-multiple-lines', + fn: leadingMultiLineCommentMultipleLines + }, + { + name: 'single-embedded-multi-line-comment', + fn: singleEmbeddedMultiLineComment + }, + { + name: 'multiple-embedded-multi-line-comments', + fn: multipleEmbeddedMultiLineComments + }, + { + name: 'select-statement', + fn: selectStatement + }, + { + name: 'update-statement', + fn: updateStatement + }, + { + name: 'delete-statement', + fn: deleteStatement + } +] + +for (const test of tests) { + suite.add(test) +} +suite.run() + +function leadingMultiLineCommentSingleLine() { + parseSql(`/* insert into bar some stuff */ insert into foo (col1)`) +} + +function leadingMultiLineCommentMultipleLines() { + parseSql(`/*insert into bar some stuff*/ + insert into foo (col1) values('bar') + `) +} + +function singleEmbeddedMultiLineComment() { + parseSql(`insert /* insert into bar */ into foo`) +} + +function multipleEmbeddedMultiLineComments() { + parseSql(`insert /* comments! */ into /* insert into bar some stuff */ foo /* MOAR */ (col1)`) +} + +function selectStatement() { + parseSql( + `with foobar (col1) as cte select * from foo as a join on cte using (col1) where a.bar = 'baz'` + ) +} + +function updateStatement() { + parseSql(`update foo set bar = 'baz' where col1 = 1`) +} + +function deleteStatement() { + parseSql(`delete from foo where bar = 'baz'`) +}