Skip to main content

bench/
bench.rs

1use std::time::Instant;
2
3use zeldhash_miner_gpu::{
4    calibrate_batch_size, dispatch_mining_batch, GpuContext, GpuError, MiningBatch,
5};
6
7const START_NONCE: u64 = 1 << 32; // keep a stable nonce byte-length across batch sizes
8
9struct BenchSample {
10    elapsed_ms: f64,
11    hashes_per_sec: f64,
12}
13
14async fn bench_batch(ctx: &GpuContext, batch_size: u32) -> Result<BenchSample, GpuError> {
15    let batch = MiningBatch {
16        tx_prefix: b"bench-prefix",
17        tx_suffix: b"bench-suffix",
18        start_nonce: START_NONCE,
19        batch_size,
20        target_zeros: 64, // effectively impossible target; keeps the kernel busy
21        use_cbor_nonce: false,
22    };
23
24    let start = Instant::now();
25    // Ignore results; we only care about throughput.
26    let _ = dispatch_mining_batch(ctx, &batch).await?;
27    let elapsed = start.elapsed().as_secs_f64().max(1e-9);
28
29    Ok(BenchSample {
30        elapsed_ms: elapsed * 1_000.0,
31        hashes_per_sec: batch_size as f64 / elapsed,
32    })
33}
34
35async fn memory_pressure_check(ctx: &GpuContext, batch_size: u32) -> Result<(), GpuError> {
36    // Use large buffers to validate allocation/dispatch under heavier pressure.
37    let blob = vec![0u8; 512 * 1024];
38    for i in 0..3 {
39        let batch = MiningBatch {
40            tx_prefix: &blob,
41            tx_suffix: &blob,
42            start_nonce: START_NONCE + (i as u64) * batch_size as u64,
43            batch_size,
44            target_zeros: 64,
45            use_cbor_nonce: false,
46        };
47        let _ = dispatch_mining_batch(ctx, &batch).await?;
48    }
49    Ok(())
50}
51
52fn main() -> Result<(), Box<dyn std::error::Error>> {
53    let ctx = pollster::block_on(GpuContext::init())?;
54    let summary = ctx.adapter_summary();
55    println!(
56        "Adapter: {} | backend={} | type={}",
57        summary.name, summary.backend, summary.device_type
58    );
59
60    let calibrated = pollster::block_on(calibrate_batch_size(&ctx))?;
61    println!("Calibrated batch size: {}", calibrated);
62
63    for size in [10_000u32, 100_000, 1_000_000] {
64        let sample = pollster::block_on(bench_batch(&ctx, size))?;
65        println!(
66            "Batch {:>7}: {:>7.2} ms | {:>8.2} MH/s",
67            size,
68            sample.elapsed_ms,
69            sample.hashes_per_sec / 1_000_000.0
70        );
71    }
72
73    let pressure_size = calibrated.min(1_000_000);
74    pollster::block_on(memory_pressure_check(&ctx, pressure_size))?;
75    println!(
76        "Memory pressure: 3x batches (size {}) with 512 KiB prefix/suffix succeeded",
77        pressure_size
78    );
79
80    Ok(())
81}