...

Source file src/github.com/syndtr/goleveldb/leveldb/cache/bench_test.go

Documentation: github.com/syndtr/goleveldb/leveldb/cache

     1  // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
     2  // All rights reserved.
     3  //
     4  // Use of this source code is governed by a BSD-style license that can be
     5  // found in the LICENSE file.
     6  
     7  package cache
     8  
     9  import (
    10  	"sync/atomic"
    11  	"testing"
    12  )
    13  
    14  func BenchmarkCache_InsertRemove(b *testing.B) {
    15  	b.StopTimer()
    16  	c := NewCache(nil)
    17  
    18  	b.StartTimer()
    19  	for i := 0; i < b.N; i++ {
    20  		c.Get(0, uint64(i), func() (int, Value) {
    21  			return 1, uint64(i)
    22  		}).Release()
    23  	}
    24  	b.ReportMetric(float64(c.Nodes()), "nodes")
    25  	b.Logf("STATS: %#v", c.GetStats())
    26  }
    27  
    28  func BenchmarkCache_Insert(b *testing.B) {
    29  	b.StopTimer()
    30  	c := NewCache(nil)
    31  
    32  	b.StartTimer()
    33  	for i := 0; i < b.N; i++ {
    34  		c.Get(0, uint64(i), func() (int, Value) {
    35  			return 1, uint64(i)
    36  		})
    37  	}
    38  	b.ReportMetric(float64(c.Nodes()), "nodes")
    39  	b.Logf("STATS: %#v", c.GetStats())
    40  }
    41  
    42  func BenchmarkCache_Lookup(b *testing.B) {
    43  	b.StopTimer()
    44  	c := NewCache(nil)
    45  	for i := 0; i < b.N; i++ {
    46  		c.Get(0, uint64(i), func() (int, Value) {
    47  			return 1, uint64(i)
    48  		})
    49  	}
    50  
    51  	b.StartTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		c.Get(0, uint64(i), nil).Release()
    54  	}
    55  	b.ReportMetric(float64(c.Nodes()), "nodes")
    56  	b.Logf("STATS: %#v", c.GetStats())
    57  }
    58  
    59  func BenchmarkCache_AppendRemove(b *testing.B) {
    60  	b.StopTimer()
    61  	c := NewCache(nil)
    62  	for i := 0; i < b.N; i++ {
    63  		c.Get(0, uint64(i), func() (int, Value) {
    64  			return 1, uint64(i)
    65  		})
    66  	}
    67  
    68  	b.StartTimer()
    69  	for i := 0; i < b.N; i++ {
    70  		c.Get(1, uint64(i), func() (int, Value) {
    71  			return 1, uint64(i)
    72  		}).Release()
    73  	}
    74  	b.ReportMetric(float64(c.Nodes()), "nodes")
    75  	b.Logf("STATS: %#v", c.GetStats())
    76  }
    77  
    78  func BenchmarkCache_Append(b *testing.B) {
    79  	b.StopTimer()
    80  	c := NewCache(nil)
    81  	for i := 0; i < b.N; i++ {
    82  		c.Get(0, uint64(i), func() (int, Value) {
    83  			return 1, uint64(i)
    84  		})
    85  	}
    86  
    87  	b.StartTimer()
    88  	for i := 0; i < b.N; i++ {
    89  		c.Get(1, uint64(i), func() (int, Value) {
    90  			return 1, uint64(i)
    91  		})
    92  	}
    93  	b.ReportMetric(float64(c.Nodes()), "nodes")
    94  	b.Logf("STATS: %#v", c.GetStats())
    95  }
    96  
    97  func BenchmarkCache_Delete(b *testing.B) {
    98  	b.StopTimer()
    99  	c := NewCache(nil)
   100  	handles := make([]*Handle, b.N)
   101  	for i := 0; i < b.N; i++ {
   102  		handles[i] = c.Get(0, uint64(i), func() (int, Value) {
   103  			return 1, uint64(i)
   104  		})
   105  	}
   106  
   107  	b.StartTimer()
   108  	for i := 0; i < b.N; i++ {
   109  		handles[i].Release()
   110  	}
   111  	b.ReportMetric(float64(c.Nodes()), "nodes")
   112  	b.Logf("STATS: %#v", c.GetStats())
   113  }
   114  
   115  func BenchmarkCacheParallel_Insert(b *testing.B) {
   116  	b.StopTimer()
   117  	c := NewCache(nil)
   118  
   119  	var ns uint64
   120  	b.StartTimer()
   121  	b.RunParallel(func(pb *testing.PB) {
   122  		ns := atomic.AddUint64(&ns, 1)
   123  		i := uint64(0)
   124  		for pb.Next() {
   125  			c.Get(ns, i, func() (int, Value) {
   126  				return 1, i
   127  			})
   128  			i++
   129  		}
   130  	})
   131  	b.ReportMetric(float64(c.Nodes()), "nodes")
   132  	b.Logf("STATS: %#v", c.GetStats())
   133  }
   134  
   135  func BenchmarkCacheParallel_Lookup(b *testing.B) {
   136  	b.StopTimer()
   137  	c := NewCache(nil)
   138  	for i := 0; i < b.N; i++ {
   139  		c.Get(0, uint64(i), func() (int, Value) {
   140  			return 1, uint64(i)
   141  		})
   142  	}
   143  
   144  	var counter uint64
   145  	b.StartTimer()
   146  	b.RunParallel(func(pb *testing.PB) {
   147  		for pb.Next() {
   148  			i := atomic.AddUint64(&counter, 1) - 1
   149  			c.Get(0, i, nil).Release()
   150  		}
   151  	})
   152  	b.ReportMetric(float64(c.Nodes()), "nodes")
   153  	b.Logf("STATS: %#v", c.GetStats())
   154  }
   155  
   156  func BenchmarkCacheParallel_Append(b *testing.B) {
   157  	b.StopTimer()
   158  	c := NewCache(nil)
   159  	for i := 0; i < b.N; i++ {
   160  		c.Get(0, uint64(i), func() (int, Value) {
   161  			return 1, uint64(i)
   162  		})
   163  	}
   164  
   165  	var ns uint64
   166  	b.StartTimer()
   167  	b.RunParallel(func(pb *testing.PB) {
   168  		ns := atomic.AddUint64(&ns, 1)
   169  		i := uint64(0)
   170  		for pb.Next() {
   171  			c.Get(ns, i, func() (int, Value) {
   172  				return 1, i
   173  			})
   174  			i++
   175  		}
   176  	})
   177  	b.ReportMetric(float64(c.Nodes()), "nodes")
   178  	b.Logf("STATS: %#v", c.GetStats())
   179  }
   180  
   181  func BenchmarkCacheParallel_Delete(b *testing.B) {
   182  	b.StopTimer()
   183  	c := NewCache(nil)
   184  	handles := make([]*Handle, b.N)
   185  	for i := 0; i < b.N; i++ {
   186  		handles[i] = c.Get(0, uint64(i), func() (int, Value) {
   187  			return 1, uint64(i)
   188  		})
   189  	}
   190  
   191  	var counter int64
   192  	b.StartTimer()
   193  	b.RunParallel(func(pb *testing.PB) {
   194  		for pb.Next() {
   195  			i := atomic.AddInt64(&counter, 1) - 1
   196  			handles[i].Release()
   197  		}
   198  	})
   199  	b.ReportMetric(float64(c.Nodes()), "nodes")
   200  	b.Logf("STATS: %#v", c.GetStats())
   201  }
   202  

View as plain text