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