sz-rust-core 0.6.3

SZ-Rust 核心库:HTTP 服务器、路由、控制器、中间件,对标 ThinkPHP 8
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
//! Runtime 性能对比测试
//!
//! ## 测试目标
//!
//! 验证 SZ-Rust tokio runtime 相比 PHP Swoole 的性能特征:
//!
//! 1. **spawn 吞吐量**:tokio::spawn vs Swoole\Coroutine::create
//! 2. **CancellationToken 取消传播延迟**:token.cancel() 到任务响应的延迟
//! 3. **JoinSet 任务管理开销**:管理大量任务的开销
//! 4. **Scheduler tick 延迟**:tokio::time::interval + try_fire_due 的 tick 精度
//! 5. **Queue 消费吞吐量**:InMemoryQueue publish/consume 循环吞吐
//!
//! ## PHP 对比基线
//!
//! | 指标 | PHP Swoole 基线 | Rust 目标 |
//! |------|----------------|-----------|
//! | spawn 10k tasks | ~50ms | <100ms |
//! | cancel 传播延迟 | ~1ms | <1ms |
//! | Scheduler tick 精度 | ±5ms | ±1ms |
//!
//! ## 注意
//!
//! - 性能测试受 CI 环境波动影响,阈值设为宽松值
//! - 不与 PHP 实测对比,仅验证 Rust 侧性能在合理范围
//! - 标记为 `#[ignore]` 默认不运行,通过 `--ignored` 显式触发

#![cfg(test)]

use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use sz_rust_core::runtime::{
    spawn_with_token, GracefulShutdown, QueueRuntime, QueueRuntimeConfig, SchedulerRuntime,
    SzRuntime, WorkerConfig,
};
use tokio_util::sync::CancellationToken;

/// 辅助函数:测量异步操作的耗时
async fn measure_async<F, Fut, T>(label: &str, f: F) -> T
where
    F: FnOnce() -> Fut,
    Fut: std::future::Future<Output = T>,
{
    let start = Instant::now();
    let result = f().await;
    let elapsed = start.elapsed();
    println!("[PERF] {}: {:?}", label, elapsed);
    result
}

// ============================================================================
// 组 1:spawn 吞吐量测试
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_spawn_10k_tasks_throughput() {
    let rt = SzRuntime::with_worker_threads(4);
    let counter = Arc::new(AtomicUsize::new(0));

    measure_async("spawn 10k tasks", || async {
        let mut handles = Vec::with_capacity(10_000);
        for _ in 0..10_000 {
            let c = counter.clone();
            handles.push(rt.spawn(async move {
                c.fetch_add(1, Ordering::Relaxed);
            }));
        }
        // 等待所有任务完成
        for h in handles {
            let _ = h.await;
        }
    })
    .await;

    assert_eq!(counter.load(Ordering::SeqCst), 10_000);
}

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_spawn_with_token_1k_tasks_throughput() {
    let token = CancellationToken::new();
    let counter = Arc::new(AtomicUsize::new(0));

    measure_async("spawn_with_token 1k tasks", || async {
        let mut handles = Vec::with_capacity(1_000);
        for _ in 0..1_000 {
            let t = token.clone();
            let c = counter.clone();
            handles.push(spawn_with_token(t, async move {
                c.fetch_add(1, Ordering::Relaxed);
            }));
        }
        for h in handles {
            let _ = h.await;
        }
    })
    .await;

    assert_eq!(counter.load(Ordering::SeqCst), 1_000);
}

// ============================================================================
// 组 2:CancellationToken 取消传播延迟
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_cancel_propagation_latency() {
    let token = CancellationToken::new();
    let token_clone = token.clone();
    let completed = Arc::new(AtomicUsize::new(0));
    let completed_clone = completed.clone();

    // spawn 100 个监听 token 的任务
    for _ in 0..100 {
        let t = token.clone();
        let c = completed.clone();
        tokio::spawn(async move {
            t.cancelled().await;
            c.fetch_add(1, Ordering::Relaxed);
        });
    }

    // 测量从 cancel() 到所有任务响应的延迟
    let start = Instant::now();
    token_clone.cancel();

    // 等待所有任务完成
    tokio::time::sleep(Duration::from_millis(50)).await;
    let elapsed = start.elapsed();

    println!("[PERF] cancel propagation for 100 tasks: {:?}", elapsed);
    assert_eq!(completed_clone.load(Ordering::SeqCst), 100);
    // 取消传播应在 50ms 内完成
    assert!(
        elapsed < Duration::from_millis(50),
        "cancel propagation too slow: {:?}",
        elapsed
    );
}

