|
| 1 | +use actix_web::web; |
| 2 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 3 | +use rand::{Rng, SeedableRng}; |
| 4 | +use rand_chacha::ChaCha8Rng; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use json_gen_actix::{ |
| 8 | + processing::{ |
| 9 | + write_location_csv_simd, write_location_json_simd, BusinessLocation, DataPools, |
| 10 | + JsonPatterns, OutputFormat, StreamGenerator, |
| 11 | + }, |
| 12 | + util::ProgressInfo, |
| 13 | +}; |
| 14 | + |
| 15 | +fn bench_formats(c: &mut Criterion) { |
| 16 | + let mut group = c.benchmark_group("data_generation"); |
| 17 | + let data_pools = web::Data::new(Arc::new(DataPools::new())); |
| 18 | + let rng = ChaCha8Rng::seed_from_u64(42); |
| 19 | + |
| 20 | + for size in [64, 256, 1024].iter() { |
| 21 | + group.bench_with_input(BenchmarkId::new("json_pretty", size), size, |b, &size| { |
| 22 | + b.iter(|| { |
| 23 | + let mut generator = StreamGenerator::new( |
| 24 | + 0, |
| 25 | + rng.clone(), |
| 26 | + data_pools.clone(), |
| 27 | + true, |
| 28 | + OutputFormat::JSON, |
| 29 | + true, |
| 30 | + Arc::new(ProgressInfo::new(1.0)), |
| 31 | + size * 1024, |
| 32 | + ); |
| 33 | + generator.generate_chunk() |
| 34 | + }) |
| 35 | + }); |
| 36 | + |
| 37 | + group.bench_with_input(BenchmarkId::new("json_compact", size), size, |b, &size| { |
| 38 | + b.iter(|| { |
| 39 | + let mut generator = StreamGenerator::new( |
| 40 | + 0, |
| 41 | + rng.clone(), |
| 42 | + data_pools.clone(), |
| 43 | + false, |
| 44 | + OutputFormat::JSON, |
| 45 | + true, |
| 46 | + Arc::new(ProgressInfo::new(1.0)), |
| 47 | + size * 1024, |
| 48 | + ); |
| 49 | + generator.generate_chunk() |
| 50 | + }) |
| 51 | + }); |
| 52 | + |
| 53 | + group.bench_with_input(BenchmarkId::new("csv", size), size, |b, &size| { |
| 54 | + b.iter(|| { |
| 55 | + let mut generator = StreamGenerator::new( |
| 56 | + 0, |
| 57 | + rng.clone(), |
| 58 | + data_pools.clone(), |
| 59 | + false, |
| 60 | + OutputFormat::CSV, |
| 61 | + true, |
| 62 | + Arc::new(ProgressInfo::new(1.0)), |
| 63 | + size * 1024, |
| 64 | + ); |
| 65 | + generator.generate_chunk() |
| 66 | + }) |
| 67 | + }); |
| 68 | + } |
| 69 | + group.finish(); |
| 70 | +} |
| 71 | + |
| 72 | +fn bench_simd_operations(c: &mut Criterion) { |
| 73 | + let mut group = c.benchmark_group("simd_ops"); |
| 74 | + let location = BusinessLocation { |
| 75 | + id: 1, |
| 76 | + name: "Test Company".into(), |
| 77 | + industry: "Technology".into(), |
| 78 | + revenue: 1000000.0, |
| 79 | + employees: 100, |
| 80 | + city: "Test City".into(), |
| 81 | + state: "Test State".into(), |
| 82 | + country: "Test Country".into(), |
| 83 | + }; |
| 84 | + |
| 85 | + group.bench_function("json_simd_write", |b| { |
| 86 | + b.iter(|| { |
| 87 | + let mut output = Vec::with_capacity(1024); |
| 88 | + write_location_json_simd(&location, &mut output, true, &JsonPatterns::new(), true); |
| 89 | + }) |
| 90 | + }); |
| 91 | + |
| 92 | + group.bench_function("csv_simd_write", |b| { |
| 93 | + b.iter(|| { |
| 94 | + let mut output = Vec::with_capacity(1024); |
| 95 | + write_location_csv_simd(&location, &mut output); |
| 96 | + }) |
| 97 | + }); |
| 98 | + group.finish(); |
| 99 | +} |
| 100 | + |
| 101 | +fn bench_data_generation(c: &mut Criterion) { |
| 102 | + let mut group = c.benchmark_group("data_gen"); |
| 103 | + let data_pools = web::Data::new(Arc::new(DataPools::new())); |
| 104 | + let rng = ChaCha8Rng::seed_from_u64(42); |
| 105 | + |
| 106 | + group.bench_function("location_gen", |b| { |
| 107 | + b.iter(|| BusinessLocation { |
| 108 | + id: 1, |
| 109 | + name: data_pools.names[0].clone(), |
| 110 | + industry: data_pools.industries[0].clone(), |
| 111 | + revenue: rng.clone().gen_range(100000.0..100000000.0), |
| 112 | + employees: rng.clone().gen_range(10..10000), |
| 113 | + city: data_pools.cities[0].clone(), |
| 114 | + state: data_pools.states[0].clone(), |
| 115 | + country: data_pools.countries[0].clone(), |
| 116 | + }) |
| 117 | + }); |
| 118 | + group.finish(); |
| 119 | +} |
| 120 | + |
| 121 | +criterion_group!( |
| 122 | + benches, |
| 123 | + bench_formats, |
| 124 | + bench_simd_operations, |
| 125 | + bench_data_generation |
| 126 | +); |
| 127 | +criterion_main!(benches); |
0 commit comments