gopkg.in/go-playground/pool.v1@v1.2.2/benchmarks_test.go (about)

     1  package pool
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func BenchmarkSmallRun(b *testing.B) {
     9  
    10  	pool := NewPool(4, 10)
    11  
    12  	fn := func(job *Job) {
    13  
    14  		i := job.Params()[0].(int)
    15  		time.Sleep(time.Second * 1)
    16  		job.Return(i)
    17  	}
    18  
    19  	for i := 0; i < 10; i++ {
    20  		pool.Queue(fn, i)
    21  	}
    22  
    23  	for range pool.Results() {
    24  	}
    25  }
    26  
    27  func BenchmarkSmallCancel(b *testing.B) {
    28  
    29  	pool := NewPool(4, 20)
    30  
    31  	fn := func(job *Job) {
    32  
    33  		i := job.Params()[0].(int)
    34  		if i == 6 {
    35  			job.Cancel()
    36  			return
    37  		}
    38  
    39  		time.Sleep(time.Second * 1)
    40  		job.Return(i)
    41  	}
    42  
    43  	for i := 0; i < 20; i++ {
    44  		pool.Queue(fn, i)
    45  	}
    46  
    47  	for range pool.Results() {
    48  	}
    49  }
    50  
    51  func BenchmarkLargeCancel(b *testing.B) {
    52  
    53  	pool := NewPool(4, 1000)
    54  
    55  	fn := func(job *Job) {
    56  
    57  		i := job.Params()[0].(int)
    58  		if i == 6 {
    59  			job.Cancel()
    60  			return
    61  		}
    62  
    63  		time.Sleep(time.Second * 1)
    64  		job.Return(i)
    65  	}
    66  
    67  	for i := 0; i < 1000; i++ {
    68  		pool.Queue(fn, i)
    69  	}
    70  
    71  	for range pool.Results() {
    72  	}
    73  }
    74  
    75  func BenchmarkOverconsumeLargeRun(b *testing.B) {
    76  
    77  	pool := NewPool(25, 100)
    78  
    79  	fn := func(job *Job) {
    80  
    81  		i := job.Params()[0].(int)
    82  		time.Sleep(time.Second * 1)
    83  		job.Return(i)
    84  	}
    85  
    86  	for i := 0; i < 100; i++ {
    87  		pool.Queue(fn, i)
    88  	}
    89  
    90  	for range pool.Results() {
    91  	}
    92  }