github.com/scottcagno/storage@v1.8.0/pkg/bio/writer_test.go (about)

     1  package bio
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"math/rand"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func doitNtimes(n int, fn func()) {
    12  	for i := 0; i < n; i++ {
    13  		time.Sleep(1 * time.Nanosecond)
    14  		fn()
    15  	}
    16  }
    17  
    18  // for benchmarks below
    19  var result interface{}
    20  
    21  func BenchmarkChunkSliceIter(b *testing.B) {
    22  	var r [][]int
    23  	b.ReportAllocs()
    24  	b.ResetTimer()
    25  	for n := 0; n < b.N; n++ {
    26  		// always record the result of Fib to prevent
    27  		// the compiler eliminating the function call
    28  		set := rand.Intn(10)
    29  		ChunkSliceIter(sampleDataInts[set], 16, func(p []int) int {
    30  			// normally we would do stuff in here
    31  			size := len(p)
    32  			if size != chunkSize {
    33  				return -1
    34  			}
    35  			return size
    36  		})
    37  	}
    38  	// always store the result to a package level variable
    39  	// so the compiler cannot eliminate the Benchmark itself.
    40  	result = r
    41  }
    42  
    43  func BenchmarkChunkSliceV1(b *testing.B) {
    44  	var r [][]int
    45  	b.ReportAllocs()
    46  	b.ResetTimer()
    47  	for n := 0; n < b.N; n++ {
    48  		// always record the result of Fib to prevent
    49  		// the compiler eliminating the function call
    50  		set := rand.Intn(10)
    51  		r = ChunkSliceV1(sampleDataInts[set], 16)
    52  	}
    53  	// always store the result to a package level variable
    54  	// so the compiler cannot eliminate the Benchmark itself.
    55  	result = r
    56  }
    57  
    58  func BenchmarkChunkSliceV2(b *testing.B) {
    59  	var r [][]int
    60  	b.ReportAllocs()
    61  	b.ResetTimer()
    62  	for n := 0; n < b.N; n++ {
    63  		// always record the result of Fib to prevent
    64  		// the compiler eliminating the function call
    65  		set := rand.Intn(10)
    66  		r = ChunkSliceV2(sampleDataInts[set], 16)
    67  	}
    68  	// always store the result to a package level variable
    69  	// so the compiler cannot eliminate the Benchmark itself.
    70  	result = r
    71  }
    72  
    73  func TestNewWriter(t *testing.T) {
    74  	b := new(bytes.Buffer)
    75  	w := NewWriter(b)
    76  	fmt.Println(Info(w, b))
    77  }
    78  
    79  func TestWriter_Write(t *testing.T) {
    80  	b := new(bytes.Buffer)
    81  	w := NewWriter(b)
    82  	n, err := w.Write([]byte("entry 1: this is just a test. this is entry number one."))
    83  	if err != nil {
    84  		t.Error(err)
    85  	}
    86  	fmt.Printf("wrote %d bytes.\n", n)
    87  	fmt.Printf("%s", Info(w, b))
    88  }
    89  
    90  func TestWriter_WriteMultiple(t *testing.T) {
    91  	b := new(bytes.Buffer)
    92  	w := NewWriter(b)
    93  	n, err := w.Write([]byte("entry 1: this is just a test. this is entry number one."))
    94  	if err != nil {
    95  		t.Error(err)
    96  	}
    97  	n, err = w.Write([]byte("entry 2: this is entry two"))
    98  	if err != nil {
    99  		t.Error(err)
   100  	}
   101  	fmt.Printf("wrote %d bytes.\n", n)
   102  	fmt.Printf("%s", Info(w, b))
   103  }
   104  
   105  func TestWriter_WriteOverflowChunk(t *testing.T) {
   106  	b := new(bytes.Buffer)
   107  	w := NewWriter(b)
   108  	var data []byte
   109  	for i := 0; i < blocksPerChunk; i++ {
   110  		data = append(data, []byte(fmt.Sprintf("entry %d: this is just a test. Not sure how long it should be.", i))...)
   111  	}
   112  	n, err := w.Write(data)
   113  	if err != nil {
   114  		t.Logf("got: %v, expected: %v\n", err, err)
   115  	} else {
   116  		t.Error("did not get any error, but expected one")
   117  	}
   118  	fmt.Printf("wrote %d bytes.\n", n)
   119  }