github.com/pharosnet/flyline@v1.0.2/queue_buffer_benchmark_test.go (about)

     1  package flyline
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestFlyline_QueueBuffer(t *testing.T) {
    11  	N := 1000
    12  	var start time.Time
    13  	var dur time.Duration
    14  
    15  	start = time.Now()
    16  	benchmarkFlylineQueueBuffer(N, true)
    17  	dur = time.Since(start)
    18  	fmt.Printf("flyline-queuqbuffer: %d ops in %s (%d/sec)\n", N, dur, int(float64(N)/dur.Seconds()))
    19  
    20  	start = time.Now()
    21  	benchmarkGoChan(N, 100, true)
    22  	dur = time.Since(start)
    23  	fmt.Printf("go-channel(100):  %d ops in %s (%d/sec)\n", N, dur, int(float64(N)/dur.Seconds()))
    24  
    25  	start = time.Now()
    26  	benchmarkGoChan(N, 10, true)
    27  	dur = time.Since(start)
    28  	fmt.Printf("go-channel(10):   %d ops in %s (%d/sec)\n", N, dur, int(float64(N)/dur.Seconds()))
    29  
    30  	start = time.Now()
    31  	benchmarkGoChan(N, 0, true)
    32  	dur = time.Since(start)
    33  	fmt.Printf("go-channel(0):    %d ops in %s (%d/sec)\n", N, dur, int(float64(N)/dur.Seconds()))
    34  
    35  }
    36  
    37  func benchmarkFlylineQueueBuffer(N int, validate bool) {
    38  	buf := NewQueueBuffer()
    39  	var wg sync.WaitGroup
    40  	wg.Add(1)
    41  	go func() {
    42  		for i := 0; i < N; i++ {
    43  			v, ok := buf.Recv()
    44  			//fmt.Println(ok, v)
    45  			if !ok {
    46  				break
    47  			}
    48  			vInt := int64(-1)
    49  			ValueScan(v, &vInt)
    50  			if validate {
    51  				if vInt != int64(i) {
    52  					panic("out of order")
    53  				}
    54  			}
    55  		}
    56  		wg.Done()
    57  	}()
    58  	for i := 0; i < N; i++ {
    59  		buf.Send(int64(i))
    60  	}
    61  	buf.Close()
    62  	wg.Wait()
    63  	//buf.Sync(context.Background())
    64  }
    65  
    66  func benchmarkGoChan(N, buffered int, validate bool) {
    67  	ch := make(chan int64, buffered)
    68  	var wg sync.WaitGroup
    69  	wg.Add(1)
    70  	go func() {
    71  		for i := 0; i < N; i++ {
    72  			v := <-ch
    73  			if validate {
    74  				if v != int64(i) {
    75  					panic("out of order")
    76  				}
    77  			}
    78  		}
    79  		wg.Done()
    80  	}()
    81  	for i := 0; i < N; i++ {
    82  		ch <- int64(i)
    83  	}
    84  	close(ch)
    85  	wg.Wait()
    86  }
    87  
    88  func BenchmarkFlyline_QueueBuffer(b *testing.B) {
    89  	b.ReportAllocs()
    90  	benchmarkFlylineQueueBuffer(b.N, false)
    91  }
    92  
    93  func BenchmarkGoChan100(b *testing.B) {
    94  	b.ReportAllocs()
    95  	benchmarkGoChan(b.N, 100, false)
    96  }
    97  
    98  func BenchmarkGoChan10(b *testing.B) {
    99  	b.ReportAllocs()
   100  	benchmarkGoChan(b.N, 10, false)
   101  }
   102  
   103  func BenchmarkGoChanUnbuffered(b *testing.B) {
   104  	b.ReportAllocs()
   105  	benchmarkGoChan(b.N, 0, false)
   106  }