github.com/claygod/queue@v0.0.0-20221013165802-9870c6dace8e/queue_bench_test.go (about)

     1  package queue
     2  
     3  // Queue
     4  // Bench
     5  // Copyright © 2016-2018 Eduard Sesigin. All rights reserved. Contacts: <claygod@yandex.ru>
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func BenchmarkPushTail(b *testing.B) {
    12  	b.StopTimer()
    13  	q := New()
    14  	b.StartTimer()
    15  	for i := 0; i < b.N; i++ {
    16  		q.PushTail(i)
    17  	}
    18  }
    19  
    20  func BenchmarkPushTailParallel(b *testing.B) {
    21  	b.StopTimer()
    22  	q := New()
    23  	i := 0
    24  	b.StartTimer()
    25  	b.RunParallel(func(pb *testing.PB) {
    26  		for pb.Next() {
    27  			q.PushTail(i)
    28  			i++
    29  		}
    30  	})
    31  }
    32  
    33  func BenchmarkPushHeadLimit(b *testing.B) {
    34  	b.StopTimer()
    35  	limit := 10000000
    36  	q := New(150000)
    37  	b.StartTimer()
    38  	for i := 0; i < b.N; i++ {
    39  		q.PushHead(i)
    40  		if i > limit {
    41  			break
    42  		}
    43  	}
    44  }
    45  
    46  func BenchmarkPushHead(b *testing.B) {
    47  	b.StopTimer()
    48  	q := New()
    49  	b.StartTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		q.PushHead(i)
    52  	}
    53  }
    54  
    55  func BenchmarkPopHead(b *testing.B) {
    56  	b.StopTimer()
    57  	q := New()
    58  	for i := 0; i < 10000000; i++ {
    59  		q.PushTail(i)
    60  	}
    61  	b.StartTimer()
    62  	for i := 0; i < 1000000; i++ {
    63  		q.PopHead()
    64  	}
    65  }
    66  
    67  func BenchmarkPopTail(b *testing.B) {
    68  	b.StopTimer()
    69  	q := New()
    70  	for i := 0; i < 10000000; i++ {
    71  		q.PushTail(i)
    72  	}
    73  	b.StartTimer()
    74  	for i := 0; i < 1000000; i++ {
    75  		q.PopTail()
    76  	}
    77  }
    78  
    79  func BenchmarkQueueList(b *testing.B) {
    80  	b.StopTimer()
    81  	q := New()
    82  	for i := 0; i < 10000000; i++ {
    83  		q.PushTail(i)
    84  	}
    85  	b.StartTimer()
    86  	for i := 0; i < 1000000; i++ {
    87  		q.PopHeadList(10)
    88  	}
    89  }
    90  
    91  func BenchmarkQueueListParallel(b *testing.B) {
    92  	b.StopTimer()
    93  	q := New()
    94  	for i := 0; i < 10000000; i++ {
    95  		q.PushTail(i)
    96  	}
    97  	b.StartTimer()
    98  	b.RunParallel(func(pb *testing.PB) {
    99  		for pb.Next() {
   100  			q.PopHeadList(2)
   101  		}
   102  	})
   103  }