|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#[macro_use] |
| 19 | +extern crate criterion; |
| 20 | +extern crate arrow; |
| 21 | +extern crate datafusion; |
| 22 | + |
| 23 | +mod data_utils; |
| 24 | +use crate::criterion::Criterion; |
| 25 | +use data_utils::{create_table_provider, make_data}; |
| 26 | +use datafusion::execution::context::SessionContext; |
| 27 | +use datafusion::physical_plan::{collect, ExecutionPlan}; |
| 28 | +use datafusion::{datasource::MemTable, error::Result}; |
| 29 | +use datafusion_execution::config::SessionConfig; |
| 30 | +use datafusion_execution::TaskContext; |
| 31 | + |
| 32 | +use parking_lot::Mutex; |
| 33 | +use std::{sync::Arc, time::Duration}; |
| 34 | +use tokio::runtime::Runtime; |
| 35 | + |
| 36 | +fn query(ctx: Arc<Mutex<SessionContext>>, sql: &str) { |
| 37 | + let rt = Runtime::new().unwrap(); |
| 38 | + let df = rt.block_on(ctx.lock().sql(sql)).unwrap(); |
| 39 | + criterion::black_box(rt.block_on(df.collect()).unwrap()); |
| 40 | +} |
| 41 | + |
| 42 | +fn create_context( |
| 43 | + partitions_len: usize, |
| 44 | + array_len: usize, |
| 45 | + batch_size: usize, |
| 46 | +) -> Result<Arc<Mutex<SessionContext>>> { |
| 47 | + let ctx = SessionContext::new(); |
| 48 | + let provider = create_table_provider(partitions_len, array_len, batch_size)?; |
| 49 | + ctx.register_table("t", provider)?; |
| 50 | + Ok(Arc::new(Mutex::new(ctx))) |
| 51 | +} |
| 52 | + |
| 53 | +fn criterion_benchmark_limited_distinct(c: &mut Criterion) { |
| 54 | + let partitions_len = 10; |
| 55 | + let array_len = 1 << 26; // 64 M |
| 56 | + let batch_size = 8192; |
| 57 | + let ctx = create_context(partitions_len, array_len, batch_size).unwrap(); |
| 58 | + |
| 59 | + let mut group = c.benchmark_group("custom-measurement-time"); |
| 60 | + group.measurement_time(Duration::from_secs(40)); |
| 61 | + |
| 62 | + group.bench_function("distinct_group_by_u64_narrow_limit_10", |b| { |
| 63 | + b.iter(|| { |
| 64 | + query( |
| 65 | + ctx.clone(), |
| 66 | + "SELECT DISTINCT u64_narrow FROM t GROUP BY u64_narrow LIMIT 10", |
| 67 | + ) |
| 68 | + }) |
| 69 | + }); |
| 70 | + |
| 71 | + group.bench_function("distinct_group_by_u64_narrow_limit_100", |b| { |
| 72 | + b.iter(|| { |
| 73 | + query( |
| 74 | + ctx.clone(), |
| 75 | + "SELECT DISTINCT u64_narrow FROM t GROUP BY u64_narrow LIMIT 100", |
| 76 | + ) |
| 77 | + }) |
| 78 | + }); |
| 79 | + |
| 80 | + group.bench_function("distinct_group_by_u64_narrow_limit_1000", |b| { |
| 81 | + b.iter(|| { |
| 82 | + query( |
| 83 | + ctx.clone(), |
| 84 | + "SELECT DISTINCT u64_narrow FROM t GROUP BY u64_narrow LIMIT 1000", |
| 85 | + ) |
| 86 | + }) |
| 87 | + }); |
| 88 | + |
| 89 | + group.bench_function("distinct_group_by_u64_narrow_limit_10000", |b| { |
| 90 | + b.iter(|| { |
| 91 | + query( |
| 92 | + ctx.clone(), |
| 93 | + "SELECT DISTINCT u64_narrow FROM t GROUP BY u64_narrow LIMIT 10000", |
| 94 | + ) |
| 95 | + }) |
| 96 | + }); |
| 97 | + |
| 98 | + group.bench_function("group_by_multiple_columns_limit_10", |b| { |
| 99 | + b.iter(|| { |
| 100 | + query( |
| 101 | + ctx.clone(), |
| 102 | + "SELECT u64_narrow, u64_wide, utf8, f64 FROM t GROUP BY 1, 2, 3, 4 LIMIT 10", |
| 103 | + ) |
| 104 | + }) |
| 105 | + }); |
| 106 | + group.finish(); |
| 107 | +} |
| 108 | + |
| 109 | +async fn distinct_with_limit( |
| 110 | + plan: Arc<dyn ExecutionPlan>, |
| 111 | + ctx: Arc<TaskContext>, |
| 112 | +) -> Result<()> { |
| 113 | + let batches = collect(plan, ctx).await?; |
| 114 | + assert_eq!(batches.len(), 1); |
| 115 | + let batch = batches.first().unwrap(); |
| 116 | + assert_eq!(batch.num_rows(), 10); |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +fn run(plan: Arc<dyn ExecutionPlan>, ctx: Arc<TaskContext>) { |
| 122 | + let rt = Runtime::new().unwrap(); |
| 123 | + criterion::black_box( |
| 124 | + rt.block_on(async { distinct_with_limit(plan.clone(), ctx.clone()).await }), |
| 125 | + ) |
| 126 | + .unwrap(); |
| 127 | +} |
| 128 | + |
| 129 | +pub async fn create_context_sampled_data( |
| 130 | + sql: &str, |
| 131 | + partition_cnt: i32, |
| 132 | + sample_cnt: i32, |
| 133 | +) -> Result<(Arc<dyn ExecutionPlan>, Arc<TaskContext>)> { |
| 134 | + let (schema, parts) = make_data(partition_cnt, sample_cnt, false /* asc */).unwrap(); |
| 135 | + let mem_table = Arc::new(MemTable::try_new(schema, parts).unwrap()); |
| 136 | + |
| 137 | + // Create the DataFrame |
| 138 | + let cfg = SessionConfig::new(); |
| 139 | + let ctx = SessionContext::new_with_config(cfg); |
| 140 | + let _ = ctx.register_table("traces", mem_table)?; |
| 141 | + let df = ctx.sql(sql).await?; |
| 142 | + let physical_plan = df.create_physical_plan().await?; |
| 143 | + Ok((physical_plan, ctx.task_ctx())) |
| 144 | +} |
| 145 | + |
| 146 | +fn criterion_benchmark_limited_distinct_sampled(c: &mut Criterion) { |
| 147 | + let rt = Runtime::new().unwrap(); |
| 148 | + |
| 149 | + let limit = 10; |
| 150 | + let partitions = 100; |
| 151 | + let samples = 100_000; |
| 152 | + let sql = |
| 153 | + format!("select DISTINCT trace_id from traces group by trace_id limit {limit};"); |
| 154 | + |
| 155 | + let distinct_trace_id_100_partitions_100_000_samples_limit_100 = rt.block_on(async { |
| 156 | + create_context_sampled_data(sql.as_str(), partitions, samples) |
| 157 | + .await |
| 158 | + .unwrap() |
| 159 | + }); |
| 160 | + |
| 161 | + c.bench_function( |
| 162 | + format!("distinct query with {} partitions and {} samples per partition with limit {}", partitions, samples, limit).as_str(), |
| 163 | + |b| b.iter(|| run(distinct_trace_id_100_partitions_100_000_samples_limit_100.0.clone(), |
| 164 | + distinct_trace_id_100_partitions_100_000_samples_limit_100.1.clone())), |
| 165 | + ); |
| 166 | + |
| 167 | + let partitions = 10; |
| 168 | + let samples = 1_000_000; |
| 169 | + let sql = |
| 170 | + format!("select DISTINCT trace_id from traces group by trace_id limit {limit};"); |
| 171 | + |
| 172 | + let distinct_trace_id_10_partitions_1_000_000_samples_limit_10 = rt.block_on(async { |
| 173 | + create_context_sampled_data(sql.as_str(), partitions, samples) |
| 174 | + .await |
| 175 | + .unwrap() |
| 176 | + }); |
| 177 | + |
| 178 | + c.bench_function( |
| 179 | + format!("distinct query with {} partitions and {} samples per partition with limit {}", partitions, samples, limit).as_str(), |
| 180 | + |b| b.iter(|| run(distinct_trace_id_10_partitions_1_000_000_samples_limit_10.0.clone(), |
| 181 | + distinct_trace_id_10_partitions_1_000_000_samples_limit_10.1.clone())), |
| 182 | + ); |
| 183 | + |
| 184 | + let partitions = 1; |
| 185 | + let samples = 10_000_000; |
| 186 | + let sql = |
| 187 | + format!("select DISTINCT trace_id from traces group by trace_id limit {limit};"); |
| 188 | + |
| 189 | + let rt = Runtime::new().unwrap(); |
| 190 | + let distinct_trace_id_1_partition_10_000_000_samples_limit_10 = rt.block_on(async { |
| 191 | + create_context_sampled_data(sql.as_str(), partitions, samples) |
| 192 | + .await |
| 193 | + .unwrap() |
| 194 | + }); |
| 195 | + |
| 196 | + c.bench_function( |
| 197 | + format!("distinct query with {} partitions and {} samples per partition with limit {}", partitions, samples, limit).as_str(), |
| 198 | + |b| b.iter(|| run(distinct_trace_id_1_partition_10_000_000_samples_limit_10.0.clone(), |
| 199 | + distinct_trace_id_1_partition_10_000_000_samples_limit_10.1.clone())), |
| 200 | + ); |
| 201 | +} |
| 202 | + |
| 203 | +criterion_group!( |
| 204 | + benches, |
| 205 | + criterion_benchmark_limited_distinct, |
| 206 | + criterion_benchmark_limited_distinct_sampled |
| 207 | +); |
| 208 | +criterion_main!(benches); |
0 commit comments