github.com/cocotyty/oneshot@v0.0.0-20210707064948-4d5a81d0b747/shot_test.go (about)

     1  package oneshot
     2  
     3  import (
     4  	"runtime"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  )
     9  const TestSize = 2000000
    10  
    11  // BenchmarkChannel-8   	 8432158	      1020 ns/op
    12  func BenchmarkChannel(b *testing.B) {
    13  	wg := sync.WaitGroup{}
    14  	chs := make([]chan struct{}, b.N)
    15  	for i := 0; i < b.N; i++ {
    16  		wg.Add(1)
    17  		chs[i] = make(chan struct{})
    18  		go func(ch chan struct{}) {
    19  			<-ch
    20  			wg.Done()
    21  		}(chs[i])
    22  	}
    23  	b.ResetTimer()
    24  	for i := 0; i < b.N; i++ {
    25  		chs[i] <- struct{}{}
    26  	}
    27  	wg.Wait()
    28  }
    29  
    30  // BenchmarkShot-8   	10965412	       889.4 ns/op
    31  func BenchmarkShot(b *testing.B) {
    32  	wg := sync.WaitGroup{}
    33  	shots := make([]*Shot, b.N)
    34  
    35  	for i := 0; i < b.N; i++ {
    36  		i := i
    37  		wg.Add(1)
    38  		shots[i] = &Shot{}
    39  		go func(s *Shot) {
    40  			s.Wait()
    41  			wg.Done()
    42  		}(shots[i])
    43  	}
    44  	b.ResetTimer()
    45  	for i := 0; i < b.N; i++ {
    46  		shots[i].WakeUp()
    47  	}
    48  	wg.Wait()
    49  }
    50  
    51  func TestShots(t *testing.T) {
    52  	N := TestSize
    53  	wg := sync.WaitGroup{}
    54  	shots := make([]Shot, N)
    55  	for i := 0; i < N; i++ {
    56  		i := i
    57  		wg.Add(1)
    58  		go func(s *Shot) {
    59  			s.Wait()
    60  			wg.Done()
    61  		}(&shots[i])
    62  	}
    63  	startTime := time.Now()
    64  	for x := 0; x < N; x += 1 {
    65  		shots[x].WakeUp()
    66  	}
    67  	wg.Wait()
    68  	t.Log("usage:", time.Now().Sub(startTime))
    69  }
    70  
    71  
    72  func TestChans(t *testing.T) {
    73  	N := TestSize
    74  	wg := sync.WaitGroup{}
    75  	shots := make([]chan struct{}, N)
    76  	for i := 0; i < N; i++ {
    77  		ch:=make(chan struct{})
    78  		shots[i] = ch
    79  		wg.Add(1)
    80  		go func(ch chan struct{}) {
    81  			<-ch
    82  			wg.Done()
    83  		}(ch)
    84  	}
    85  	startTime := time.Now()
    86  	for x := 0; x < N; x += 1 {
    87  		shots[x]<- struct{}{}
    88  	}
    89  	wg.Wait()
    90  	t.Log("usage:", time.Now().Sub(startTime))
    91  }
    92  
    93  
    94  func TestShotParallel(t *testing.T) {
    95  	N := TestSize
    96  	wg := sync.WaitGroup{}
    97  	shots := make([]*Shot, N)
    98  	for i := 0; i < N; i++ {
    99  		i := i
   100  		shots[i] = &Shot{}
   101  		wg.Add(1)
   102  		go func(s *Shot) {
   103  			s.Wait()
   104  			wg.Done()
   105  		}(shots[i])
   106  	}
   107  	startTime := time.Now()
   108  	cpu := runtime.NumCPU()
   109  	batch := N / cpu
   110  	xshots := shots
   111  	for {
   112  		if len(xshots) <= batch {
   113  			go func(shots []*Shot) {
   114  				for _, s := range shots {
   115  					s.WakeUp()
   116  				}
   117  			}(xshots)
   118  			break
   119  		}
   120  		go func(shots []*Shot) {
   121  			for _, s := range shots {
   122  				s.WakeUp()
   123  			}
   124  		}(xshots[:batch])
   125  		xshots = xshots[batch:]
   126  	}
   127  	wg.Wait()
   128  	t.Log("usage:", time.Now().Sub(startTime))
   129  }
   130  
   131  func TestChannelParallel(t *testing.T) {
   132  	N := TestSize
   133  	wg := sync.WaitGroup{}
   134  	chs := make([]chan struct{}, N)
   135  	for i := 0; i < N; i++ {
   136  		i := i
   137  		wg.Add(1)
   138  		ch := make(chan struct{})
   139  		chs[i] = ch
   140  		go func(ch chan struct{}) {
   141  			<-ch
   142  			wg.Done()
   143  		}(ch)
   144  	}
   145  	startTime := time.Now()
   146  	batch := N / runtime.NumCPU()
   147  	xchs := chs
   148  	for {
   149  		if len(xchs) <= batch {
   150  			go func(chs []chan struct{}) {
   151  				for _, ch := range chs {
   152  					ch <- struct{}{}
   153  				}
   154  			}(xchs)
   155  			break
   156  		}
   157  		go func(chs []chan struct{}) {
   158  			for _, ch := range chs {
   159  				ch <- struct{}{}
   160  			}
   161  		}(xchs[:batch])
   162  		xchs = xchs[batch:]
   163  	}
   164  	wg.Wait()
   165  	t.Log("usage:", time.Now().Sub(startTime))
   166  }