============================================================================= 高级测试 =============================================================================
(t *testing.T)
| 281 | // ============================================================================= |
| 282 | |
| 283 | func TestActorHighThroughput(t *testing.T) { |
| 284 | if testing.Short() { |
| 285 | t.Skip("跳过高吞吐量测试(-short 模式)") |
| 286 | } |
| 287 | |
| 288 | config := actor.DefaultSystemConfig() |
| 289 | config.MailboxSize = 100000 |
| 290 | config.DefaultActorMailboxSize = 50000 |
| 291 | system := actor.NewSystemWithConfig("throughput", config) |
| 292 | defer system.Shutdown() |
| 293 | |
| 294 | // 创建计数器 Actor |
| 295 | var processed int64 |
| 296 | counterFunc := actor.ActorFunc(func(ctx *actor.Context, msg actor.Message) { |
| 297 | if _, ok := msg.(*IncrementMsg); ok { |
| 298 | atomic.AddInt64(&processed, 1) |
| 299 | } |
| 300 | }) |
| 301 | |
| 302 | props := &actor.Props{ |
| 303 | Name: "counter", |
| 304 | MailboxSize: 50000, |
| 305 | } |
| 306 | pid := system.SpawnWithProps(counterFunc, props) |
| 307 | |
| 308 | // 发送大量消息 |
| 309 | numMessages := 100000 |
| 310 | start := time.Now() |
| 311 | |
| 312 | for range numMessages { |
| 313 | pid.Tell(&IncrementMsg{Value: 1}) |
| 314 | } |
| 315 | |
| 316 | // 等待处理完成 |
| 317 | for atomic.LoadInt64(&processed) < int64(numMessages) { |
| 318 | time.Sleep(10 * time.Millisecond) |
| 319 | } |
| 320 | |
| 321 | elapsed := time.Since(start) |
| 322 | throughput := float64(numMessages) / elapsed.Seconds() |
| 323 | |
| 324 | t.Logf("处理 %d 条消息耗时 %v,吞吐量: %.0f msg/s", numMessages, elapsed, throughput) |
| 325 | assert.GreaterOrEqual(t, throughput, 10000.0, "吞吐量应至少 10K msg/s") |
| 326 | } |
| 327 | |
| 328 | // ============================================================================= |
| 329 | // 测试入口 |
nothing calls this directly
no test coverage detected