hyperdb-api 0.1.1

Pure Rust API for Hyper database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! gRPC performance benchmarks for Hyper API
//!
//! These benchmarks test gRPC query performance with various data sizes
//! and transfer modes, measuring both rows/second and MB/second throughput.
//!
//! # Running this example
//!
//! ```bash
//! cargo run -p hyperdb-api --example grpc_benchmark_tests --release
//! ```

// Benchmark harness: intentional wide→narrow conversions for row-count display
// and throughput math (f64 rows/sec rounded back to u64 for formatting).
#![expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    reason = "benchmark harness: throughput math casts f64 → u64 for display"
)]

use hyperdb_api::grpc::{GrpcConfig, GrpcConnection, TransferMode};
use hyperdb_api::{HyperProcess, ListenMode, Parameters, Result};
use std::time::Instant;

// ============================================================================
// Benchmark Configuration
// ============================================================================

/// Scale points for testing (in rows)
/// Reduced for faster example execution (full benchmarks can be run separately)
const SCALE_POINTS: &[u64] = &[
    10_000,  // 10K - quick sanity check
    100_000, // 100K - small dataset
];

/// All transfer modes to test
const TRANSFER_MODES: &[(TransferMode, &str)] = &[
    (TransferMode::Sync, "SYNC"),
    (TransferMode::Adaptive, "ADAPTIVE"),
    (TransferMode::Async, "ASYNC"),
];

// ============================================================================
// Result Types
// ============================================================================

/// Benchmark result with timing and throughput metrics
#[derive(Debug, Clone)]
struct BenchmarkResult {
    mode: &'static str,
    row_count: u64,
    data_size_bytes: usize,
    elapsed_secs: f64,
    rows_per_sec: f64,
    mb_per_sec: f64,
}

impl BenchmarkResult {
    fn new(
        mode: &'static str,
        row_count: u64,
        data_size_bytes: usize,
        elapsed: std::time::Duration,
    ) -> Self {
        let elapsed_secs = elapsed.as_secs_f64();
        let rows_per_sec = row_count as f64 / elapsed_secs;
        let mb_per_sec = (data_size_bytes as f64 / 1_000_000.0) / elapsed_secs;

        BenchmarkResult {
            mode,
            row_count,
            data_size_bytes,
            elapsed_secs,
            rows_per_sec,
            mb_per_sec,
        }
    }
}

// ============================================================================
// Output Formatting
// ============================================================================

fn format_count(count: u64) -> String {
    if count >= 1_000_000_000 {
        format!("{:.1}B", count as f64 / 1_000_000_000.0)
    } else if count >= 1_000_000 {
        format!("{:.1}M", count as f64 / 1_000_000.0)
    } else if count >= 1_000 {
        format!("{:.1}K", count as f64 / 1_000.0)
    } else {
        format!("{count}")
    }
}

fn format_size(bytes: usize) -> String {
    if bytes >= 1_000_000_000 {
        format!("{:.2} GB", bytes as f64 / 1_000_000_000.0)
    } else if bytes >= 1_000_000 {
        format!("{:.2} MB", bytes as f64 / 1_000_000.0)
    } else if bytes >= 1_000 {
        format!("{:.2} KB", bytes as f64 / 1_000.0)
    } else {
        format!("{bytes} B")
    }
}

fn print_header(title: &str) {
    println!();
    println!("╔══════════════════════════════════════════════════════════════════════════════╗");
    println!("║ {title:^76} ║");
    println!("╚══════════════════════════════════════════════════════════════════════════════╝");
    println!();
}

fn print_section(title: &str) {
    println!();
    println!("┌──────────────────────────────────────────────────────────────────────────────┐");
    println!("{}{}", title, " ".repeat(76 - title.len()));
    println!("└──────────────────────────────────────────────────────────────────────────────┘");
}

fn print_table_header() {
    println!();
    println!("┌────────────┬────────────┬────────────┬────────────┬──────────────┬──────────────┐");
    println!(
        "│ {:>10} │ {:>10} │ {:>10} │ {:>10} │ {:>12} │ {:>12} │",
        "Mode", "Rows", "Data Size", "Time (s)", "Rows/sec", "MB/sec"
    );
    println!("├────────────┼────────────┼────────────┼────────────┼──────────────┼──────────────┤");
}

fn print_table_row(result: &BenchmarkResult) {
    println!(
        "│ {:>10} │ {:>10} │ {:>10} │ {:>10.2} │ {:>12} │ {:>12.2} │",
        result.mode,
        format_count(result.row_count),
        format_size(result.data_size_bytes),
        result.elapsed_secs,
        format_count(result.rows_per_sec as u64),
        result.mb_per_sec
    );
}

