Skip to content

Apache DataFusion SQL Query Engine

bench: add `range_and_generate_series` (#19428)

Raz Luvaton4249e4ecd354
diff --git a/datafusion/core/Cargo.toml b/datafusion/core/Cargo.toml
index d2ecd34886..f584fea0b5 100644
--- a/datafusion/core/Cargo.toml
+++ b/datafusion/core/Cargo.toml
@@ -238,6 +238,10 @@ harness = false
name = "parquet_query_sql"
required-features = ["parquet"]
+[[bench]]
+harness = false
+name = "range_and_generate_series"
+
[[bench]]
harness = false
name = "sql_planner"
diff --git a/datafusion/core/benches/range_and_generate_series.rs b/datafusion/core/benches/range_and_generate_series.rs
new file mode 100644
index 0000000000..2b1463a210
--- /dev/null
+++ b/datafusion/core/benches/range_and_generate_series.rs
@@ -0,0 +1,90 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate criterion;
+extern crate datafusion;
+
+mod data_utils;
+
+use crate::criterion::Criterion;
+use datafusion::execution::context::SessionContext;
+use parking_lot::Mutex;
+use std::hint::black_box;
+use std::sync::Arc;
+use tokio::runtime::Runtime;
+
+#[expect(clippy::needless_pass_by_value)]
+fn query(ctx: Arc<Mutex<SessionContext>>, rt: &Runtime, sql: &str) {
+ let df = rt.block_on(ctx.lock().sql(sql)).unwrap();
+ black_box(rt.block_on(df.collect()).unwrap());
+}
+
+fn create_context() -> Arc<Mutex<SessionContext>> {
+ let ctx = SessionContext::new();
+ Arc::new(Mutex::new(ctx))
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let ctx = create_context();
+ let rt = Runtime::new().unwrap();
+
+ c.bench_function("range(1000000)", |b| {
+ b.iter(|| query(ctx.clone(), &rt, "SELECT value from range(1000000)"))
+ });
+
+ c.bench_function("generate_series(1000000)", |b| {
+ b.iter(|| {
+ query(
+ ctx.clone(),
+ &rt,
+ "SELECT value from generate_series(1000000)",
+ )
+ })
+ });
+
+ c.bench_function("range(0, 1000000, 5)", |b| {
+ b.iter(|| query(ctx.clone(), &rt, "SELECT value from range(0, 1000000, 5)"))
+ });
+
+ c.bench_function("generate_series(0, 1000000, 5)", |b| {
+ b.iter(|| {
+ query(
+ ctx.clone(),
+ &rt,
+ "SELECT value from generate_series(0, 1000000, 5)",
+ )
+ })
+ });
+
+ c.bench_function("range(1000000, 0, -5)", |b| {
+ b.iter(|| query(ctx.clone(), &rt, "SELECT value from range(1000000, 0, -5)"))
+ });
+
+ c.bench_function("generate_series(1000000, 0, -5)", |b| {
+ b.iter(|| {
+ query(
+ ctx.clone(),
+ &rt,
+ "SELECT value from generate_series(1000000, 0, -5)",
+ )
+ })
+ });
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);