github.com/ethersphere/bee/v2@v2.2.0/pkg/storage/storagetest/benchmark_test.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package storagetest
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"math/rand"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  const (
    16  	cr = 0.5
    17  	vs = 100
    18  )
    19  
    20  var (
    21  	format = "100000000000000%d"
    22  )
    23  
    24  func TestCompressibleBytes(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	rng := rand.New(rand.NewSource(time.Now().Unix()))
    28  
    29  	bts := compressibleBytes(rng, cr, vs)
    30  	if !bytes.Equal(bts[:50], bts[50:]) {
    31  		t.Errorf("expected \n%s to equal \n%s", string(bts[:50]), string(bts[50:]))
    32  	}
    33  
    34  	bts = compressibleBytes(rng, 0.25, vs)
    35  	if !bytes.Equal(bts[:25], bts[25:50]) || !bytes.Equal(bts[50:75], bts[75:]) {
    36  		t.Errorf("expected \n%s to equal \n%s", string(bts[:50]), string(bts[50:]))
    37  	}
    38  }
    39  
    40  func TestRandomValueGenerator(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	rng := rand.New(rand.NewSource(time.Now().Unix()))
    44  
    45  	t.Run("generates random values", func(t *testing.T) {
    46  		gen := makeRandomValueGenerator(rng, cr, vs)
    47  		if bytes.Equal(gen.Value(1), gen.Value(2)) {
    48  			t.Fatal("expected values to be random, got same value")
    49  		}
    50  	})
    51  
    52  	t.Run("respects value size", func(t *testing.T) {
    53  		gen := makeRandomValueGenerator(rng, cr, 10)
    54  		if len(gen.Value(1)) != 10 {
    55  			t.Fatal("expected values size to be 10, got", len(gen.Value(1)))
    56  		}
    57  	})
    58  }
    59  
    60  func TestFullRandomEntryGenerator(t *testing.T) {
    61  	t.Parallel()
    62  
    63  	t.Run("startAt is respected", func(t *testing.T) {
    64  		startAt, size := 10, 100
    65  		gen := newFullRandomEntryGenerator(startAt, size)
    66  		minVal := []byte("1000000000000010")
    67  		for i := 0; i < gen.NKey(); i++ {
    68  			if bytes.Compare(minVal, gen.Key(i)) > 0 {
    69  				t.Fatalf("%s should not be lower than %s", gen.Key(i), minVal)
    70  			}
    71  		}
    72  	})
    73  }
    74  
    75  func TestSequentialEntryGenerator(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	t.Run("generated values are consecutive ascending", func(t *testing.T) {
    79  		gen := newSequentialEntryGenerator(10)
    80  		for i := 0; i < gen.NKey(); i++ {
    81  			expected := fmt.Sprintf(format, i)
    82  			if expected != string(gen.Key(i)) {
    83  				t.Fatalf("%s expected to equal %s", expected, string(gen.Key(i)))
    84  			}
    85  		}
    86  	})
    87  }
    88  
    89  func TestReverseGenerator(t *testing.T) {
    90  	t.Parallel()
    91  
    92  	t.Run("generated values are consecutive descending", func(t *testing.T) {
    93  		gen := newReversedKeyGenerator(newSequentialKeyGenerator(10))
    94  		for i := 0; i < gen.NKey(); i++ {
    95  			expected := fmt.Sprintf(format, 9-i)
    96  			if expected != string(gen.Key(i)) {
    97  				t.Fatalf("%s expected to equal %s", expected, string(gen.Key(i)))
    98  			}
    99  		}
   100  	})
   101  }
   102  
   103  func TestStartAtEntryGenerator(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	t.Run("generated values are consecutive ascending", func(t *testing.T) {
   107  		startAt := 5
   108  		gen := newStartAtEntryGenerator(startAt, newSequentialEntryGenerator(10))
   109  		for i := 0; i < gen.NKey(); i++ {
   110  			expected := fmt.Sprintf(format, i+startAt)
   111  			if expected != string(gen.Key(i)) {
   112  				t.Fatalf("%s expected to equal %s", expected, string(gen.Key(i)))
   113  			}
   114  		}
   115  	})
   116  }
   117  
   118  func TestRoundKeyGenerator(t *testing.T) {
   119  	t.Parallel()
   120  
   121  	t.Run("repeating values are generated", func(t *testing.T) {
   122  		gen := newRoundKeyGenerator(newRandomKeyGenerator(100))
   123  		v := string(gen.Key(50))
   124  		var found bool
   125  		for i := 50; i < gen.NKey()+50; i++ {
   126  			if v == string(gen.Key(i)) {
   127  				found = true
   128  				break
   129  			}
   130  		}
   131  		if !found {
   132  			t.Fatal("repeating values not found")
   133  		}
   134  	})
   135  }