network-protocol 1.2.1

Secure, high-performance protocol core with backpressure control, structured logging, timeout handling, TLS support, and comprehensive benchmarking for robust Rust networked applications and services.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# Performance Tuning Guide


This guide provides recommendations for optimizing the performance of network-protocol in production deployments.

## Table of Contents


- [Compression Configuration]#compression-configuration
- [Serialization Format Selection]#serialization-format-selection
- [Build Optimization]#build-optimization
- [Runtime Configuration]#runtime-configuration
- [Platform-Specific Optimizations]#platform-specific-optimizations
- [Benchmarking Methodology]#benchmarking-methodology
- [Monitoring and Profiling]#monitoring-and-profiling

---

## Compression Configuration


### Algorithm Selection


The library supports two compression algorithms with different performance characteristics:

#### LZ4 (Default - Recommended)

- **Speed**: ~500 MB/s compression, ~2 GB/s decompression
- **Ratio**: Moderate (2-3x typical)
- **Use case**: Low-latency applications, real-time communication
- **CPU overhead**: Minimal

```rust
use network_protocol::utils::compression::{compress_lz4, decompress_lz4};

let data = b"Your data here...";
let compressed = compress_lz4(data)?;
let decompressed = decompress_lz4(&compressed)?;
```

#### Zstandard (High Compression)

- **Speed**: ~100-400 MB/s compression, ~800 MB/s decompression
- **Ratio**: High (3-5x typical, tunable)
- **Use case**: Bandwidth-constrained scenarios, archival
- **CPU overhead**: Moderate to high

```rust
use network_protocol::utils::compression::{compress_zstd, decompress_zstd};

let data = b"Your data here...";
let compressed = compress_zstd(data)?;
let decompressed = decompress_zstd(&compressed)?;
```

### Automatic Compression Threshold


The `maybe_compress()` function automatically compresses data only when beneficial:

```rust
use network_protocol::utils::compression::{maybe_compress, maybe_decompress};

// Only compresses if data is larger than 128 bytes
let (compressed_data, was_compressed) = maybe_compress(data);

// Automatically detects if decompression is needed
let original = maybe_decompress(&compressed_data, was_compressed)?;
```

**Threshold Recommendations:**
- **Default (128 bytes)**: Good for mixed workloads
- **Larger (512-1024 bytes)**: For low-latency requirements
- **Smaller (64 bytes)**: For bandwidth-constrained networks

---

## Serialization Format Selection


Choose the serialization format based on your requirements:

### Bincode (Default - Production)

```rust
use network_protocol::core::serialization::{MultiFormat, SerializationFormat};

let message = Message::Ping;
let bytes = message.serialize_format(SerializationFormat::Bincode)?;
```

**Performance**: ~100-200ns per message  
**Size**: Most compact binary format  
**Use case**: Production deployments, high-throughput scenarios

### JSON (Debugging & Interop)

```rust
let bytes = message.serialize_format(SerializationFormat::Json)?;
let json_str = std::str::from_utf8(&bytes)?;
println!("Debug: {}", json_str);
```

**Performance**: ~500-1000ns per message  
**Size**: 2-3x larger than bincode  
**Use case**: Debugging, web APIs, human-readable logs

### MessagePack (Cross-Language)

```rust
let bytes = message.serialize_format(SerializationFormat::MessagePack)?;
```

**Performance**: ~150-300ns per message  
**Size**: Compact, similar to bincode  
**Use case**: Cross-language interoperability, polyglot systems

**Recommendation**: Use Bincode for production, JSON for development/debugging.

---

## Build Optimization


### Release Profile


The default release profile is optimized for maximum performance:

```toml
[profile.release]
lto = true              # Link-Time Optimization
codegen-units = 1       # Single codegen unit for better optimization
opt-level = 3           # Maximum optimization
debug = false           # No debug symbols
strip = "symbols"       # Strip all symbols
```

**Build command:**
```bash
cargo build --release
```

**Expected improvements:**
- 20-30% faster than default release build
- Smaller binary size
- Longer compile time (acceptable for production builds)

### Platform-Specific Optimizations


#### Linux (x86_64)

```bash
RUSTFLAGS="-C target-cpu=native" cargo build --release
```

#### macOS (ARM64/M1/M2)

```bash
RUSTFLAGS="-C target-cpu=native" cargo build --release
```

#### Cross-compilation

```bash
# For specific CPU features

RUSTFLAGS="-C target-feature=+aes,+sse4.2" cargo build --release
```

### Benchmarking Profile


For accurate benchmarks:

```toml
[profile.bench]
lto = true
codegen-units = 1
opt-level = 3
debug = true            # Enable debug symbols for profiling
```

---

## Runtime Configuration


### Connection Pool Sizing


Configure based on expected concurrency:

