github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/aq/example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"time"
     7  
     8  	"github.com/egonelbre/exp/aq"
     9  )
    10  
    11  const MaxSize = 512 << 20
    12  
    13  func main() {
    14  	db, err := aq.New("example.db", MaxSize)
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	defer db.Close()
    19  
    20  	const (
    21  		minWrite = 1 << 8
    22  		maxWrite = 1 << 12
    23  	)
    24  
    25  	empty := make([]byte, maxWrite)
    26  	written := 0
    27  	count := 0
    28  
    29  	start := time.Now()
    30  	for {
    31  		n := rand.Intn(maxWrite-minWrite) + minWrite
    32  		_, err := db.Write(empty[:n])
    33  		if err != nil {
    34  			break
    35  		}
    36  
    37  		written += n
    38  		count++
    39  	}
    40  	elapsed := time.Since(start)
    41  
    42  	mb := float64(written) / float64(1<<20)
    43  	fmt.Printf("Write %.3fMB in %v\n", mb, elapsed)
    44  	fmt.Printf("      %.3fMB/s\n", mb/elapsed.Seconds())
    45  	fmt.Printf("      %v entries\n", count)
    46  
    47  	start = time.Now()
    48  	db.Flush()
    49  	elapsed = time.Since(start)
    50  	fmt.Printf("Flush %v\n", elapsed)
    51  
    52  	start = time.Now()
    53  	read := 0
    54  	count = 0
    55  	sum := 0
    56  	it := db.Iterate()
    57  	for it.Next() {
    58  		b := it.Bytes()
    59  		n := len(b)
    60  		read += n
    61  		for _, v := range b {
    62  			sum += int(v)
    63  		}
    64  		count++
    65  	}
    66  	elapsed = time.Since(start)
    67  
    68  	mb = float64(read) / float64(1<<20)
    69  	fmt.Printf("Read  %.3fMB in %v\n", mb, elapsed)
    70  	fmt.Printf("      %.3fMB/s\n", mb/elapsed.Seconds())
    71  	fmt.Printf("      %v entries\n", count)
    72  }