kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/test/services/graphstore/graphstore.go (about)

     1  /*
     2   * Copyright 2015 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 graphstore contains common utilities for testing GraphStore
    18  // implementations.
    19  package graphstore // import "kythe.io/kythe/go/test/services/graphstore"
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"kythe.io/kythe/go/services/graphstore"
    27  	"kythe.io/kythe/go/test/testutil"
    28  	"kythe.io/kythe/go/util/compare"
    29  
    30  	spb "kythe.io/kythe/proto/storage_go_proto"
    31  )
    32  
    33  // Service re-exports graphstore.Service for tests
    34  type Service graphstore.Service
    35  
    36  // CreateFunc creates a temporary graphstore.Service with a corresponding
    37  // function to destroy it completely.
    38  type CreateFunc func() (Service, DestroyFunc, error)
    39  
    40  // DestroyFunc destroys its corresponding graphstore.Service returned
    41  // from a CreateFunc.
    42  type DestroyFunc func() error
    43  
    44  // NullDestroy does nothing.
    45  func NullDestroy() error { return nil }
    46  
    47  const keySize = 16 // bytes
    48  
    49  var ctx = context.Background()
    50  
    51  // BatchWriteBenchmark benchmarks the Write method of the given
    52  // graphstore.Service.  The number of updates per write is configured with
    53  // batchSize.
    54  func BatchWriteBenchmark(b *testing.B, create CreateFunc, batchSize int) {
    55  	b.StopTimer()
    56  	gs, destroy, err := create()
    57  	testutil.Fatalf(b, "CreateFunc error: %v", err)
    58  	defer func() {
    59  		testutil.Fatalf(b, "gs close error: %v", gs.Close(ctx))
    60  		testutil.Fatalf(b, "DestroyFunc error: %v", destroy())
    61  	}()
    62  
    63  	updates := make([]spb.WriteRequest_Update, batchSize)
    64  	req := &spb.WriteRequest{
    65  		Source: &spb.VName{},
    66  		Update: make([]*spb.WriteRequest_Update, batchSize),
    67  	}
    68  	for i := 0; i < b.N; i++ {
    69  		randVName(req.Source, keySize)
    70  
    71  		for j := 0; j < batchSize; j++ {
    72  			randUpdate(&updates[j], keySize)
    73  			req.Update[j] = &updates[j]
    74  		}
    75  
    76  		testutil.Fatalf(b, "write error: %v", gs.Write(ctx, req))
    77  	}
    78  }
    79  
    80  // OrderTest tests the ordering of the streamed entries while reading from the
    81  // CreateFunc created graphstore.Service.
    82  func OrderTest(t *testing.T, create CreateFunc, batchSize int) {
    83  	gs, destroy, err := create()
    84  	testutil.Fatalf(t, "CreateFunc error: %v", err)
    85  	defer func() {
    86  		testutil.Fatalf(t, "gs close error: %v", gs.Close(ctx))
    87  		testutil.Fatalf(t, "DestroyFunc error: %v", destroy())
    88  	}()
    89  
    90  	updates := make([]spb.WriteRequest_Update, batchSize)
    91  	req := &spb.WriteRequest{
    92  		Source: &spb.VName{},
    93  		Update: make([]*spb.WriteRequest_Update, batchSize),
    94  	}
    95  	for i := 0; i < 1024; i++ {
    96  		randVName(req.Source, keySize)
    97  
    98  		for j := 0; j < batchSize; j++ {
    99  			randUpdate(&updates[j], keySize)
   100  			req.Update[j] = &updates[j]
   101  		}
   102  
   103  		testutil.Fatalf(t, "write error: %v", gs.Write(ctx, req))
   104  	}
   105  
   106  	var lastEntry *spb.Entry
   107  	testutil.Fatalf(t, "entryLess error: %v",
   108  		gs.Scan(ctx, new(spb.ScanRequest), func(entry *spb.Entry) error {
   109  			if compare.Entries(lastEntry, entry) != compare.LT {
   110  				return fmt.Errorf("expected {%v} < {%v}", lastEntry, entry)
   111  			}
   112  			return nil
   113  		}))
   114  }
   115  
   116  var factValue = []byte("factValue")
   117  
   118  func randUpdate(u *spb.WriteRequest_Update, size int) {
   119  	u.Target = &spb.VName{}
   120  	randVName(u.GetTarget(), size)
   121  	u.EdgeKind = testutil.RandStr(2)
   122  	u.FactName = testutil.RandStr(size)
   123  	u.FactValue = factValue
   124  }
   125  
   126  func randVName(v *spb.VName, size int) {
   127  	v.Signature = testutil.RandStr(size)
   128  	v.Corpus = testutil.RandStr(size)
   129  	v.Root = testutil.RandStr(size)
   130  	v.Path = testutil.RandStr(size)
   131  	v.Language = testutil.RandStr(size)
   132  }