Skip to content

Commit

Permalink
make this benchmark memory efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanashworth committed Sep 28, 2020
1 parent b12dea3 commit 056dfe6
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions internal/collection/fields_test.go
@@ -1,6 +1,8 @@
package collection

import (
"fmt"
"math"
"math/rand"
"strconv"
"testing"
Expand All @@ -9,7 +11,6 @@ import (
)

type fieldobject struct {
id string
fieldNames []string
fieldValues []float64
}
Expand All @@ -20,15 +21,11 @@ func BenchmarkItemFields(b *testing.B) {
var coll *Collection = New()

var k int = 100
var fillRatio float64 = 0.1

// Generate k fields, mapping to probability p.
var fields map[string]float64 = make(map[string]float64, k)
for i := 0; i < k; i++ {
field := strconv.Itoa(i)
prob := rand.Float64()

fields[field] = prob
}
// We have k fields, which will be filled with probability fillRatio.
// They are just 0...k-1 in string form so we don't actually need to
// map them.

// Generate n objects, just points.
json := `{"type":"Point","coordinates":[190,90]}`
Expand All @@ -37,34 +34,40 @@ func BenchmarkItemFields(b *testing.B) {
b.Fatal(err)
}

var objs []fieldobject
var objs []fieldobject = make([]fieldobject, b.N)

for i := 0; i < b.N; i++ {
var obj string = strconv.Itoa(i)

// set fields
var fieldNames []string
var values []float64
for field, prob := range fields {
if rand.Float64() > prob {
continue
}
var numFields int = int(math.Floor(float64(k) * fillRatio))

fieldNames = append(fieldNames, field)
values = append(values, 1.0)
var fieldNames []string = make([]string, numFields)
var values []float64 = make([]float64, numFields)

// set random fields at a random position.
// so start at a random position inside k, then increment so long
// as j is less than numFields. take the modulo to find insert position.
var start int = int(math.Floor(rand.Float64() * float64(k)))
for j := 0; j < numFields; j++ {
fieldNames[j] = strconv.Itoa((start + j) % k)
values[j] = 1.0
}

objs = append(objs, fieldobject{
id: obj,
objs[i] = fieldobject{
fieldNames: fieldNames,
fieldValues: values,
})
}
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
obj := objs[i]

coll.Set(obj.id, pt, obj.fieldNames, obj.fieldValues)
coll.Set(strconv.Itoa(i), pt, obj.fieldNames, obj.fieldValues)
}
b.StopTimer()

// Print out the memory usage of the first object.
bytesUsed := coll.TotalWeight()
fmt.Printf("%d bytes used, %f bytes per object, %f bytes per object per field\n",
bytesUsed, float64(bytesUsed) / float64(b.N), float64(bytesUsed) / (float64(b.N * k) * fillRatio))
}

0 comments on commit 056dfe6

Please sign in to comment.