...

Source file src/go.etcd.io/etcd/server/v3/mvcc/backend/backend_bench_test.go

Documentation: go.etcd.io/etcd/server/v3/mvcc/backend

     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_test
    16  
    17  import (
    18  	"crypto/rand"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
    24  	"go.etcd.io/etcd/server/v3/mvcc/buckets"
    25  )
    26  
    27  func BenchmarkBackendPut(b *testing.B) {
    28  	backend, _ := betesting.NewTmpBackend(b, 100*time.Millisecond, 10000)
    29  	defer betesting.Close(b, backend)
    30  
    31  	// prepare keys
    32  	keys := make([][]byte, b.N)
    33  	for i := 0; i < b.N; i++ {
    34  		keys[i] = make([]byte, 64)
    35  		_, err := rand.Read(keys[i])
    36  		assert.NoError(b, err)
    37  	}
    38  	value := make([]byte, 128)
    39  	_, err := rand.Read(value)
    40  	assert.NoError(b, err)
    41  
    42  	batchTx := backend.BatchTx()
    43  
    44  	batchTx.Lock()
    45  	batchTx.UnsafeCreateBucket(buckets.Test)
    46  	batchTx.Unlock()
    47  
    48  	b.ResetTimer()
    49  	for i := 0; i < b.N; i++ {
    50  		batchTx.Lock()
    51  		batchTx.UnsafePut(buckets.Test, keys[i], value)
    52  		batchTx.Unlock()
    53  	}
    54  }
    55  

View as plain text