github.com/matrixorigin/matrixone@v0.7.0/pkg/txn/storage/memorystorage/memtable/bench_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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 memtable
    16  
    17  import (
    18  	"sync/atomic"
    19  	"testing"
    20  
    21  	"github.com/google/uuid"
    22  )
    23  
    24  func BenchmarkDelete(b *testing.B) {
    25  	tx := NewTransaction(uuid.NewString(), Time{}, SnapshotIsolation)
    26  	table := NewTable[Int, int, TestRow]()
    27  	b.ResetTimer()
    28  	for i := 0; i < b.N; i++ {
    29  		key := Int(i)
    30  		if err := table.Delete(tx, key); err != nil {
    31  			b.Fatal(err)
    32  		}
    33  	}
    34  }
    35  
    36  func BenchmarkInsert(b *testing.B) {
    37  	tx := NewTransaction(uuid.NewString(), Time{}, SnapshotIsolation)
    38  	table := NewTable[Int, int, TestRow]()
    39  	b.ResetTimer()
    40  	for i := 0; i < b.N; i++ {
    41  		key := Int(i)
    42  		row := TestRow{
    43  			key:   key,
    44  			value: i,
    45  		}
    46  		if err := table.Insert(tx, row); err != nil {
    47  			b.Fatal(err)
    48  		}
    49  	}
    50  }
    51  
    52  func BenchmarkParallelInsert(b *testing.B) {
    53  	table := NewTable[Int, int, TestRow]()
    54  	b.ResetTimer()
    55  	var i int64
    56  	b.RunParallel(func(pb *testing.PB) {
    57  		tx := NewTransaction(uuid.NewString(), Time{}, SnapshotIsolation)
    58  		for pb.Next() {
    59  			i := atomic.AddInt64(&i, 1)
    60  			key := Int(i)
    61  			row := TestRow{
    62  				key:   key,
    63  				value: int(i),
    64  			}
    65  			if err := table.Insert(tx, row); err != nil {
    66  				b.Fatal(err)
    67  			}
    68  		}
    69  	})
    70  }
    71  
    72  func BenchmarkInsertAndGet(b *testing.B) {
    73  	tx := NewTransaction(uuid.NewString(), Time{}, SnapshotIsolation)
    74  	table := NewTable[Int, int, TestRow]()
    75  	b.ResetTimer()
    76  	for i := 0; i < b.N; i++ {
    77  		key := Int(i)
    78  		row := TestRow{
    79  			key:   key,
    80  			value: i,
    81  		}
    82  		if err := table.Insert(tx, row); err != nil {
    83  			b.Fatal(err)
    84  		}
    85  		tx.Time.Timestamp.PhysicalTime++
    86  		p, err := table.Get(tx, key)
    87  		if err != nil {
    88  			b.Fatal(err)
    89  		}
    90  		if p != i {
    91  			b.Fatal()
    92  		}
    93  	}
    94  }
    95  
    96  func BenchmarkInsertAndIndex(b *testing.B) {
    97  	tx := NewTransaction(uuid.NewString(), Time{}, SnapshotIsolation)
    98  	table := NewTable[Int, int, TestRow]()
    99  	b.ResetTimer()
   100  	for i := 0; i < b.N; i++ {
   101  		key := Int(i)
   102  		row := TestRow{
   103  			key:   key,
   104  			value: i,
   105  		}
   106  		if err := table.Insert(tx, row); err != nil {
   107  			b.Fatal(err)
   108  		}
   109  		tx.Time.Timestamp.PhysicalTime++
   110  		entries, err := table.Index(tx, Tuple{
   111  			Text("foo"), Int(i),
   112  		})
   113  		if err != nil {
   114  			b.Fatal(err)
   115  		}
   116  		if len(entries) != 1 {
   117  			b.Fatal()
   118  		}
   119  	}
   120  }