// ============================================================================
// 组 3:JoinSet (GracefulShutdown) 任务管理开销
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_graceful_shutdown_1k_tasks() {
    let mut gs = GracefulShutdown::new();

    measure_async("GracefulShutdown spawn 1k tasks", || async {
        for _ in 0..1_000 {
            let token = gs.token();
            gs.spawn(async move {
                token.cancelled().await;
            });
        }
    })
    .await;

    assert_eq!(gs.len(), 1_000);

    let (success, aborted) = measure_async("GracefulShutdown shutdown 1k tasks", || async {
        gs.shutdown(Duration::from_secs(1)).await
    })
    .await;

    assert!(success);
    assert_eq!(aborted, 0);
}

// ============================================================================
// 组 4:Scheduler tick 精度
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_scheduler_tick_latency() {
    use sz_rust_core::runtime::scheduler::SchedulerRuntimeConfig;

    let config = SchedulerRuntimeConfig::new(100);
    let sr = SchedulerRuntime::new(config);

    // 测量 10 次 tick 的总耗时
    let tick_count = 10;
    let expected_total = Duration::from_millis(100 * tick_count); // 100ms per tick

    let start = Instant::now();
    for _ in 0..tick_count {
        tokio::time::sleep(Duration::from_millis(100)).await;
        sr.try_fire_due();
    }
    let elapsed = start.elapsed();

    println!(
        "[PERF] scheduler {} ticks: {:?} (expected ~{:?})",
        tick_count, elapsed, expected_total
    );

    // 误差应在 ±20% 以内
    let tolerance = expected_total / 5;
    let lower = expected_total - tolerance;
    let upper = expected_total + tolerance;
    assert!(
        elapsed >= lower && elapsed <= upper,
        "tick latency out of tolerance: {:?} not in [{:?}, {:?}]",
        elapsed,
        lower,
        upper
    );
}

// ============================================================================
// 组 5:Queue 消费吞吐量
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_queue_publish_consume_throughput() {
    use sz_orm_queue::{InMemoryQueue, MessageQueue};

    let queue: Arc<dyn MessageQueue> = Arc::new(InMemoryQueue::new());
    let message_count = 1_000usize;

    // 测量 publish 吞吐
    measure_async("publish 1k messages", || async {
        for i in 0..message_count {
            let payload = format!("msg-{}", i);
            queue
                .publish("perf-test", payload.as_bytes())
                .await
                .unwrap();
        }
    })
    .await;

    // 测量 consume 吞吐
    let consumed = Arc::new(AtomicUsize::new(0));
    let consumed_clone = consumed.clone();
    measure_async("consume 1k messages", || async {
        loop {
            let msg = queue.consume("perf-test").await.unwrap();
            if msg.is_none() {
                break;
            }
            consumed_clone.fetch_add(1, Ordering::Relaxed);
        }
    })
    .await;

    assert_eq!(consumed.load(Ordering::SeqCst), message_count);
}

// ============================================================================
// 组 6:WorkerConfig 边界性能
// ============================================================================

#[tokio::test]
async fn perf_worker_config_build_benchmark() {
    let iterations = 10_000;
    let start = Instant::now();
    for _ in 0..iterations {
        let _config = WorkerConfig::new()
            .with_worker_num(8)
            .with_reactor_num(4)
            .with_task_worker_num(2);
    }
    let elapsed = start.elapsed();
    let per_op = elapsed / iterations as u32;
    println!(
        "[PERF] WorkerConfig build x{}: {:?} (per-op: {:?})",
        iterations, elapsed, per_op
    );
    // WorkerConfig 构建应在微秒级
    assert!(
        per_op < Duration::from_micros(100),
        "WorkerConfig build too slow: {:?} per op",
        per_op
    );
}

// ============================================================================
// 组 7:CancellationToken clone 性能
// ============================================================================

