github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/concurrent/cmap/concurrent_map_bench_test.go (about)

     1  package cmap
     2  
     3  import "testing"
     4  import "strconv"
     5  
     6  func BenchmarkItems(b *testing.B) {
     7  	m := New(32)
     8  
     9  	// Insert 100 elements.
    10  	for i := 0; i < 10000; i++ {
    11  		m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
    12  	}
    13  	for i := 0; i < b.N; i++ {
    14  		m.Items()
    15  	}
    16  }
    17  
    18  func BenchmarkMarshalJson(b *testing.B) {
    19  	m := New(32)
    20  
    21  	// Insert 100 elements.
    22  	for i := 0; i < 10000; i++ {
    23  		m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
    24  	}
    25  	for i := 0; i < b.N; i++ {
    26  		m.MarshalJSON()
    27  	}
    28  }
    29  
    30  func BenchmarkStrconv(b *testing.B) {
    31  	for i := 0; i < b.N; i++ {
    32  		strconv.Itoa(i)
    33  	}
    34  }
    35  
    36  func BenchmarkSingleInsertAbsent(b *testing.B) {
    37  	m := New(32)
    38  	b.ResetTimer()
    39  	for i := 0; i < b.N; i++ {
    40  		m.Set(strconv.Itoa(i), "value")
    41  	}
    42  }
    43  
    44  func BenchmarkSingleInsertPresent(b *testing.B) {
    45  	m := New(32)
    46  	m.Set("key", "value")
    47  	b.ResetTimer()
    48  	for i := 0; i < b.N; i++ {
    49  		m.Set("key", "value")
    50  	}
    51  }
    52  
    53  func benchmarkMultiInsertDifferent(b *testing.B, count int) {
    54  	m := New(count)
    55  	finished := make(chan struct{}, b.N)
    56  	_, set := GetSet(m, finished)
    57  	b.ResetTimer()
    58  	for i := 0; i < b.N; i++ {
    59  		set(strconv.Itoa(i), "value")
    60  	}
    61  	for i := 0; i < b.N; i++ {
    62  		<-finished
    63  	}
    64  }
    65  
    66  func BenchmarkClear(b *testing.B) {
    67  	m := New(32)
    68  
    69  	// Insert 100 elements.
    70  	for i := 0; i < 10000; i++ {
    71  		m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
    72  	}
    73  	b.ResetTimer()
    74  	for i := 0; i < b.N; i++ {
    75  		m.Clear()
    76  	}
    77  }
    78  func BenchmarkPopAll(b *testing.B) {
    79  	m := New(32)
    80  
    81  	// Insert 100 elements.
    82  	for i := 0; i < 10000; i++ {
    83  		m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
    84  	}
    85  	b.ResetTimer()
    86  	for i := 0; i < b.N; i++ {
    87  		m.PopAll()
    88  	}
    89  }
    90  
    91  func BenchmarkMultiInsertDifferent_1_Shard(b *testing.B) {
    92  	benchmarkMultiInsertDifferent(b, 1)
    93  }
    94  func BenchmarkMultiInsertDifferent_16_Shard(b *testing.B) {
    95  	benchmarkMultiInsertDifferent(b, 16)
    96  }
    97  func BenchmarkMultiInsertDifferent_32_Shard(b *testing.B) {
    98  	benchmarkMultiInsertDifferent(b, 32)
    99  }
   100  func BenchmarkMultiInsertDifferent_256_Shard(b *testing.B) {
   101  	benchmarkMultiInsertDifferent(b, 256)
   102  }
   103  
   104  func BenchmarkMultiInsertSame(b *testing.B) {
   105  	m := New(32)
   106  	finished := make(chan struct{}, b.N)
   107  	_, set := GetSet(m, finished)
   108  	m.Set("key", "value")
   109  	b.ResetTimer()
   110  	for i := 0; i < b.N; i++ {
   111  		set("key", "value")
   112  	}
   113  	for i := 0; i < b.N; i++ {
   114  		<-finished
   115  	}
   116  }
   117  
   118  func BenchmarkMultiGetSame(b *testing.B) {
   119  	m := New(32)
   120  	finished := make(chan struct{}, b.N)
   121  	get, _ := GetSet(m, finished)
   122  	m.Set("key", "value")
   123  	b.ResetTimer()
   124  	for i := 0; i < b.N; i++ {
   125  		get("key", "value")
   126  	}
   127  	for i := 0; i < b.N; i++ {
   128  		<-finished
   129  	}
   130  }
   131  
   132  func benchmarkMultiGetSetDifferent(b *testing.B, count int) {
   133  	m := New(count)
   134  	finished := make(chan struct{}, 2*b.N)
   135  	get, set := GetSet(m, finished)
   136  	m.Set("-1", "value")
   137  	b.ResetTimer()
   138  	for i := 0; i < b.N; i++ {
   139  		set(strconv.Itoa(i-1), "value")
   140  		get(strconv.Itoa(i), "value")
   141  	}
   142  	for i := 0; i < 2*b.N; i++ {
   143  		<-finished
   144  	}
   145  }
   146  
   147  func BenchmarkMultiGetSetDifferent_1_Shard(b *testing.B) {
   148  	benchmarkMultiGetSetDifferent(b, 1)
   149  }
   150  func BenchmarkMultiGetSetDifferent_16_Shard(b *testing.B) {
   151  	benchmarkMultiGetSetDifferent(b, 16)
   152  }
   153  func BenchmarkMultiGetSetDifferent_32_Shard(b *testing.B) {
   154  	benchmarkMultiGetSetDifferent(b, 32)
   155  }
   156  func BenchmarkMultiGetSetDifferent_256_Shard(b *testing.B) {
   157  	benchmarkMultiGetSetDifferent(b, 256)
   158  }
   159  
   160  func benchmarkMultiGetSetBlock(b *testing.B, count int) {
   161  	m := New(count)
   162  	finished := make(chan struct{}, 2*b.N)
   163  	get, set := GetSet(m, finished)
   164  	for i := 0; i < b.N; i++ {
   165  		m.Set(strconv.Itoa(i%100), "value")
   166  	}
   167  	b.ResetTimer()
   168  	for i := 0; i < b.N; i++ {
   169  		set(strconv.Itoa(i%100), "value")
   170  		get(strconv.Itoa(i%100), "value")
   171  	}
   172  	for i := 0; i < 2*b.N; i++ {
   173  		<-finished
   174  	}
   175  }
   176  
   177  func BenchmarkMultiGetSetBlock_1_Shard(b *testing.B) {
   178  	benchmarkMultiGetSetBlock(b, 1)
   179  }
   180  func BenchmarkMultiGetSetBlock_16_Shard(b *testing.B) {
   181  	benchmarkMultiGetSetBlock(b, 16)
   182  }
   183  func BenchmarkMultiGetSetBlock_32_Shard(b *testing.B) {
   184  	benchmarkMultiGetSetBlock(b, 32)
   185  }
   186  func BenchmarkMultiGetSetBlock_256_Shard(b *testing.B) {
   187  	benchmarkMultiGetSetBlock(b, 256)
   188  }
   189  
   190  func GetSet(m ConcurrentMap, finished chan struct{}) (set func(key, value string), get func(key, value string)) {
   191  	return func(key, value string) {
   192  			for i := 0; i < 10; i++ {
   193  				m.Get(key)
   194  			}
   195  			finished <- struct{}{}
   196  		}, func(key, value string) {
   197  			for i := 0; i < 10; i++ {
   198  				m.Set(key, value)
   199  			}
   200  			finished <- struct{}{}
   201  		}
   202  }
   203  
   204  func BenchmarkKeys(b *testing.B) {
   205  	m := New(32)
   206  
   207  	// Insert 100 elements.
   208  	for i := 0; i < 10000; i++ {
   209  		m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
   210  	}
   211  	for i := 0; i < b.N; i++ {
   212  		m.Keys()
   213  	}
   214  }