Skip to content

Commit

Permalink
Add memory usage test case
Browse files Browse the repository at this point in the history
Test the memory usage overhead per object to make sure changes we make don't
drastically increase our memory consumption.

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Nov 6, 2024
1 parent 132edd6 commit ac41f4c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"expvar"
"fmt"
"log/slog"
"reflect"
"runtime"
"slices"
"strconv"
Expand Down Expand Up @@ -1055,6 +1056,52 @@ func TestDB_Initialization(t *testing.T) {
}
}

// TestDB_MemoryUsage tests how much memory in terms of bytes and allocated objects
// is used per object.
func TestDB_MemoryUsage(t *testing.T) {
const (
// maxBytesOverhead is the maximum number of additional bytes per database
// object.
maxBytesOverhead = 400

// maxObjectsOverhead defines the maximum number of heap objects allocated
// per each database object.
maxObjectsOverhead = 6.0
)

db, table, _ := newTestDB(t)
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)

const numObjects = 20000
txn := db.WriteTxn(table)
for i := range numObjects {
_, _, err := table.Insert(txn, testObject{ID: uint64(i)})
require.NoError(t, err)
}
txn.Commit()
runtime.GC()

runtime.ReadMemStats(&after)

objSize := int(reflect.TypeOf(testObject{}).Size())
objectsRatio := float64(after.HeapObjects-before.HeapObjects) / float64(numObjects)
perObjectBytes := (after.HeapInuse - before.HeapInuse) / numObjects
bytesOverhead := int(perObjectBytes) - objSize

t.Logf("objSize: %d, perObjectBytes: %d, objectsRatio: %f, bytesOverhead: %d",
objSize,
perObjectBytes,
objectsRatio,
bytesOverhead,
)

assert.LessOrEqual(t, bytesOverhead, maxBytesOverhead, "expected bytes ratio exceeded")
assert.LessOrEqual(t, objectsRatio, maxObjectsOverhead, "expected objects ratio exceeded")

}

func TestWriteJSON(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit ac41f4c

Please sign in to comment.