codel_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package aqm
  2. import (
  3. "context"
  4. "fmt"
  5. "sync"
  6. "sync/atomic"
  7. "testing"
  8. "time"
  9. "go-common/library/ecode"
  10. )
  11. var testConf = &Config{
  12. Target: 20,
  13. Internal: 500,
  14. }
  15. var qps = time.Microsecond * 2000
  16. func TestCoDel1200(t *testing.T) {
  17. q := New(testConf)
  18. drop := new(int64)
  19. tm := new(int64)
  20. delay := time.Millisecond * 3000
  21. testPush(q, qps, delay, drop, tm)
  22. fmt.Printf("qps %v process time %v drop %d timeout %d \n", int64(time.Second/qps), delay, *drop, *tm)
  23. time.Sleep(time.Second)
  24. }
  25. func TestCoDel200(t *testing.T) {
  26. q := New(testConf)
  27. drop := new(int64)
  28. tm := new(int64)
  29. delay := time.Millisecond * 2000
  30. testPush(q, qps, delay, drop, tm)
  31. fmt.Printf("qps %v process time %v drop %d timeout %d \n", int64(time.Second/qps), delay, *drop, *tm)
  32. time.Sleep(time.Second)
  33. }
  34. func TestCoDel100(t *testing.T) {
  35. q := New(testConf)
  36. drop := new(int64)
  37. tm := new(int64)
  38. delay := time.Millisecond * 1000
  39. testPush(q, qps, delay, drop, tm)
  40. fmt.Printf("qps %v process time %v drop %d timeout %d \n", int64(time.Second/qps), delay, *drop, *tm)
  41. }
  42. func TestCoDel50(t *testing.T) {
  43. q := New(testConf)
  44. drop := new(int64)
  45. tm := new(int64)
  46. delay := time.Millisecond * 500
  47. testPush(q, qps, delay, drop, tm)
  48. fmt.Printf("qps %v process time %v drop %d timeout %d \n", int64(time.Second/qps), delay, *drop, *tm)
  49. }
  50. func testPush(q *Queue, sleep time.Duration, delay time.Duration, drop *int64, tm *int64) {
  51. var group sync.WaitGroup
  52. for i := 0; i < 5000; i++ {
  53. time.Sleep(sleep)
  54. group.Add(1)
  55. go func() {
  56. defer group.Done()
  57. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*1000))
  58. defer cancel()
  59. if err := q.Push(ctx); err != nil {
  60. if err == ecode.LimitExceed {
  61. atomic.AddInt64(drop, 1)
  62. } else {
  63. atomic.AddInt64(tm, 1)
  64. }
  65. } else {
  66. time.Sleep(delay)
  67. q.Pop()
  68. }
  69. }()
  70. }
  71. group.Wait()
  72. }
  73. func BenchmarkAQM(b *testing.B) {
  74. q := Default()
  75. b.RunParallel(func(p *testing.PB) {
  76. for p.Next() {
  77. ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond*5))
  78. err := q.Push(ctx)
  79. if err == nil {
  80. q.Pop()
  81. }
  82. cancel()
  83. }
  84. })
  85. }