github.com/bruceshao/lockfree@v1.1.3-0.20230816090528-e89824c0a6e9/channel_test.go (about)

     1  /*
     2   * Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   */
     7  
     8  package lockfree
     9  
    10  import (
    11  	"fmt"
    12  	"sync"
    13  	"sync/atomic"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func BenchmarkChan(b *testing.B) {
    19  	var (
    20  		length   = 1024 * 1024
    21  		goSize   = GoSize
    22  		numPerGo = SchPerGo
    23  		counter  = uint64(0)
    24  		wg       sync.WaitGroup
    25  	)
    26  	ch := make(chan uint64, length)
    27  	// 消费端
    28  	go func() {
    29  		var ts time.Time
    30  		var count int32
    31  		for {
    32  			<-ch
    33  			atomic.AddInt32(&count, 1)
    34  			if count == 1 {
    35  				ts = time.Now()
    36  			}
    37  			//if x%10000000 == 0 {
    38  			//	fmt.Printf("read %d\n", x)
    39  			//}
    40  			if count == int32(goSize*numPerGo) {
    41  				tl := time.Since(ts)
    42  				fmt.Printf("read time = %d ms\n", tl.Milliseconds())
    43  			}
    44  		}
    45  	}()
    46  	wg.Add(b.N)
    47  	b.ResetTimer()
    48  	for i := 0; i < b.N; i++ {
    49  		go func() {
    50  			for j := 0; j < numPerGo; j++ {
    51  				x := atomic.AddUint64(&counter, 1)
    52  				ch <- x
    53  			}
    54  			wg.Done()
    55  		}()
    56  	}
    57  	wg.Wait()
    58  }
    59  
    60  func TestChan1(t *testing.T) {
    61  	var (
    62  		t1_10us     = uint64(0) // 1-10微秒
    63  		t10_100us   = uint64(0) // 10-100微秒
    64  		t100_1000us = uint64(0) // 100-1000微秒
    65  		t1_10ms     = uint64(0) // 1-10毫秒
    66  		t10_100ms   = uint64(0) // 10-100毫秒
    67  		t100_ms     = uint64(0) // 大于100毫秒
    68  	)
    69  
    70  	var (
    71  		length   = 1024 * 1024
    72  		goSize   = GoSize
    73  		numPerGo = SchPerGo
    74  		counter  = uint64(0)
    75  		slower   = uint64(0)
    76  		wg       sync.WaitGroup
    77  	)
    78  	ch := make(chan uint64, length)
    79  	// 消费端
    80  	go func() {
    81  		var ts time.Time
    82  		var count int32
    83  		for {
    84  			x := <-ch
    85  			atomic.AddInt32(&count, 1)
    86  			if count == 1 {
    87  				ts = time.Now()
    88  			}
    89  			if x%1000000 == 0 {
    90  				fmt.Printf("read %d\n", x)
    91  			}
    92  			if count == int32(goSize*numPerGo) {
    93  				tl := time.Since(ts)
    94  				fmt.Printf("read time = %d ms\n", tl.Milliseconds())
    95  			}
    96  		}
    97  	}()
    98  	wg.Add(goSize)
    99  	totalS := time.Now()
   100  	for i := 0; i < goSize; i++ {
   101  		go func() {
   102  			for j := 0; j < numPerGo; j++ {
   103  				x := atomic.AddUint64(&counter, 1)
   104  				ts := time.Now()
   105  				ch <- x
   106  				tl := time.Since(ts)
   107  				ms := tl.Microseconds()
   108  				if ms > 1 {
   109  					atomic.AddUint64(&slower, 1)
   110  					if ms < 10 { // t1_10us
   111  						atomic.AddUint64(&t1_10us, 1)
   112  					} else if ms < 100 {
   113  						atomic.AddUint64(&t10_100us, 1)
   114  					} else if ms < 1000 {
   115  						atomic.AddUint64(&t100_1000us, 1)
   116  					} else if ms < 10000 {
   117  						atomic.AddUint64(&t1_10ms, 1)
   118  					} else if ms < 100000 {
   119  						atomic.AddUint64(&t10_100ms, 1)
   120  					} else {
   121  						atomic.AddUint64(&t100_ms, 1)
   122  					}
   123  				}
   124  			}
   125  			wg.Done()
   126  		}()
   127  	}
   128  	wg.Wait()
   129  	totalL := time.Since(totalS)
   130  	fmt.Printf("write total time = [%d ms]\n", totalL.Milliseconds())
   131  	time.Sleep(time.Second * 3)
   132  	fmt.Printf("slow ratio = %.2f \n", float64(slower)*100.0/float64(counter))
   133  	fmt.Printf("quick ratio = %.2f \n", float64(goSize*numPerGo-int(slower))*100.0/float64(goSize*numPerGo))
   134  	fmt.Printf("[<1us][%d] \n", counter-slower)
   135  	fmt.Printf("[1-10us][%d] \n", t1_10us)
   136  	fmt.Printf("[10-100us][%d] \n", t10_100us)
   137  	fmt.Printf("[100-1000us][%d] \n", t100_1000us)
   138  	fmt.Printf("[1-10ms][%d] \n", t1_10ms)
   139  	fmt.Printf("[10-100ms][%d] \n", t10_100ms)
   140  	fmt.Printf("[>100ms][%d] \n", t100_ms)
   141  }