github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/internal/pool/pool_test.go (about)

     1  package pool
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  const (
    10  	BenchmarkNTask = 100000
    11  	UnitNTask      = 1000
    12  	SleepTime      = time.Millisecond * 10
    13  )
    14  
    15  func TestTaskPool(t *testing.T) {
    16  	t.Run("TestTaskPool", func(t *testing.T) {
    17  		testPool(t, NewTaskPool[int64](256, 256, 1024, func(poolId int, err interface{}) {
    18  			t.Fatal(poolId, err)
    19  		}))
    20  	})
    21  	t.Run("TestFixedPool", func(t *testing.T) {
    22  		testPool(t, NewFixedPool[int64](2048, 256, 1024, func(poolId int, err interface{}) {
    23  			t.Fatal(poolId, err)
    24  		}))
    25  	})
    26  }
    27  
    28  func testPool(t *testing.T, pool TaskPool[int64]) {
    29  	defer pool.Stop()
    30  	go func() {
    31  		for {
    32  			time.Sleep(time.Second * 5)
    33  			t.Log("Live-Size", pool.LiveSize())
    34  			t.Log("Buffer-Size", pool.BufSize())
    35  			t.Log("Success", pool.ExecuteSuccess())
    36  			t.Log("Error", pool.ExecuteError())
    37  		}
    38  	}()
    39  	for i := 0; i < UnitNTask; i++ {
    40  		_ = pool.Push(int64(i), func() {
    41  			time.Sleep(time.Second)
    42  		})
    43  	}
    44  	_ = pool.Stop()
    45  }
    46  
    47  func BenchmarkTaskPool(b *testing.B) {
    48  	b.Run("DynamicTaskPool", func(b *testing.B) {
    49  		pool := NewTaskPool[int64](2048, 256, MaxTaskPoolSize, func(poolId int, err interface{}) {
    50  			return
    51  		})
    52  		b.ReportAllocs()
    53  		var wg sync.WaitGroup
    54  		b.ResetTimer()
    55  		for i := 0; i < b.N; i++ {
    56  			wg.Add(BenchmarkNTask)
    57  			b.StartTimer()
    58  			for j := 0; j < BenchmarkNTask; j++ {
    59  				_ = pool.Push(int64(j), func() {
    60  					time.Sleep(SleepTime)
    61  					wg.Done()
    62  				})
    63  			}
    64  			wg.Wait()
    65  			b.StopTimer()
    66  		}
    67  	})
    68  	b.Run("FixedPool", func(b *testing.B) {
    69  		pool := NewFixedPool[int64](1024*1024, 1024, MaxTaskPoolSize, func(poolId int, err interface{}) {
    70  			return
    71  		})
    72  		b.ReportAllocs()
    73  		var wg sync.WaitGroup
    74  		b.ResetTimer()
    75  		for i := 0; i < b.N; i++ {
    76  			wg.Add(BenchmarkNTask)
    77  			b.StartTimer()
    78  			for j := 0; j < BenchmarkNTask; j++ {
    79  				_ = pool.Push(int64(j), func() {
    80  					time.Sleep(SleepTime)
    81  					wg.Done()
    82  				})
    83  			}
    84  			wg.Wait()
    85  			b.StopTimer()
    86  		}
    87  	})
    88  	b.Run("NoTaskPool", func(b *testing.B) {
    89  		b.ReportAllocs()
    90  		var wg sync.WaitGroup
    91  		b.ResetTimer()
    92  		for i := 0; i < b.N; i++ {
    93  			wg.Add(BenchmarkNTask)
    94  			b.StartTimer()
    95  			for j := 0; j < BenchmarkNTask; j++ {
    96  				go func() {
    97  					time.Sleep(SleepTime)
    98  					wg.Done()
    99  				}()
   100  			}
   101  			wg.Wait()
   102  			b.StopTimer()
   103  		}
   104  	})
   105  }