github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/perf/codec-perf-rig/main.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/binary"
    10  	"fmt"
    11  	"time"
    12  
    13  	"github.com/attic-labs/noms/go/chunks"
    14  	"github.com/attic-labs/noms/go/d"
    15  	"github.com/attic-labs/noms/go/datas"
    16  	"github.com/attic-labs/noms/go/types"
    17  	"github.com/attic-labs/noms/go/util/profile"
    18  
    19  	"github.com/attic-labs/kingpin"
    20  )
    21  
    22  var (
    23  	count    = kingpin.Flag("count", "number of elements").Default("100000").Uint64()
    24  	blobSize = kingpin.Flag("blobsize", "size of blob of create").Default(fmt.Sprintf("%d", 2<<24 /* 32MB */)).Uint64()
    25  )
    26  
    27  const numberSize = uint64(8)
    28  const strPrefix = "i am a 32 bytes.....%12d"
    29  const stringSize = uint64(32)
    30  const boolSize = uint64(1)
    31  const structSize = uint64(64)
    32  
    33  func main() {
    34  	profile.RegisterProfileFlags(kingpin.CommandLine)
    35  	kingpin.Parse()
    36  
    37  	buildCount := *count
    38  	insertCount := buildCount
    39  	defer profile.MaybeStartProfile().Stop()
    40  
    41  	collectionTypes := []string{"List", "Set", "Map"}
    42  	buildFns := []buildCollectionFn{buildList, buildSet, buildMap}
    43  	buildIncrFns := []buildCollectionFn{buildListIncrementally, buildSetIncrementally, buildMapIncrementally}
    44  	readFns := []readCollectionFn{readList, readSet, readMap}
    45  
    46  	elementTypes := []string{"numbers (8 B)", "strings (32 B)", "structs (64 B)"}
    47  	elementSizes := []uint64{numberSize, stringSize, structSize}
    48  	valueFns := []createValueFn{createNumber, createString, createStruct}
    49  
    50  	for i, colType := range collectionTypes {
    51  		fmt.Printf("Testing %s: \t\tbuild %d\t\t\tscan %d\t\t\tinsert %d\n", colType, buildCount, buildCount, insertCount)
    52  
    53  		for j, elementType := range elementTypes {
    54  			valueFn := valueFns[j]
    55  
    56  			// Build One-Time
    57  			storage := &chunks.MemoryStorage{}
    58  			db := datas.NewDatabase(storage.NewView())
    59  			ds := db.GetDataset("test")
    60  			t1 := time.Now()
    61  			col := buildFns[i](db, buildCount, valueFn)
    62  			ds, err := db.CommitValue(ds, col)
    63  			d.Chk.NoError(err)
    64  			buildDuration := time.Since(t1)
    65  
    66  			// Read
    67  			t1 = time.Now()
    68  			col = ds.HeadValue().(types.Collection)
    69  			readFns[i](col)
    70  			readDuration := time.Since(t1)
    71  
    72  			// Build Incrementally
    73  			storage = &chunks.MemoryStorage{}
    74  			db = datas.NewDatabase(storage.NewView())
    75  			ds = db.GetDataset("test")
    76  			t1 = time.Now()
    77  			col = buildIncrFns[i](db, insertCount, valueFn)
    78  			ds, err = db.CommitValue(ds, col)
    79  			d.Chk.NoError(err)
    80  			incrDuration := time.Since(t1)
    81  
    82  			elementSize := elementSizes[j]
    83  			buildSize := elementSize * buildCount
    84  			incrSize := elementSize * insertCount
    85  
    86  			fmt.Printf("%s\t\t%s\t\t%s\t\t%s\n", elementType, rate(buildDuration, buildSize), rate(readDuration, buildSize), rate(incrDuration, incrSize))
    87  		}
    88  		fmt.Println()
    89  	}
    90  
    91  	fmt.Printf("Testing Blob: \t\tbuild %d MB\t\t\tscan %d MB\n", *blobSize/1000000, *blobSize/1000000)
    92  
    93  	storage := &chunks.MemoryStorage{}
    94  	db := datas.NewDatabase(storage.NewView())
    95  	ds := db.GetDataset("test")
    96  
    97  	blobBytes := makeBlobBytes(*blobSize)
    98  	t1 := time.Now()
    99  	blob := types.NewBlob(db, bytes.NewReader(blobBytes))
   100  	db.CommitValue(ds, blob)
   101  	buildDuration := time.Since(t1)
   102  
   103  	db = datas.NewDatabase(storage.NewView())
   104  	ds = db.GetDataset("test")
   105  	t1 = time.Now()
   106  	blob = ds.HeadValue().(types.Blob)
   107  	buff := &bytes.Buffer{}
   108  	blob.Copy(buff)
   109  	outBytes := buff.Bytes()
   110  	readDuration := time.Since(t1)
   111  	d.PanicIfFalse(bytes.Compare(blobBytes, outBytes) == 0)
   112  	fmt.Printf("\t\t\t%s\t\t%s\n\n", rate(buildDuration, *blobSize), rate(readDuration, *blobSize))
   113  }
   114  
   115  func rate(d time.Duration, size uint64) string {
   116  	return fmt.Sprintf("%d ms (%.2f MB/s)", uint64(d)/1000000, float64(size)*1000/float64(d))
   117  }
   118  
   119  type createValueFn func(i uint64) types.Value
   120  type buildCollectionFn func(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection
   121  type readCollectionFn func(value types.Collection)
   122  
   123  func makeBlobBytes(byteLength uint64) []byte {
   124  	buff := &bytes.Buffer{}
   125  	counter := uint64(0)
   126  	for uint64(buff.Len()) < byteLength {
   127  		binary.Write(buff, binary.BigEndian, counter)
   128  		counter++
   129  	}
   130  	return buff.Bytes()
   131  }
   132  
   133  func createString(i uint64) types.Value {
   134  	return types.String(fmt.Sprintf("%s%d", strPrefix, i))
   135  }
   136  
   137  func createNumber(i uint64) types.Value {
   138  	return types.Number(i)
   139  }
   140  
   141  var structType = types.MakeStructType("S1",
   142  	types.StructField{
   143  		Name: "bool",
   144  		Type: types.BoolType,
   145  	},
   146  	types.StructField{
   147  		Name: "num",
   148  		Type: types.NumberType,
   149  	},
   150  	types.StructField{
   151  		Name: "str",
   152  		Type: types.StringType,
   153  	},
   154  )
   155  
   156  var structTemplate = types.MakeStructTemplate("S1", []string{"bool", "num", "str"})
   157  
   158  func createStruct(i uint64) types.Value {
   159  	return structTemplate.NewStruct([]types.Value{
   160  		types.Bool(i%2 == 0), // "bool"
   161  		types.Number(i),      // "num"
   162  		types.String(fmt.Sprintf("i am a 55 bytes............................%12d", i)), // "str"
   163  	})
   164  }
   165  
   166  func buildList(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   167  	values := make([]types.Value, count)
   168  	for i := uint64(0); i < count; i++ {
   169  		values[i] = createFn(i)
   170  	}
   171  
   172  	return types.NewList(vrw, values...)
   173  }
   174  
   175  func buildListIncrementally(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   176  	l := types.NewList(vrw).Edit()
   177  	for i := uint64(0); i < count; i++ {
   178  		l.Append(createFn(i))
   179  	}
   180  
   181  	return l.List()
   182  }
   183  
   184  func readList(c types.Collection) {
   185  	c.(types.List).IterAll(func(v types.Value, idx uint64) {
   186  	})
   187  }
   188  
   189  func buildSet(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   190  	values := make([]types.Value, count)
   191  	for i := uint64(0); i < count; i++ {
   192  		values[i] = createFn(i)
   193  	}
   194  
   195  	return types.NewSet(vrw, values...)
   196  }
   197  
   198  func buildSetIncrementally(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   199  	s := types.NewSet(vrw).Edit()
   200  	for i := uint64(0); i < count; i++ {
   201  		s.Insert(createFn(i))
   202  	}
   203  
   204  	return s.Set()
   205  }
   206  
   207  func readSet(c types.Collection) {
   208  	c.(types.Set).IterAll(func(v types.Value) {
   209  	})
   210  }
   211  
   212  func buildMap(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   213  	values := make([]types.Value, count*2)
   214  	for i := uint64(0); i < count*2; i++ {
   215  		values[i] = createFn(i)
   216  	}
   217  
   218  	return types.NewMap(vrw, values...)
   219  }
   220  
   221  func buildMapIncrementally(vrw types.ValueReadWriter, count uint64, createFn createValueFn) types.Collection {
   222  	me := types.NewMap(vrw).Edit()
   223  
   224  	for i := uint64(0); i < count*2; i += 2 {
   225  		me.Set(createFn(i), createFn(i+1))
   226  	}
   227  
   228  	return me.Map()
   229  }
   230  
   231  func readMap(c types.Collection) {
   232  	c.(types.Map).IterAll(func(k types.Value, v types.Value) {
   233  	})
   234  }