#!/bin/bash

# Measure throughput for Wasot random file generation
# Usage: ./measure_throughput.sh [EVENT_COUNT]

N=${1:-1000000}
OUTPUT_FILE="/tmp/wasot_throughput_test.wasot"

echo "Measuring throughput for $N events..."

# Build in release mode first
cargo build --release --bin wasot

# Clean up any existing file
rm -f "$OUTPUT_FILE"

# Run with time measurement
START_TIME=$(date +%s.%N)
cargo run --release --bin wasot -- random -n "$N" -o "$OUTPUT_FILE"
END_TIME=$(date +%s.%N)

# Calculate duration
DURATION=$(echo "$END_TIME - $START_TIME" | bc)

# Get file size
if [ -f "$OUTPUT_FILE" ]; then
    FILE_SIZE_BYTES=$(stat -c%s "$OUTPUT_FILE" 2>/dev/null || stat -f%z "$OUTPUT_FILE" 2>/dev/null)
    FILE_SIZE_MB=$(echo "scale=2; $FILE_SIZE_BYTES / 1024 / 1024" | bc)
    FILE_SIZE_GB=$(echo "scale=3; $FILE_SIZE_BYTES / 1024 / 1024 / 1024" | bc)
    
    # Calculate throughput
    THROUGHPUT_MB_S=$(echo "scale=2; $FILE_SIZE_MB / $DURATION" | bc)
    THROUGHPUT_GB_S=$(echo "scale=3; $FILE_SIZE_GB / $DURATION" | bc)
    EVENTS_PER_SEC=$(echo "scale=0; $N / $DURATION" | bc)
    
    echo "Results:"
    echo "  Events: $N"
    echo "  Duration: ${DURATION}s"
    echo "  File size: ${FILE_SIZE_MB}MB (${FILE_SIZE_GB}GB)"
    echo "  Throughput: ${THROUGHPUT_MB_S}MB/s (${THROUGHPUT_GB_S}GB/s)"
    echo "  Events/sec: ${EVENTS_PER_SEC}"
    
    # Clean up
    rm -f "$OUTPUT_FILE"
else
    echo "Error: Output file not created"
fi