From 49d61b1bea219a96cc7483538e4efe14895f1ba3 Mon Sep 17 00:00:00 2001 From: Ellen Marie Dash Date: Thu, 13 Feb 2025 19:20:18 -0500 Subject: [PATCH 1/2] Add pg_query_parse and pg_query_parse_protobuf benchmarks. --- Cargo.lock | 23 ++++++++++++++++++++ Cargo.toml | 5 +++++ benches/parse.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 benches/parse.rs diff --git a/Cargo.lock b/Cargo.lock index 9efd998..cfc9b16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,6 +81,16 @@ dependencies = [ "constant_time_eq", ] +[[package]] +name = "brunch" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "016d950e43311624fa0b3e1bfe340f49f1913d21d76165f883ede0cfee569b62" +dependencies = [ + "dactyl", + "unicode-width", +] + [[package]] name = "byteorder" version = "1.4.3" @@ -161,6 +171,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "dactyl" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a4d2c8b71b31d345e76c2532f9c9a99eae384ec1f47a6eb6347e35b5645aae4" + [[package]] name = "diff" version = "0.1.13" @@ -393,6 +409,7 @@ name = "pg_query" version = "6.0.0" dependencies = [ "bindgen", + "brunch", "cc", "clippy", "easy-parallel", @@ -702,6 +719,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index ab8cdcc..d5aad98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,3 +30,8 @@ glob = "0.3.1" easy-parallel = "3.2.0" pretty_assertions = "1.4.0" regex = "1.6.0" +brunch = "0.8.*" + +[[bench]] +name = "parse" +harness = false diff --git a/benches/parse.rs b/benches/parse.rs new file mode 100644 index 0000000..ff2e347 --- /dev/null +++ b/benches/parse.rs @@ -0,0 +1,56 @@ +use std::ffi::{c_char, CString}; +use brunch::Bench; +use pg_query; +use pg_query::bindings::{pg_query_parse, pg_query_parse_protobuf}; + +fn build_query(table_references: i32) -> String { + let mut query = "SELECT * FROM t".to_string(); + for i in 0..table_references { + query = format!("{query} JOIN t{i} ON t.id = t{i}.t_id AND t{i}.k IN (1, 2, 3, 4) AND t{i}.f IN (SELECT o FROM p WHERE q = 'foo')"); + } + query +} + +fn seed() -> String { + build_query(100) +} + +fn c_seed() -> CString { + CString::new(seed()).unwrap() +} + +// pub fn pg_query_parse_protobuf(input: *const ::std::os::raw::c_char) -> PgQueryProtobufParseResult; +// pub fn pg_query_parse_protobuf_opts(input: *const ::std::os::raw::c_char, parser_options: ::std::os::raw::c_int) -> PgQueryProtobufParseResult; + +// pub fn pg_query_parse(input: *const ::std::os::raw::c_char) -> PgQueryParseResult; +// pub fn pg_query_parse_opts(input: *const ::std::os::raw::c_char, parser_options: ::std::os::raw::c_int) -> PgQueryParseResult; + +// pg_query_raw_parse ? + +brunch::benches!( + Bench::new("pg_query_parse") + .run_seeded_with(c_seed, |query| { + unsafe { pg_query_parse(query.as_ptr() as *const c_char) } + //let result = pg_query::parse(&query); + //assert!(result.is_ok()); + }), + + Bench::new("pg_query_parse_protobuf") + .run_seeded_with(c_seed, |query| { + unsafe { pg_query_parse_protobuf(query.as_ptr() as *const c_char) } + }), + + /* + Bench::new("pg_query_raw_parse") + .run_seeded_with(seed, |query| { + }), + */ + + // This was entirely for my own curiousity. -@duckinator + /* + Bench::new("pg_query::parse (uses parse_protobuf)") + .run_seeded_with(seed, |query| { + pg_query::parse(&query).unwrap() + }), + */ +); diff --git a/src/lib.rs b/src/lib.rs index 1e3b3e1..9d912fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ //! ``` //! -mod bindings; +pub mod bindings; mod error; mod node_enum; mod node_mut; From 99a414ad2f04b3dbb9fe82b215bc0bc3d443c809 Mon Sep 17 00:00:00 2001 From: Ellen Marie Dash Date: Thu, 13 Feb 2025 21:01:22 -0500 Subject: [PATCH 2/2] Split benchmarks into multiple files, to allow generation of per-benchmark flamegraphs. --- Cargo.toml | 7 +++++++ benches/helpers.rs | 17 +++++++++++++++++ benches/parse.rs | 39 ++++----------------------------------- benches/parse_protobuf.rs | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 benches/helpers.rs create mode 100644 benches/parse_protobuf.rs diff --git a/Cargo.toml b/Cargo.toml index d5aad98..46f185f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,13 @@ pretty_assertions = "1.4.0" regex = "1.6.0" brunch = "0.8.*" +[profile.bench] +debug = true + [[bench]] name = "parse" harness = false + +[[bench]] +name = "parse_protobuf" +harness = false diff --git a/benches/helpers.rs b/benches/helpers.rs new file mode 100644 index 0000000..ba0d4ff --- /dev/null +++ b/benches/helpers.rs @@ -0,0 +1,17 @@ +use std::ffi::CString; + +pub fn build_query(table_references: i32) -> String { + let mut query = "SELECT * FROM t".to_string(); + for i in 0..table_references { + query = format!("{query} JOIN t{i} ON t.id = t{i}.t_id AND t{i}.k IN (1, 2, 3, 4) AND t{i}.f IN (SELECT o FROM p WHERE q = 'foo')"); + } + query +} + +pub fn seed() -> String { + build_query(100) +} + +pub fn c_seed() -> CString { + CString::new(seed()).unwrap() +} diff --git a/benches/parse.rs b/benches/parse.rs index ff2e347..82a5684 100644 --- a/benches/parse.rs +++ b/benches/parse.rs @@ -1,23 +1,11 @@ -use std::ffi::{c_char, CString}; +mod helpers; +use helpers::*; + +use std::ffi::c_char; use brunch::Bench; use pg_query; use pg_query::bindings::{pg_query_parse, pg_query_parse_protobuf}; -fn build_query(table_references: i32) -> String { - let mut query = "SELECT * FROM t".to_string(); - for i in 0..table_references { - query = format!("{query} JOIN t{i} ON t.id = t{i}.t_id AND t{i}.k IN (1, 2, 3, 4) AND t{i}.f IN (SELECT o FROM p WHERE q = 'foo')"); - } - query -} - -fn seed() -> String { - build_query(100) -} - -fn c_seed() -> CString { - CString::new(seed()).unwrap() -} // pub fn pg_query_parse_protobuf(input: *const ::std::os::raw::c_char) -> PgQueryProtobufParseResult; // pub fn pg_query_parse_protobuf_opts(input: *const ::std::os::raw::c_char, parser_options: ::std::os::raw::c_int) -> PgQueryProtobufParseResult; @@ -34,23 +22,4 @@ brunch::benches!( //let result = pg_query::parse(&query); //assert!(result.is_ok()); }), - - Bench::new("pg_query_parse_protobuf") - .run_seeded_with(c_seed, |query| { - unsafe { pg_query_parse_protobuf(query.as_ptr() as *const c_char) } - }), - - /* - Bench::new("pg_query_raw_parse") - .run_seeded_with(seed, |query| { - }), - */ - - // This was entirely for my own curiousity. -@duckinator - /* - Bench::new("pg_query::parse (uses parse_protobuf)") - .run_seeded_with(seed, |query| { - pg_query::parse(&query).unwrap() - }), - */ ); diff --git a/benches/parse_protobuf.rs b/benches/parse_protobuf.rs new file mode 100644 index 0000000..5c1e074 --- /dev/null +++ b/benches/parse_protobuf.rs @@ -0,0 +1,15 @@ +mod helpers; +use helpers::*; + +use std::ffi::c_char; +use brunch::Bench; +use pg_query; +use pg_query::bindings::{pg_query_parse, pg_query_parse_protobuf}; + + +brunch::benches!( + Bench::new("pg_query_parse_protobuf") + .run_seeded_with(c_seed, |query| { + unsafe { pg_query_parse_protobuf(query.as_ptr() as *const c_char) } + }), +);