|
| 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 | +//! mem_profile binary entrypoint |
| 19 | +use datafusion::error::Result; |
| 20 | +use std::{ |
| 21 | + io::{BufRead, BufReader}, |
| 22 | + process::{Command, Stdio}, |
| 23 | +}; |
| 24 | +use structopt::StructOpt; |
| 25 | + |
| 26 | +#[derive(Debug, StructOpt)] |
| 27 | +#[structopt(about = "memory profile command")] |
| 28 | +struct MemProfileOpt { |
| 29 | + #[structopt(subcommand)] |
| 30 | + command: BenchmarkCommand, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, StructOpt)] |
| 34 | +enum BenchmarkCommand { |
| 35 | + Tpch(TpchOpt), |
| 36 | + // TODO Add other benchmark commands here |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug, StructOpt)] |
| 40 | +struct TpchOpt { |
| 41 | + #[structopt(long, required = true)] |
| 42 | + path: String, |
| 43 | + |
| 44 | + /// Query number. If not specified, runs all queries |
| 45 | + #[structopt(short, long)] |
| 46 | + query: Option<usize>, |
| 47 | +} |
| 48 | + |
| 49 | +#[tokio::main] |
| 50 | +pub async fn main() -> Result<()> { |
| 51 | + // 1. parse args and check which benchmarks should be run |
| 52 | + let opt = MemProfileOpt::from_args(); |
| 53 | + |
| 54 | + // 2. prebuild test binary so that memory does not blow up due to build process |
| 55 | + // check binary file location |
| 56 | + println!("Pre-building benchmark binary..."); |
| 57 | + let status = Command::new("cargo") |
| 58 | + .args(["build", "--profile", "release-nonlto", "--bin", "dfbench"]) |
| 59 | + .status() |
| 60 | + .expect("Failed to build dfbench"); |
| 61 | + |
| 62 | + if !status.success() { |
| 63 | + panic!("Failed to build dfbench"); |
| 64 | + } |
| 65 | + println!("Benchmark binary built successfully."); |
| 66 | + |
| 67 | + // 3. create a subprocess, run each benchmark with args (1) (2) |
| 68 | + match opt.command { |
| 69 | + BenchmarkCommand::Tpch(tpch_opt) => { |
| 70 | + run_tpch_benchmark(tpch_opt).await?; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // (maybe we cannot support result file.. and just have to print..) |
| 75 | + Ok(()) |
| 76 | +} |
| 77 | + |
| 78 | +async fn run_tpch_benchmark(opt: TpchOpt) -> Result<()> { |
| 79 | + let mut args: Vec<String> = vec![ |
| 80 | + "./target/release-nonlto/dfbench".to_string(), |
| 81 | + "tpch".to_string(), |
| 82 | + "--iterations".to_string(), |
| 83 | + "1".to_string(), |
| 84 | + "--path".to_string(), |
| 85 | + opt.path.clone(), |
| 86 | + "--format".to_string(), |
| 87 | + "parquet".to_string(), |
| 88 | + "--partitions".to_string(), |
| 89 | + "4".to_string(), |
| 90 | + "--memory-stat-enabled".to_string(), |
| 91 | + "--query".to_string(), |
| 92 | + ]; |
| 93 | + |
| 94 | + let mut query_strings: Vec<String> = Vec::new(); |
| 95 | + if let Some(query_id) = opt.query { |
| 96 | + query_strings.push(query_id.to_string()); |
| 97 | + } else { |
| 98 | + // run all queries. |
| 99 | + for i in 1..=22 { |
| 100 | + query_strings.push(i.to_string()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let mut results = vec![]; |
| 105 | + for query_str in query_strings { |
| 106 | + args.push(query_str); |
| 107 | + let _ = run_query(&args, &mut results); |
| 108 | + args.pop(); |
| 109 | + } |
| 110 | + |
| 111 | + print_summary_table(&results); |
| 112 | + Ok(()) |
| 113 | +} |
| 114 | + |
| 115 | +fn run_query(args: &[String], results: &mut Vec<QueryResult>) -> Result<()> { |
| 116 | + let exec_path = &args[0]; |
| 117 | + let exec_args = &args[1..]; |
| 118 | + |
| 119 | + let mut child = Command::new(exec_path) |
| 120 | + .args(exec_args) |
| 121 | + .stdout(Stdio::piped()) |
| 122 | + .spawn() |
| 123 | + .expect("Failed to start benchmark"); |
| 124 | + |
| 125 | + let stdout = child.stdout.take().unwrap(); |
| 126 | + let reader = BufReader::new(stdout); |
| 127 | + |
| 128 | + // buffer stdout |
| 129 | + let lines: Result<Vec<String>, std::io::Error> = |
| 130 | + reader.lines().collect::<Result<_, _>>(); |
| 131 | + |
| 132 | + child |
| 133 | + .wait() |
| 134 | + .expect("Benchmark process exited with an error"); |
| 135 | + |
| 136 | + // parse after child process terminates |
| 137 | + let lines = lines?; |
| 138 | + let mut iter = lines.iter().peekable(); |
| 139 | + |
| 140 | + while let Some(line) = iter.next() { |
| 141 | + if let Some((query, duration_ms)) = parse_query_time(line) { |
| 142 | + if let Some(next_line) = iter.peek() { |
| 143 | + if let Some((vmpeak, vmhwm, resident)) = parse_vm_line(next_line) { |
| 144 | + results.push(QueryResult { |
| 145 | + query, |
| 146 | + duration_ms, |
| 147 | + vmpeak, |
| 148 | + vmhwm, |
| 149 | + resident, |
| 150 | + }); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + Ok(()) |
| 158 | +} |
| 159 | + |
| 160 | +#[derive(Debug)] |
| 161 | +struct QueryResult { |
| 162 | + query: usize, |
| 163 | + duration_ms: f64, |
| 164 | + vmpeak: String, |
| 165 | + vmhwm: String, |
| 166 | + resident: String, |
| 167 | +} |
| 168 | + |
| 169 | +fn parse_query_time(line: &str) -> Option<(usize, f64)> { |
| 170 | + let re = regex::Regex::new(r"Query (\d+) avg time: ([\d.]+) ms").unwrap(); |
| 171 | + if let Some(caps) = re.captures(line) { |
| 172 | + let query_id = caps[1].parse::<usize>().ok()?; |
| 173 | + let avg_time = caps[2].parse::<f64>().ok()?; |
| 174 | + Some((query_id, avg_time)) |
| 175 | + } else { |
| 176 | + None |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +fn parse_vm_line(line: &str) -> Option<(String, String, String)> { |
| 181 | + let re = regex::Regex::new( |
| 182 | + r"VmPeak:\s*([\d.]+\s*[A-Z]+),\s*VmHWM:\s*([\d.]+\s*[A-Z]+),\s*RSS:\s*([\d.]+\s*[A-Z]+)" |
| 183 | + ).ok()?; |
| 184 | + let caps = re.captures(line)?; |
| 185 | + let vmpeak = caps.get(1)?.as_str().to_string(); |
| 186 | + let vmhwm = caps.get(2)?.as_str().to_string(); |
| 187 | + let resident = caps.get(3)?.as_str().to_string(); |
| 188 | + Some((vmpeak, vmhwm, resident)) |
| 189 | +} |
| 190 | + |
| 191 | +// Print as simple aligned table |
| 192 | +fn print_summary_table(results: &[QueryResult]) { |
| 193 | + println!( |
| 194 | + "\n{:<8} {:>10} {:>12} {:>12} {:>12}", |
| 195 | + "Query", "Time (ms)", "VmPeak", "VmHWM", "RSS" |
| 196 | + ); |
| 197 | + println!("{}", "-".repeat(68)); |
| 198 | + |
| 199 | + for r in results { |
| 200 | + println!( |
| 201 | + "{:<8} {:>10.2} {:>12} {:>12} {:>12}", |
| 202 | + r.query, r.duration_ms, r.vmpeak, r.vmhwm, r.resident |
| 203 | + ); |
| 204 | + } |
| 205 | +} |
0 commit comments