fn print_table_footer() {
    println!("└────────────┴────────────┴────────────┴────────────┴──────────────┴──────────────┘");
}

fn print_error_row(mode: &str, rows: u64, error: &str) {
    println!(
        "│ {:>10} │ {:>10} │ {:^44} │",
        mode,
        format_count(rows),
        format!("ERROR: {}", &error[..error.len().min(38)])
    );
}

// ============================================================================
// Benchmark Execution
// ============================================================================

fn run_benchmark(
    conn: &mut GrpcConnection,
    query: &str,
    row_count: u64,
    mode: &'static str,
) -> Result<BenchmarkResult> {
    let start = Instant::now();
    let result = conn.execute_query(query)?;
    let elapsed = start.elapsed();
    let data_size = result.arrow_data().len();

    Ok(BenchmarkResult::new(mode, row_count, data_size, elapsed))
}

/// Simple query with 3 columns (for speed)
fn simple_query(row_count: u64) -> String {
    format!(
        "SELECT i AS id, i % 10000 AS bucket, random() AS rand FROM generate_series(1, {row_count}) AS s(i)"
    )
}

/// Complex query with 10+ mixed columns
fn complex_query(row_count: u64) -> String {
    format!(
        r"SELECT
            i AS id,
            i % 10000 AS bucket,
            i * 2 AS doubled,
            random() AS rand_float,
            CASE WHEN i % 2 = 0 THEN true ELSE false END AS is_even,
            'row_' || CAST(i AS TEXT) AS label,
            i % 100 AS small_int,
            i * 0.001 AS scaled,
            CAST(i AS TEXT) || '_suffix' AS text_col,
            i % 1000000 AS medium_int,
            CASE WHEN i % 3 = 0 THEN 'A' WHEN i % 3 = 1 THEN 'B' ELSE 'C' END AS category,
            i / 1000 AS quotient
        FROM generate_series(1, {row_count}) AS s(i)"
    )
}

// ============================================================================
// Main Benchmark Functions
// ============================================================================

/// Comprehensive benchmark comparing all transfer modes at multiple scale points
fn benchmark_all_modes_all_scales() -> Result<()> {
    print_header("gRPC TRANSFER MODE BENCHMARK");

    println!("This benchmark compares SYNC, ADAPTIVE, and ASYNC transfer modes");
    println!("at multiple data scales. Each test uses a simple 3-column query.");
    println!();
    println!(
        "Scale points: {:?}",
        SCALE_POINTS
            .iter()
            .map(|&x| format_count(x))
            .collect::<Vec<_>>()
    );
    println!();

    // Start Hyper with gRPC
    let mut params = Parameters::new();
    params.set("log_dir", "test_results");
    params.set_listen_mode(ListenMode::Grpc { port: 0 });
    // Note: grpc_threads is automatically set by HyperProcess
    let hyper = HyperProcess::new(None, Some(&params))?;
    let grpc_url = hyper.grpc_url().unwrap();

    let mut all_results: Vec<BenchmarkResult> = Vec::new();

    // Run each scale point
    for &row_count in SCALE_POINTS {
        print_section(&format!("Scale: {} rows", format_count(row_count)));
        print_table_header();

        let query = simple_query(row_count);

        for &(mode, mode_name) in TRANSFER_MODES {
            let config = GrpcConfig::new(&grpc_url).transfer_mode(mode);
            match GrpcConnection::connect_with_config(config) {
                Ok(mut conn) => match run_benchmark(&mut conn, &query, row_count, mode_name) {
                    Ok(result) => {
                        print_table_row(&result);
                        all_results.push(result);
                    }
                    Err(e) => {
                        print_error_row(mode_name, row_count, &e.to_string());
                    }
                },
                Err(e) => {
                    print_error_row(mode_name, row_count, &e.to_string());
                }
            }
        }

        print_table_footer();
    }

    // Print summary
    print_section("Summary: Best Results by Mode");
    println!();

    for &(_, mode_name) in TRANSFER_MODES {
        let mode_results: Vec<_> = all_results.iter().filter(|r| r.mode == mode_name).collect();
        if mode_results.is_empty() {
            continue;
        }

        let max_throughput = mode_results
            .iter()
            .map(|r| r.rows_per_sec)
            .fold(0.0_f64, f64::max);
        let max_mb_sec = mode_results
            .iter()
            .map(|r| r.mb_per_sec)
            .fold(0.0_f64, f64::max);

        println!(
            "  {:<10}: Peak {}/sec rows, {:.2} MB/sec",
            mode_name,
            format_count(max_throughput as u64),
            max_mb_sec
        );
    }

    Ok(())
}

