Skip to content

Commit 9b6de68

Browse files
authored
chore: Added a benchmark script for our sql parser (#2708)
1 parent 4432c42 commit 9b6de68

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

bin/run-bench.js

100644100755
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
/*
23
* Copyright 2022 New Relic Corporation. All rights reserved.
34
* SPDX-License-Identifier: Apache-2.0

test/benchmark/Readme.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Running Benchmarks
2+
3+
The easiest way to run all benchmarks is by using the npm script:
4+
5+
```sh
6+
> npm run bench
7+
```
8+
9+
If you need to run a single benchmark suite, for example the sql parser
10+
benchmarks, it is easiest to run and view the output by:
11+
12+
```sh
13+
> ./bin/run-bench.js lib/db/query-parsers/sql.bench.js && \
14+
cat benchmark_results/$(ls -1rt benchmark_results | tail -n 1)
15+
```
16+
17+
Notice that we do not specify the leading "test/benchmark/" when providing
18+
the benchmark file we want to run.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2024 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const parseSql = require('../../../../../lib/db/query-parsers/sql')
9+
const benchmark = require('../../../../lib/benchmark')
10+
const suite = benchmark.createBenchmark({ name: 'parseSql', runs: 200_000 })
11+
12+
const tests = [
13+
{
14+
name: 'leading-multi-line-comment-single-line',
15+
fn: leadingMultiLineCommentSingleLine
16+
},
17+
{
18+
name: 'leading-multi-line-comment-multiple-lines',
19+
fn: leadingMultiLineCommentMultipleLines
20+
},
21+
{
22+
name: 'single-embedded-multi-line-comment',
23+
fn: singleEmbeddedMultiLineComment
24+
},
25+
{
26+
name: 'multiple-embedded-multi-line-comments',
27+
fn: multipleEmbeddedMultiLineComments
28+
},
29+
{
30+
name: 'select-statement',
31+
fn: selectStatement
32+
},
33+
{
34+
name: 'update-statement',
35+
fn: updateStatement
36+
},
37+
{
38+
name: 'delete-statement',
39+
fn: deleteStatement
40+
}
41+
]
42+
43+
for (const test of tests) {
44+
suite.add(test)
45+
}
46+
suite.run()
47+
48+
function leadingMultiLineCommentSingleLine() {
49+
parseSql(`/* insert into bar some stuff */ insert into foo (col1)`)
50+
}
51+
52+
function leadingMultiLineCommentMultipleLines() {
53+
parseSql(`/*insert into bar some stuff*/
54+
insert into foo (col1) values('bar')
55+
`)
56+
}
57+
58+
function singleEmbeddedMultiLineComment() {
59+
parseSql(`insert /* insert into bar */ into foo`)
60+
}
61+
62+
function multipleEmbeddedMultiLineComments() {
63+
parseSql(`insert /* comments! */ into /* insert into bar some stuff */ foo /* MOAR */ (col1)`)
64+
}
65+
66+
function selectStatement() {
67+
parseSql(
68+
`with foobar (col1) as cte select * from foo as a join on cte using (col1) where a.bar = 'baz'`
69+
)
70+
}
71+
72+
function updateStatement() {
73+
parseSql(`update foo set bar = 'baz' where col1 = 1`)
74+
}
75+
76+
function deleteStatement() {
77+
parseSql(`delete from foo where bar = 'baz'`)
78+
}

0 commit comments

Comments
 (0)