|
| 1 | +import argparse |
| 2 | +import logging |
| 3 | +import typing |
| 4 | +from dataclasses import dataclass |
| 5 | +from datetime import datetime, timedelta |
| 6 | +from pathlib import Path |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import ray |
| 10 | + |
| 11 | +import daft |
| 12 | + |
| 13 | +from ..tpch import __main__ as tpch |
| 14 | +from ..tpch import ray_job_runner |
| 15 | +from . import datagen, helpers |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | +SQL_QUERIES_PATH = Path(__file__).parent / "queries" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class ParsedArgs: |
| 24 | + tpcds_gen_folder: Path |
| 25 | + scale_factor: float |
| 26 | + questions: str |
| 27 | + ray_address: Optional[str] |
| 28 | + dry_run: bool |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class RunArgs: |
| 33 | + scaled_tpcds_gen_folder: Path |
| 34 | + query_indices: list[int] |
| 35 | + ray_address: Optional[str] |
| 36 | + dry_run: bool |
| 37 | + |
| 38 | + |
| 39 | +@dataclass |
| 40 | +class Result: |
| 41 | + index: int |
| 42 | + duration: Optional[timedelta] |
| 43 | + error_msg: Optional[str] |
| 44 | + |
| 45 | + def __repr__(self) -> str: |
| 46 | + if self.duration and self.error_msg: |
| 47 | + typing.assert_never("Both duration and error_msg are not None") |
| 48 | + elif self.duration: |
| 49 | + return f"(Q{self.index} SUCCESS - duration: {self.duration})" |
| 50 | + elif self.error_msg: |
| 51 | + return f"(Q{self.index} FAILURE - error msg: {self.error_msg})" |
| 52 | + else: |
| 53 | + typing.assert_never("Both duration and error_msg are None") |
| 54 | + |
| 55 | + |
| 56 | +def run_query_on_ray( |
| 57 | + run_args: RunArgs, |
| 58 | +) -> list[Result]: |
| 59 | + ray.init(address=run_args.ray_address if run_args.ray_address else None) |
| 60 | + results = [] |
| 61 | + |
| 62 | + for query_index in run_args.query_indices: |
| 63 | + working_dir = Path("benchmarking") / "tpcds" |
| 64 | + ray_entrypoint_script = "ray_entrypoint.py" |
| 65 | + duration = None |
| 66 | + error_msg = None |
| 67 | + try: |
| 68 | + start = datetime.now() |
| 69 | + ray_job_runner.run_on_ray( |
| 70 | + run_args.ray_address, |
| 71 | + { |
| 72 | + "entrypoint": f"python {ray_entrypoint_script} --tpcds-gen-folder 'data/0.01' --question {query_index} {'--dry-run' if run_args.dry_run else ''}", |
| 73 | + "runtime_env": { |
| 74 | + "working_dir": working_dir, |
| 75 | + }, |
| 76 | + }, |
| 77 | + ) |
| 78 | + end = datetime.now() |
| 79 | + duration = end - start |
| 80 | + except Exception as e: |
| 81 | + error_msg = str(e) |
| 82 | + |
| 83 | + results.append(Result(index=query_index, duration=duration, error_msg=error_msg)) |
| 84 | + |
| 85 | + return results |
| 86 | + |
| 87 | + |
| 88 | +def run_query_on_local( |
| 89 | + run_args: RunArgs, |
| 90 | +) -> list[Result]: |
| 91 | + catalog = helpers.generate_catalog(run_args.scaled_tpcds_gen_folder) |
| 92 | + results = [] |
| 93 | + |
| 94 | + for query_index in run_args.query_indices: |
| 95 | + query_file = SQL_QUERIES_PATH / f"{query_index:02}.sql" |
| 96 | + with open(query_file) as f: |
| 97 | + query = f.read() |
| 98 | + |
| 99 | + start = datetime.now() |
| 100 | + |
| 101 | + duration = None |
| 102 | + error_msg = None |
| 103 | + try: |
| 104 | + daft.sql(query, catalog=catalog).explain(show_all=True) |
| 105 | + if not run_args.dry_run: |
| 106 | + daft.sql(query, catalog=catalog).collect() |
| 107 | + |
| 108 | + end = datetime.now() |
| 109 | + duration = end - start |
| 110 | + except Exception as e: |
| 111 | + error_msg = str(e) |
| 112 | + |
| 113 | + results.append(Result(index=query_index, duration=duration, error_msg=error_msg)) |
| 114 | + |
| 115 | + return results |
| 116 | + |
| 117 | + |
| 118 | +def run_benchmarks( |
| 119 | + run_args: RunArgs, |
| 120 | +) -> list[Result]: |
| 121 | + logger.info( |
| 122 | + "Running the following questions: %s", |
| 123 | + run_args.query_indices, |
| 124 | + ) |
| 125 | + |
| 126 | + runner = tpch.get_daft_benchmark_runner_name() |
| 127 | + |
| 128 | + logger.info( |
| 129 | + "Running on the following runner: %s", |
| 130 | + runner, |
| 131 | + ) |
| 132 | + |
| 133 | + if runner == "ray": |
| 134 | + return run_query_on_ray(run_args) |
| 135 | + elif runner == "py" or runner == "native": |
| 136 | + return run_query_on_local(run_args) |
| 137 | + else: |
| 138 | + typing.assert_never(runner) |
| 139 | + |
| 140 | + |
| 141 | +def main(args: ParsedArgs): |
| 142 | + scaled_tpcds_gen_folder = args.tpcds_gen_folder / str(args.scale_factor) |
| 143 | + datagen.gen_tpcds(scaled_tpcds_gen_folder, args.scale_factor) |
| 144 | + query_indices = helpers.parse_questions_str(args.questions) |
| 145 | + results = run_benchmarks( |
| 146 | + RunArgs( |
| 147 | + scaled_tpcds_gen_folder=scaled_tpcds_gen_folder, |
| 148 | + query_indices=query_indices, |
| 149 | + ray_address=args.ray_address, |
| 150 | + dry_run=args.dry_run, |
| 151 | + ) |
| 152 | + ) |
| 153 | + |
| 154 | + # TODO(ronnie): improve visualization of results; simply printing them to console is not the best way... |
| 155 | + print(f"{results=}") |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + logging.basicConfig(level="INFO") |
| 160 | + |
| 161 | + parser = argparse.ArgumentParser() |
| 162 | + parser.add_argument( |
| 163 | + "--tpcds-gen-folder", |
| 164 | + default="benchmarking/tpcds/data", |
| 165 | + type=Path, |
| 166 | + help="Path to the folder containing the TPC-DS dsdgen tool and generated data", |
| 167 | + ) |
| 168 | + parser.add_argument("--scale-factor", default=0.01, type=float, help="Scale factor to run on in GB") |
| 169 | + parser.add_argument("--questions", default="*", type=str, help="The questions to run") |
| 170 | + parser.add_argument("--ray-address", type=str, help="The address of the head node of the ray cluster") |
| 171 | + parser.add_argument( |
| 172 | + "--dry-run", |
| 173 | + action="store_true", |
| 174 | + help="Whether to run in dry-run mode; if true, only the plan will be printed, but no query will be executed", |
| 175 | + ) |
| 176 | + args = parser.parse_args() |
| 177 | + |
| 178 | + tpcds_gen_folder: Path = args.tpcds_gen_folder |
| 179 | + assert args.scale_factor > 0 |
| 180 | + |
| 181 | + main( |
| 182 | + ParsedArgs( |
| 183 | + tpcds_gen_folder=tpcds_gen_folder, |
| 184 | + scale_factor=args.scale_factor, |
| 185 | + questions=args.questions, |
| 186 | + ray_address=args.ray_address, |
| 187 | + dry_run=args.dry_run, |
| 188 | + ) |
| 189 | + ) |
0 commit comments