// ============================================================================
// Complex Column Benchmark (100M rows, 12 columns)
// ============================================================================

/// Benchmark with 100M rows and 12 mixed-type columns
fn benchmark_100m_complex() -> Result<()> {
    print_header("100M ROWS × 12 COLUMNS BENCHMARK");

    println!("This benchmark tests the maximum throughput with a realistic workload:");
    println!("  • 100,000,000 rows");
    println!("  • 12 columns of mixed types (INT, FLOAT, BOOL, TEXT, etc.)");
    println!();
    println!("Column schema:");
    println!("  1. id          BIGINT     - Row identifier");
    println!("  2. bucket      INT        - Modulo bucket (0-9999)");
    println!("  3. doubled     BIGINT     - Doubled value");
    println!("  4. rand_float  DOUBLE     - Random float");
    println!("  5. is_even     BOOL       - Even/odd flag");
    println!("  6. label       TEXT       - String with row number");
    println!("  7. small_int   INT        - Small modulo (0-99)");
    println!("  8. scaled      DOUBLE     - Scaled value");
    println!("  9. text_col    TEXT       - Another text column");
    println!(" 10. medium_int  INT        - Medium modulo");
    println!(" 11. category    TEXT       - A/B/C category");
    println!(" 12. quotient    BIGINT     - Divided value");
    println!();

    // Start Hyper with gRPC
    let mut params = Parameters::new();
    params.set("log_dir", "test_results");
    params.set_listen_mode(ListenMode::Grpc { port: 0 });
    // Note: grpc_threads is automatically set by HyperProcess
    let hyper = HyperProcess::new(None, Some(&params))?;
    let grpc_url = hyper.grpc_url().unwrap();

    let row_count: u64 = 100_000_000;
    let query = complex_query(row_count);

    // Run benchmarks first, collect results
    println!("Running benchmarks (this may take 1-2 minutes)...");
    let mut results: Vec<std::result::Result<BenchmarkResult, String>> = Vec::new();

    for &(mode, mode_name) in TRANSFER_MODES {
        print!("  {mode_name}... ");
        std::io::Write::flush(&mut std::io::stdout()).ok();

        let config = GrpcConfig::new(&grpc_url).transfer_mode(mode);
        match GrpcConnection::connect_with_config(config) {
            Ok(mut conn) => match run_benchmark(&mut conn, &query, row_count, mode_name) {
                Ok(result) => {
                    println!("done ({:.2}s)", result.elapsed_secs);
                    results.push(Ok(result));
                }
                Err(e) => {
                    println!("failed");
                    results.push(Err(format!("{mode_name}: {e}")));
                }
            },
            Err(e) => {
                println!("connection failed");
                results.push(Err(format!("{mode_name}: connection failed - {e}")));
            }
        }
    }

    // Now print the results table
    println!();
    print_table_header();

    for result in &results {
        match result {
            Ok(r) => print_table_row(r),
            Err(msg) => println!("│ {msg:^76} │"),
        }
    }

    print_table_footer();

    println!();
    println!("Note: ASYNC mode typically performs best with large datasets");
    println!("      because it allows Hyper to pipeline result preparation.");

    Ok(())
}

// ============================================================================
// Quick Sanity Check
// ============================================================================

/// Quick benchmark for CI/CD - tests 1M rows only
fn benchmark_quick() -> Result<()> {
    print_header("QUICK BENCHMARK (1M rows)");

    // Start Hyper with gRPC
    let mut params = Parameters::new();
    params.set("log_dir", "test_results");
    params.set_listen_mode(ListenMode::Grpc { port: 0 });
    // Note: grpc_threads is automatically set by HyperProcess
    let hyper = HyperProcess::new(None, Some(&params))?;
    let grpc_url = hyper.grpc_url().unwrap();

    let row_count: u64 = 10_000;
    let query = simple_query(row_count);

    print_table_header();

    for &(mode, mode_name) in TRANSFER_MODES {
        let config = GrpcConfig::new(&grpc_url).transfer_mode(mode);
        match GrpcConnection::connect_with_config(config) {
            Ok(mut conn) => match run_benchmark(&mut conn, &query, row_count, mode_name) {
                Ok(result) => print_table_row(&result),
                Err(e) => print_error_row(mode_name, row_count, &e.to_string()),
            },
            Err(e) => print_error_row(mode_name, row_count, &e.to_string()),
        }
    }

    print_table_footer();

    Ok(())
}

// ============================================================================
// Main Entry Point
// ============================================================================

fn main() -> Result<()> {
    benchmark_quick()?;
    benchmark_all_modes_all_scales()?;
    benchmark_100m_complex()?;
    Ok(())
}