Skip to content

Commit 6e6fae7

Browse files
authored
Add benchmarks using cargo bench / criterion (#190)
1 parent d32df52 commit 6e6fae7

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

docs/benchmarking.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Benchmarking
2+
3+
Run `cargo bench` in the project `sqlparser_bench` execute the queries.
4+
It will report results using the `criterion` library to perform the benchmarking.
5+
6+
The bench project lives in another crate, to avoid the negative impact on building the `sqlparser` crate.

sqlparser_bench/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "sqlparser_bench"
3+
version = "0.1.0"
4+
authors = ["Dandandan <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
sqlparser = { path = "../", version = "0.5.1-alpha-0" }
11+
12+
[dev-dependencies]
13+
criterion = {version = "0.3"}
14+
15+
[[bench]]
16+
name = "sqlparser_bench"
17+
harness = false
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
use criterion::{criterion_group, criterion_main, Criterion};
14+
use sqlparser::dialect::GenericDialect;
15+
use sqlparser::parser::Parser;
16+
17+
fn basic_queries(c: &mut Criterion) {
18+
let mut group = c.benchmark_group("sqlparser-rs parsing benchmark");
19+
let dialect = GenericDialect {};
20+
21+
let string = "SELECT * FROM table WHERE 1 = 1";
22+
group.bench_function("sqlparser::select", |b| {
23+
b.iter(|| Parser::parse_sql(&dialect, string));
24+
});
25+
26+
let with_query = "
27+
WITH derived AS (
28+
SELECT MAX(a) AS max_a,
29+
COUNT(b) AS b_num,
30+
user_id
31+
FROM TABLE
32+
GROUP BY user_id
33+
)
34+
SELECT * FROM table
35+
LEFT JOIN derived USING (user_id)
36+
";
37+
group.bench_function("sqlparser::with_select", |b| {
38+
b.iter(|| Parser::parse_sql(&dialect, with_query));
39+
});
40+
}
41+
42+
criterion_group!(benches, basic_queries);
43+
criterion_main!(benches);

sqlparser_bench/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[cfg(test)]
2+
mod tests {
3+
#[test]
4+
fn it_works() {
5+
assert_eq!(2 + 2, 4);
6+
}
7+
}

0 commit comments

Comments
 (0)