github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/read_state_test.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package pebble
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/petermattis/pebble/vfs"
    13  	"golang.org/x/exp/rand"
    14  )
    15  
    16  func BenchmarkReadState(b *testing.B) {
    17  	d, err := Open("", &Options{
    18  		FS: vfs.NewMem(),
    19  	})
    20  	if err != nil {
    21  		b.Fatal(err)
    22  	}
    23  
    24  	for _, updateFrac := range []float32{0, 0.1, 0.5} {
    25  		b.Run(fmt.Sprintf("updates=%.0f", updateFrac*100), func(b *testing.B) {
    26  			b.RunParallel(func(pb *testing.PB) {
    27  				rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
    28  
    29  				for pb.Next() {
    30  					if rng.Float32() < updateFrac {
    31  						d.mu.Lock()
    32  						d.updateReadStateLocked()
    33  						d.mu.Unlock()
    34  					} else {
    35  						s := d.loadReadState()
    36  						s.unref()
    37  					}
    38  				}
    39  			})
    40  		})
    41  	}
    42  
    43  	if err := d.Close(); err != nil {
    44  		b.Fatal(err)
    45  	}
    46  }