# Performance

Benchmarks, optimization tips, and tuning guide for SRX.

---

## Overview

SRX is designed for high performance on modern hardware. This guide covers:

- Benchmark results
- Performance tuning
- Optimization strategies
- Hardware considerations

---

## Benchmarks

### Test Environment

| Component | Specification |
|-----------|---------------|
| CPU | AMD Ryzen 9 5950X (16 cores, 32 threads) |
| RAM | 64 GB DDR4-3200 |
| Storage | NVMe SSD (Samsung 980 Pro) |
| OS | Linux 6.1, Rust 1.75 |
| SRX | v1.0.0 (release build) |

### Compression Performance

#### Zstd Compression

| Level | Compression | Decompression | Ratio |
|-------|-------------|---------------|-------|
| 1 | 520 MB/s | 1100 MB/s | 42% |
| 3 (default) | 400 MB/s | 1000 MB/s | 38% |
| 9 | 180 MB/s | 1000 MB/s | 32% |
| 19 | 45 MB/s | 1000 MB/s | 28% |
| 22 | 12 MB/s | 1000 MB/s | 26% |

#### LZ4 Compression

| Level | Compression | Decompression | Ratio |
|-------|-------------|---------------|-------|
| Default | 550 MB/s | 2200 MB/s | 48% |
| Fast | 680 MB/s | 2400 MB/s | 52% |
| High | 280 MB/s | 2100 MB/s | 42% |

### Encryption Performance

| Operation | Speed | Notes |
|-----------|-------|-------|
| XChaCha20-Poly1305 | 1.2 GB/s | Single-threaded |
| Argon2id (default) | ~100ms | One-time cost |
| BLAKE3 | 1.5 GB/s | Multi-threaded |

### End-to-End Performance

#### Text Files (JSON, XML, Logs)

| File Size | Pack (Zstd-3) | Unpack | Ratio |
|-----------|---------------|--------|-------|
| 1 MB | 3 ms | 1 ms | 25% |
| 10 MB | 25 ms | 10 ms | 22% |
| 100 MB | 250 ms | 100 ms | 20% |
| 1 GB | 2.5 s | 1 s | 18% |

#### Binary Files (Executables, Databases)

| File Size | Pack (Zstd-3) | Unpack | Ratio |
|-----------|---------------|--------|-------|
| 1 MB | 4 ms | 2 ms | 45% |
| 10 MB | 40 ms | 20 ms | 42% |
| 100 MB | 400 ms | 200 ms | 40% |
| 1 GB | 4 s | 2 s | 38% |

#### Pre-compressed Files (Videos, Images)

| File Size | Pack (none) | Unpack | Ratio |
|-----------|-------------|--------|-------|
| 1 MB | 1 ms | 1 ms | 100% |
| 10 MB | 10 ms | 10 ms | 100% |
| 100 MB | 100 ms | 100 ms | 100% |
| 1 GB | 1 s | 1 s | 100% |

---

## Performance Tuning

### Chunk Size

Chunk size affects both performance and compression ratio.

| Chunk Size | Compression Ratio | Memory Usage | Parallelism |
|------------|-------------------|--------------|-------------|
| 64 KiB | Lower | Low | High |
| 256 KiB (default) | Balanced | Medium | Good |
| 1 MiB | Higher | High | Lower |
| 4 MiB | Highest | Very High | Low |

#### When to Use Different Chunk Sizes

```bash
# Small chunks: Many small files, limited RAM
srx pack data/ data.srx -p "key" -c 65536

# Default: General use
srx pack data/ data.srx -p "key"

# Large chunks: Large files, maximum compression
srx pack large_file.bin large_file.srx -p "key" -c 1048576
```

### Compression Level

Choose compression level based on your use case:

```bash
# Fast: Real-time applications, backups
srx pack data/ data.srx -p "key" -l 1

# Default: General use
srx pack data/ data.srx -p "key" -l 3

# High: Archival, storage optimization
srx pack data/ data.srx -p "key" -l 9

# Maximum: Long-term archival
srx pack data/ data.srx -p "key" -l 19
```

### Compression Algorithm

| Algorithm | Best For | Speed | Ratio |
|-----------|----------|-------|-------|
| `zstd` | General use | Fast | Good |
| `lz4` | Real-time, speed-critical | Very Fast | Moderate |
| `none` | Pre-compressed data | Instant | 1:1 |

```bash
# Speed-critical
srx pack logs/ logs.srx -p "key" -a lz4

# Already compressed (videos, images, zip files)
srx pack video.mp4 video.srx -p "key" -a none
```

---

## Memory Usage

### Pack Operation

| Phase | Memory Usage |
|-------|--------------|
| Argon2 key derivation | 64 MiB (configurable) |
| Chunk buffer | 2 × chunk size |
| Compression | ~1-4 MiB per thread |
| Encryption | ~1 MiB |

**Total**: ~70-100 MiB (default settings)

### Unpack Operation

| Phase | Memory Usage |
|-------|--------------|
| Argon2 key derivation | 64 MiB |
| Chunk buffer | 2 × chunk size |
| Decompression | ~1-4 MiB |
| Decryption | ~1 MiB |

**Total**: ~70-90 MiB (default settings)