```rust
use network_protocol::config::Config;

let config = Config {
    // For high-concurrency servers (1000+ connections)
    max_connections: 2000,
    connection_timeout_ms: 30000,
    
    // For low-latency applications
    max_connections: 100,
    connection_timeout_ms: 5000,
};
```

**Guidelines:**
- **Web servers**: max_connections = expected_concurrent_users × 1.5
- **Microservices**: max_connections = upstream_services × 10
- **Real-time systems**: Keep under 100 for predictable latency

### Channel Buffer Sizes


Tune backpressure channels based on workload:

```rust
// Default: 1000 messages
const DEFAULT_CHANNEL_CAPACITY: usize = 1000;

// High-throughput (trade memory for throughput)
const HIGH_THROUGHPUT_CAPACITY: usize = 10000;

// Low-latency (minimize queuing)
const LOW_LATENCY_CAPACITY: usize = 100;
```

**Monitoring**: Use `metrics::channel_depth()` to track buffer utilization.

### Timeout Configuration


Balance responsiveness with reliability:

```rust
use std::time::Duration;

// Aggressive (low-latency)
let timeouts = Timeouts {
    connect: Duration::from_millis(500),
    read: Duration::from_secs(1),
    write: Duration::from_secs(1),
};

// Conservative (unreliable networks)
let timeouts = Timeouts {
    connect: Duration::from_secs(5),
    read: Duration::from_secs(30),
    write: Duration::from_secs(30),
};
```

---

## Platform-Specific Optimizations


### Linux


#### TCP Tuning

```bash
# Increase TCP buffer sizes

sudo sysctl -w net.core.rmem_max=16777216
sudo sysctl -w net.core.wmem_max=16777216
sudo sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216"
sudo sysctl -w net.ipv4.tcp_wmem="4096 65536 16777216"

# Enable TCP fast open

sudo sysctl -w net.ipv4.tcp_fastopen=3
```

#### File Descriptor Limits

```bash
# Check current limit

ulimit -n

# Increase (temporary)

ulimit -n 65536

# Permanent: Edit /etc/security/limits.conf

* soft nofile 65536
* hard nofile 65536
```

### macOS

#### Increase File Descriptors
```bash
# Check current limit
launchctl limit maxfiles

# Increase (requires restart)
sudo launchctl limit maxfiles 65536 200000
```

#### Network Tuning

```bash
# Increase socket buffer sizes

sudo sysctl -w kern.ipc.maxsockbuf=16777216
sudo sysctl -w net.inet.tcp.sendspace=1048576
sudo sysctl -w net.inet.tcp.recvspace=1048576
```

### Windows


#### Named Pipes (Recommended)

The library uses native Windows Named Pipes by default for 30-40% better IPC performance:

```rust
// Automatic on Windows
use network_protocol::transport::local;

// Start server (uses Named Pipes on Windows)
local::start_server("my_app").await?;
```

#### TCP Fallback (if needed)

```toml
[features]
use-tcp-on-windows = []
```

```bash
cargo build --release --features use-tcp-on-windows
```

---

## Benchmarking Methodology


### Running Benchmarks


```bash
# Run all benchmarks

cargo bench

# Run specific benchmark

cargo bench --bench packet_bench

# Save baseline for comparison

cargo bench -- --save-baseline before-optimization

# Compare against baseline

cargo bench -- --baseline before-optimization
```

### Available Benchmarks


#### Packet Benchmark

Tests core packet encoding/decoding:
```bash
cargo bench --bench packet_bench
```

**Metrics:**
- Encode throughput: ~2-3 GB/s
- Decode throughput: ~1.5-2 GB/s

#### Compression Benchmark
Tests compression algorithms:
```bash
cargo bench --bench compression_bench

```

**Metrics:**
- LZ4: ~500 MB/s (compression), ~2 GB/s (decompression)
- Zstd: ~100-400 MB/s (compression), ~800 MB/s (decompression)

#### Message Benchmark

Tests message serialization:
```bash
cargo bench --bench message_bench
```

**Metrics:**
- Bincode: ~100-200ns per message
- JSON: ~500-1000ns per message
- MessagePack: ~150-300ns per message

### Interpreting Results

Look for:
- **Throughput**: Higher is better (MB/s or messages/sec)
- **Latency**: Lower is better (ns or µs)
- **Variance**: Lower is more consistent (check std dev)

**Example output:**
```
packet_encode          time:   [12.345 ns 12.567 ns 12.789 ns]
                       thrpt:  [2.34 GiB/s 2.38 GiB/s 2.42 GiB/s]
```

### Custom Benchmarks


Create application-specific benchmarks:

```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn my_benchmark(c: &mut Criterion) {
    c.bench_function("my_operation", |b| {
        b.iter(|| {
            // Your code here
            black_box(expensive_operation());
        });
    });
}

criterion_group!(benches, my_benchmark);
criterion_main!(benches);
```

---

## Monitoring and Profiling


### Built-in Metrics


