kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/storage/leveldb/leveldb_test.go (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package leveldb
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"kythe.io/kythe/go/test/services/graphstore"
    26  	"kythe.io/kythe/go/test/storage/keyvalue"
    27  )
    28  
    29  const (
    30  	smallBatchSize  = 4
    31  	mediumBatchSize = 16
    32  	largeBatchSize  = 64
    33  )
    34  
    35  func tempDB() (keyvalue.DB, keyvalue.DestroyFunc, error) {
    36  	path, err := ioutil.TempDir("", "levelDB.benchmark")
    37  	if err != nil {
    38  		return nil, keyvalue.NullDestroy, err
    39  	}
    40  	db, err := Open(path, nil)
    41  	return db, func() error { return os.RemoveAll(path) }, err
    42  }
    43  
    44  func tempGS() (graphstore.Service, graphstore.DestroyFunc, error) {
    45  	db, destroy, err := tempDB()
    46  	if err != nil {
    47  		return nil, graphstore.DestroyFunc(destroy), fmt.Errorf("error creating temporary DB: %v", err)
    48  	}
    49  	return keyvalue.NewGraphStore(db), graphstore.DestroyFunc(destroy), err
    50  }
    51  
    52  func BenchmarkWriteSingle(b *testing.B) { keyvalue.BatchWriteBenchmark(b, tempDB, 1) }
    53  func BenchmarkWriteBatchSml(b *testing.B) {
    54  	keyvalue.BatchWriteBenchmark(b, tempDB, smallBatchSize)
    55  }
    56  func BenchmarkWriteBatchMed(b *testing.B) {
    57  	keyvalue.BatchWriteBenchmark(b, tempDB, mediumBatchSize)
    58  }
    59  func BenchmarkWriteBatchLrg(b *testing.B) {
    60  	keyvalue.BatchWriteBenchmark(b, tempDB, largeBatchSize)
    61  }
    62  
    63  func BenchmarkWriteParallelSingle(b *testing.B) {
    64  	keyvalue.BatchWriteParallelBenchmark(b, tempDB, 1)
    65  }
    66  func BenchmarkWriteParallelBatchLrg(b *testing.B) {
    67  	keyvalue.BatchWriteParallelBenchmark(b, tempDB, largeBatchSize)
    68  }
    69  
    70  func BenchmarkGSWriteSingleEntry(b *testing.B) {
    71  	graphstore.BatchWriteBenchmark(b, tempGS, 1)
    72  }
    73  func BenchmarkGSWriteBatchSml(b *testing.B) {
    74  	graphstore.BatchWriteBenchmark(b, tempGS, smallBatchSize)
    75  }
    76  func BenchmarkGSWriteBatchLrg(b *testing.B) {
    77  	graphstore.BatchWriteBenchmark(b, tempGS, largeBatchSize)
    78  }
    79  
    80  func TestOrder(t *testing.T) {
    81  	graphstore.OrderTest(t, tempGS, largeBatchSize)
    82  }