### Reducing Memory Usage

```bash
# Reduce Argon2 memory
export SRX_MEMORY_LIMIT=32

# Use smaller chunks
srx pack file.txt file.srx -p "key" -c 65536
```

---

## Parallelism

### Multi-threaded Operations

SRX can process multiple chunks in parallel:

| Threads | Speedup (16-core CPU) |
|---------|----------------------|
| 1 | 1.0× |
| 2 | 1.9× |
| 4 | 3.5× |
| 8 | 6.5× |
| 16 | 11× |

### Controlling Parallelism

```bash
# Set via environment variable
export RAYON_NUM_THREADS=8
srx pack large_file.bin large_file.srx -p "key"
```

### Library Usage

```rust
use srx::Packer;

// Parallelism is automatic for multi-chunk archives
let packer = Packer::new("password".to_string())
    .chunk_size(256 * 1024);  // Smaller chunks = more parallelism
```

---

## I/O Optimization

### Storage Considerations

| Storage Type | Read Speed | Write Speed | Recommendation |
|--------------|------------|-------------|----------------|
| NVMe SSD | 3-7 GB/s | 2-5 GB/s | Best performance |
| SATA SSD | 500 MB/s | 450 MB/s | Good performance |
| HDD | 100-200 MB/s | 100-150 MB/s | Use larger chunks |
| Network (1Gbps) | 125 MB/s | 125 MB/s | Use compression |
| Network (10Gbps) | 1.25 GB/s | 1.25 GB/s | Good performance |

### Optimizing for HDD

```bash
# Larger chunks reduce seek overhead
srx pack large_file.bin large_file.srx -p "key" -c 1048576
```

### Optimizing for Network Storage

```bash
# Higher compression reduces bandwidth
srx pack data/ data.srx -p "key" -l 9
```

---

## Benchmarking

### Running Benchmarks

```bash
# Run all benchmarks
cargo bench

# Run specific benchmark
cargo bench -- compression

# Run with custom input
cargo bench -- --test data/large_file.bin
```

### Custom Benchmark

```rust
use std::time::Instant;
use srx::{Packer, Unpacker};

fn benchmark_pack(path: &str, password: &str) {
    let start = Instant::now();
    Packer::new(password.to_string()).pack(path, "benchmark.srx").unwrap();
    let duration = start.elapsed();
    
    let size = std::fs::metadata(path).unwrap().len();
    let mbps = (size as f64 / duration.as_secs_f64()) / 1_000_000.0;
    
    println!("Pack: {:.2} MB/s", mbps);
}
```

---

## Performance Tips

### General Tips

1. **Use release builds** - Debug builds are 10-100× slower
   ```bash
   cargo build --release
   ```

2. **Match compression to data type**
   - Text/logs: Zstd level 3-9
   - Binaries: Zstd level 3
   - Pre-compressed: No compression

3. **Use appropriate chunk sizes**
   - Small files: 64-256 KiB
   - Large files: 256 KiB-1 MiB
   - Streaming: 64 KiB

4. **Leverage parallelism**
   - More chunks = more parallelism
   - Use all CPU cores

### For Large Files (>1 GB)

```bash
# Larger chunks, higher compression
srx pack large.tar large.srx -p "key" -l 9 -c 1048576
```

### For Many Small Files

```bash
# First tar, then pack
tar -cf files.tar files/
srx pack files.tar files.srx -p "key" -l 3
```

### For Backups

```bash
# Fast compression for frequent backups
srx pack backup/ backup.srx -p "key" -a lz4

# High compression for long-term storage
srx pack backup/ backup.srx -p "key" -l 19
```

---

## Performance Comparison

### SRX vs Other Tools

| Tool | Pack Speed | Unpack Speed | Ratio | Encryption |
|------|------------|--------------|-------|------------|
| SRX (zstd-3) | 400 MB/s | 1000 MB/s | 38% | ✅ Always |
| zip (deflate) | 150 MB/s | 400 MB/s | 40% | ⚠️ Optional |
| 7z (lzma2) | 50 MB/s | 200 MB/s | 30% | ⚠️ Optional |
| gzip | 100 MB/s | 300 MB/s | 35% | ❌ No |
| age | N/A | N/A | 100% | ✅ Always |
| gpg | 200 MB/s | 200 MB/s | 100% | ✅ Always |

*Note: SRX always encrypts; other tools may have encryption overhead separately.*

---

## Troubleshooting Performance

### Slow Compression

**Symptoms**: Compression takes much longer than expected

**Solutions**:
- Check compression level: `-l 3` is default
- Use LZ4 for speed: `-a lz4`
- Check disk I/O: `iostat -x 1`

### Slow Decompression

**Symptoms**: Extraction is slow

**Solutions**:
- Check if archive uses high compression level
- Verify disk write speed
- Check available memory

### High Memory Usage

**Symptoms**: Out of memory errors

**Solutions**:
- Reduce chunk size: `-c 65536`
- Reduce Argon2 memory: `export SRX_MEMORY_LIMIT=32`
- Use streaming mode for large files

---

## See Also

- [CLI Reference](CLI-Reference) - Command options
- [Library API](Library-API) - Programmatic usage
- [File Format](File-Format) - Format specification