# Zipf Distribution for Load Testing
## Overview
[Zipf distribution](https://en.wikipedia.org/wiki/Zipf%27s_law) models real-world access patterns where a small number of items account for the majority of requests. This pattern appears frequently in:
- Web cache access patterns (hot data)
- Database query frequency distributions
- File system access patterns
- Social media content popularity
Using zipf-distributed workloads in benchmarking creates realistic test scenarios that closely mirror production traffic patterns.
## Mathematical Foundation
The Zipf distribution follows a power law, $P(x) = C \cdot x^{-s}$, where $s > 0$ is the
shape parameter (higher values create steeper distributions) and $C$ is a normalization
constant. Values are drawn using
[inverse transform sampling](https://en.wikipedia.org/wiki/Inverse_transform_sampling).
See [formulas.md](formulas.md) for the full derivation — the
normalization constant, the inverse-transform step, and the $s = 1$ special case.
## Usage Examples
### Basic Zipf Generation
```rust
use load::zipf::Zipf;
// Create a zipf generator for range [1, 100] with shape parameter 1.1
let zipf = Zipf::new(1.0..100.0, 1.1).unwrap();
// Generate zipf-distributed values
let value1 = zipf.sample(0.1); // Low u → high rank (popular items)
let value2 = zipf.sample(0.9); // High u → low rank (unpopular items)
// Assert formatted values (4 decimal places)
assert_eq!(format!("{:.4}", value1), "1.4565");
assert_eq!(format!("{:.4}", value2), "56.6416");
```
### Load Testing Scenario
```rust
use load::zipf::Zipf;
// Generate 10,000 accesses to a 1,000-item cache
// with zipf distribution (s=1.2 for realistic web traffic)
let cache_accesses: Vec<usize> = Zipf::indices_access(1..1001, 1.2).unwrap()
.take(10) // Number of accesses
.collect();
// Assert exact sequence with deterministic seed
assert_eq!(cache_accesses, vec![2, 13, 6, 1, 223, 546, 4, 121, 2, 2]);
```
### Custom RNG for Reproducible Tests
```rust
use load::zipf::{Zipf, ZipfIterator};
// Create reproducible workload for benchmarking
let zipf = Zipf::new(1.0..100.0, 1.5).unwrap();
let mut iter = ZipfIterator::with_seed(zipf, 42);
// Assert exact sequence with seed 42
assert_eq!(workload, vec![3, 3, 5, 2, 1]);
```
### Streaming Processing (Memory Efficient)
```rust
use load::zipf::Zipf;
use std::collections::HashMap;
// Process large workloads without allocating all indices upfront
let mut hit_counts = HashMap::new();
for index in Zipf::indices_access(1..11, 1.2).unwrap().take(100) {
*hit_counts.entry(index).or_insert(0) += 1;
// Process each access immediately (e.g., cache lookup, database query)
// This approach uses constant memory regardless of workload size
}
- **Range [a, b]**: Defines the output range. For array indices, use `[1, array_length+1]`
- **Shape parameter s**: Controls distribution steepness
- `s = 1.0`: Classical Zipf (web requests, word frequency)
- `s = 1.2`: Typical web cache patterns
- `s = 2.0`: Very steep distribution (80/20 rule becomes 95/5)
- **Array length**: Size of the dataset being accessed
- **Iterator control**: Use `.take(n)` to limit the number of accesses, or process indefinitely