go.etcd.io/etcd@v3.3.27+incompatible/mvcc/backend/backend_bench_test.go (about)

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package backend
    16  
    17  import (
    18  	"crypto/rand"
    19  	"os"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func BenchmarkBackendPut(b *testing.B) {
    25  	backend, tmppath := NewTmpBackend(100*time.Millisecond, 10000)
    26  	defer backend.Close()
    27  	defer os.Remove(tmppath)
    28  
    29  	// prepare keys
    30  	keys := make([][]byte, b.N)
    31  	for i := 0; i < b.N; i++ {
    32  		keys[i] = make([]byte, 64)
    33  		rand.Read(keys[i])
    34  	}
    35  	value := make([]byte, 128)
    36  	rand.Read(value)
    37  
    38  	batchTx := backend.BatchTx()
    39  
    40  	batchTx.Lock()
    41  	batchTx.UnsafeCreateBucket([]byte("test"))
    42  	batchTx.Unlock()
    43  
    44  	b.ResetTimer()
    45  	for i := 0; i < b.N; i++ {
    46  		batchTx.Lock()
    47  		batchTx.UnsafePut([]byte("test"), keys[i], value)
    48  		batchTx.Unlock()
    49  	}
    50  }