Skip to content

Commit

Permalink
chore: Added a benchmark script for our sql parser (#2708)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr authored Nov 7, 2024
1 parent 4432c42 commit 9b6de68
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/run-bench.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
/*
* Copyright 2022 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
Expand Down
18 changes: 18 additions & 0 deletions test/benchmark/Readme.md
Original file line number Diff line number Diff line change
@@ -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.
78 changes: 78 additions & 0 deletions test/benchmark/lib/db/query-parsers/sql.bench.js
Original file line number Diff line number Diff line change
@@ -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'`)
}

0 comments on commit 9b6de68

Please sign in to comment.