#[tokio::test]
async fn perf_token_clone_benchmark() {
    let token = CancellationToken::new();
    let iterations = 100_000;
    let start = Instant::now();
    for _ in 0..iterations {
        let _clone = token.clone();
    }
    let elapsed = start.elapsed();
    let per_op = elapsed / iterations as u32;
    println!(
        "[PERF] CancellationToken clone x{}: {:?} (per-op: {:?})",
        iterations, elapsed, per_op
    );
    // clone 应在纳秒级
    assert!(
        per_op < Duration::from_micros(10),
        "token clone too slow: {:?} per op",
        per_op
    );
}

// ============================================================================
// 组 8:SzRuntime 创建/销毁开销
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_runtime_create_destroy() {
    let iterations = 10;
    let start = Instant::now();
    for _ in 0..iterations {
        let rt = SzRuntime::with_worker_threads(1);
        // 立即关闭
        rt.shutdown_timeout(Duration::from_millis(10));
    }
    let elapsed = start.elapsed();
    let per_op = elapsed / iterations as u32;
    println!(
        "[PERF] SzRuntime create+destroy x{}: {:?} (per-op: {:?})",
        iterations, elapsed, per_op
    );
    // runtime 创建+销毁应在 100ms 以内
    assert!(
        per_op < Duration::from_millis(100),
        "runtime create/destroy too slow: {:?} per op",
        per_op
    );
}

// ============================================================================
// 组 9:QueueRuntime 消费循环性能
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_queue_runtime_consumer_loop() {
    use sz_orm_queue::{InMemoryQueue, MessageQueue};

    let queue: Arc<dyn MessageQueue> = Arc::new(InMemoryQueue::new());

    // 预发布消息
    for i in 0..100 {
        let payload = format!("msg-{}", i);
        queue.publish("perf", payload.as_bytes()).await.unwrap();
    }

    let config = QueueRuntimeConfig::new("perf").with_poll_interval(1);
    let runtime = QueueRuntime::new(config, queue.clone());

    struct CountingConsumer {
        counter: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl sz_rust_core::runtime::QueueConsumer for CountingConsumer {
        async fn handle(
            &self,
            _message: &sz_orm_queue::Message,
        ) -> Result<(), sz_rust_core::runtime::queue::QueueConsumerError> {
            self.counter.fetch_add(1, Ordering::Relaxed);
            Ok(())
        }
    }

    let counter = Arc::new(AtomicUsize::new(0));
    let consumer = Arc::new(CountingConsumer {
        counter: counter.clone(),
    });

    let token = CancellationToken::new();
    let token_clone = token.clone();

    let handle = runtime.start(consumer, token);

    // 等待消费完成
    tokio::time::sleep(Duration::from_millis(200)).await;
    token_clone.cancel();
    let _ = handle.await;

    println!(
        "[PERF] QueueRuntime consumed: {}",
        counter.load(Ordering::SeqCst)
    );
    assert_eq!(counter.load(Ordering::SeqCst), 100);
}

// ============================================================================
// 组 10:综合场景 — spawn + cancel + join
// ============================================================================

#[tokio::test]
#[ignore = "性能测试默认不运行,使用 --ignored 触发"]
async fn perf_integrated_scenario() {
    let rt = SzRuntime::with_worker_threads(2);
    let token = rt.shutdown_token();
    let counter = Arc::new(AtomicUsize::new(0));

    // spawn 100 个任务,每个任务循环 100 次
    let mut handles = Vec::with_capacity(100);
    for _ in 0..100 {
        let t = token.clone();
        let c = counter.clone();
        handles.push(rt.spawn(async move {
            for _ in 0..100 {
                if t.is_cancelled() {
                    return;
                }
                c.fetch_add(1, Ordering::Relaxed);
                tokio::task::yield_now().await;
            }
        }));
    }

    // 等待一段时间让任务执行
    tokio::time::sleep(Duration::from_millis(50)).await;

    // 触发关闭
    let start = Instant::now();
    rt.shutdown_timeout(Duration::from_millis(500));
    let elapsed = start.elapsed();

    println!(
        "[PERF] integrated scenario: counter={}, shutdown={:?}",
        counter.load(Ordering::SeqCst),
        elapsed
    );

    // 验证任务确实执行了
    assert!(counter.load(Ordering::SeqCst) > 0);
}