The library provides atomic counters for monitoring:

```rust
use network_protocol::utils::metrics;

// Get current metrics
let stats = metrics::get_stats();
println!("Handshakes: {}", stats.handshakes_completed);
println!("Messages: {}", stats.messages_sent);
println!("Errors: {}", stats.errors_total);
```

**Available metrics:**
- `handshakes_completed`: Total successful handshakes
- `messages_sent`: Total messages transmitted
- `messages_received`: Total messages received
- `connections_active`: Current active connections
- `errors_total`: Total errors encountered

### Logging Configuration


Structured logging with tracing:

```rust
use network_protocol::{init_with_config, utils::logging::LogConfig};
use tracing::Level;

let config = LogConfig {
    app_name: "my-app".to_string(),
    log_level: Level::INFO,      // INFO for production
    log_to_file: true,
    log_dir: "/var/log/my-app".into(),
};

init_with_config(&config);
```

**Log levels:**
- `ERROR`: Critical issues only
- `WARN`: Production default (errors + warnings)
- `INFO`: Normal operation visibility
- `DEBUG`: Development/troubleshooting
- `TRACE`: Verbose debugging (not for production)

### Profiling with perf (Linux)


```bash
# Record profile

perf record --call-graph dwarf ./target/release/my-app

# View report

perf report

# Generate flamegraph

perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg
```

### Profiling with Instruments (macOS)


```bash
# Time profiler

instruments -t "Time Profiler" -D profile.trace ./target/release/my-app

# Allocations

instruments -t "Allocations" -D allocs.trace ./target/release/my-app
```

### Memory Profiling with valgrind


```bash
# Install valgrind

sudo apt install valgrind

# Run with massif (heap profiler)

valgrind --tool=massif ./target/release/my-app

# Analyze results

ms_print massif.out.*
```

---

## Performance Checklist


Before deploying to production:

- [ ] Build with `--release` and LTO enabled
- [ ] Choose appropriate compression algorithm (LZ4 for latency, Zstd for bandwidth)
- [ ] Use Bincode serialization format
- [ ] Configure timeouts based on network characteristics
- [ ] Tune channel buffer sizes for workload
- [ ] Set appropriate connection pool limits
- [ ] Enable platform-specific optimizations
- [ ] Run benchmarks to establish baseline
- [ ] Configure monitoring and metrics collection
- [ ] Set log level to WARN or ERROR in production
- [ ] Test under expected load (stress testing)
- [ ] Profile hotspots and optimize critical paths

---

## Performance Targets


**Typical performance on modern hardware (Linux, x86_64, 3.0 GHz):**

| Operation | Latency | Throughput |
|-----------|---------|------------|
| Packet encode | ~12 ns | 2.5 GB/s |
| Packet decode | ~18 ns | 1.8 GB/s |
| Handshake (full) | ~50-100 µs | 10k-20k/sec |
| Message send | ~200 ns | 5M msgs/sec |
| LZ4 compress | ~2 µs/KB | 500 MB/s |
| LZ4 decompress | ~0.5 µs/KB | 2 GB/s |

**Scalability:**
- 10,000+ concurrent connections per server
- 100,000+ messages/sec sustained throughput
- Sub-millisecond p99 latency at moderate load
- Linear scaling with CPU cores (to ~16 cores)

---

## Troubleshooting Performance Issues


### High Latency


**Symptoms**: Slow response times, timeouts  
**Solutions**:
- Reduce compression threshold
- Use LZ4 instead of Zstd
- Decrease channel buffer sizes
- Check network latency (`ping`, `traceroute`)
- Profile with `tracing` at DEBUG level

### High CPU Usage


**Symptoms**: 100% CPU, thread contention  
**Solutions**:
- Reduce compression level (or disable)
- Increase compression threshold
- Use async operations instead of blocking
- Profile with `perf` or `Instruments`
- Check for busy loops in application code

### High Memory Usage


**Symptoms**: Growing RSS, OOM errors  
**Solutions**:
- Reduce channel buffer sizes
- Implement connection limits
- Use compression for large payloads
- Check for memory leaks with `valgrind`
- Review replay cache and session cache sizes

### Low Throughput


**Symptoms**: Below expected messages/sec  
**Solutions**:
- Increase channel buffer sizes
- Use Bincode serialization
- Batch messages when possible
- Check network bandwidth (`iperf`)
- Profile with benchmarks
- Verify no single-threaded bottlenecks

---

## Additional Resources


- [Rust Performance Book]https://nnethercote.github.io/perf-book/
- [Tokio Performance Tuning]https://tokio.rs/tokio/topics/performance
- [Linux Performance Tools]http://www.brendangregg.com/linuxperf.html
- [Criterion.rs Documentation]https://bheisler.github.io/criterion.rs/book/

---

For deployment patterns and architecture guidance, see [DEPLOYMENT.md](./DEPLOYMENT.md).