diff --git a/builtin_map_test.go b/builtin_map_test.go index 948c97c2..e8b61bba 100644 --- a/builtin_map_test.go +++ b/builtin_map_test.go @@ -253,33 +253,33 @@ func TestMapObjectMemUsage(t *testing.T) { expectedNewMem uint64 errExpected error }{ - // { - // name: "mem below threshold", - // mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}), - // mo: &mapObject{ - // m: &orderedMap{ - // hashTable: map[uint64]*mapEntry{ - // 1: { - // key: vm.ToValue("key"), - // value: vm.ToValue("value"), - // }, - // }, - // }, - // }, - // // baseObject + (len(key) + overhead) + (len(value) + overhead) - // expectedMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString), - // // baseObject + (len(key) + overhead) + (len(value) + overhead) - // expectedNewMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString), - // errExpected: nil, - // }, - // { - // name: "mem is SizeEmptyStruct given a nil map object", - // mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}), - // mo: nil, - // expectedMem: SizeEmptyStruct, - // expectedNewMem: SizeEmptyStruct, - // errExpected: nil, - // }, + { + name: "mem below threshold", + mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}), + mo: &mapObject{ + m: &orderedMap{ + hashTable: map[uint64]*mapEntry{ + 1: { + key: vm.ToValue("key"), + value: vm.ToValue("value"), + }, + }, + }, + }, + // baseObject + (len(key) + overhead) + (len(value) + overhead) + expectedMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString), + // baseObject + (len(key) + overhead) + (len(value) + overhead) + expectedNewMem: SizeEmptyStruct + (3 + SizeString) + (5 + SizeString), + errExpected: nil, + }, + { + name: "mem is SizeEmptyStruct given a nil map object", + mu: NewMemUsageContext(vm, 88, 5000, 50, 50, TestNativeMemUsageChecker{}), + mo: nil, + expectedMem: SizeEmptyStruct, + expectedNewMem: SizeEmptyStruct, + errExpected: nil, + }, { name: "mem way above threshold returns first crossing of threshold", mu: NewMemUsageContext(vm, 88, 100, 50, 50, TestNativeMemUsageChecker{}), diff --git a/object_goslice_test.go b/object_goslice_test.go index 0a8ac547..c191f1b2 100644 --- a/object_goslice_test.go +++ b/object_goslice_test.go @@ -298,6 +298,19 @@ func TestGoSliceMemUsage(t *testing.T) { expectedNewMem: SizeEmptyStruct + SizeInt*2, errExpected: nil, }, + { + name: "should account for baseObject only given a nil data field", + val: &objectGoSlice{ + baseObject: baseObject{ + val: &Object{runtime: vm}, + }, + }, + // overhead + expectedMem: SizeEmptyStruct, + // overhead + expectedNewMem: SizeEmptyStruct, + errExpected: nil, + }, { name: "should account for each value given a slice with go ints", val: &objectGoSlice{ @@ -342,8 +355,8 @@ func TestGoSliceMemUsage(t *testing.T) { }, }, }, - // overhead + (value + len("length") with string overhead + "length".value + prototype + ints) - expectedMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2), + // default + default since we don't account for objectGoSlice in (*Object).MemUsage + expectedMem: SizeEmptyStruct + SizeEmptyStruct, // overhead + (value + len("length") with string overhead + "length".value + prototype + ints) expectedNewMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2), errExpected: nil, @@ -361,8 +374,8 @@ func TestGoSliceMemUsage(t *testing.T) { }, }, }, - // overhead + (value + len("length") with string overhead + "length".value + prototype + ints) - expectedMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2), + // default + default since we don't account for objectGoSlice in (*Object).MemUsage + expectedMem: SizeEmptyStruct + SizeEmptyStruct, // overhead + (value + len("length") with string overhead + "length".value + prototype + ints) expectedNewMem: SizeEmptyStruct + (SizeEmptyStruct + (6 + SizeString) + SizeEmptyStruct + (SizeEmptyStruct + SizeEmptyStruct) + SizeNumber*2), errExpected: nil, @@ -371,7 +384,7 @@ func TestGoSliceMemUsage(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - total, newTotal, err := tc.val.MemUsage(NewMemUsageContext(vm, 100, 100, 100, 100, nil)) + total, newTotal, err := tc.val.MemUsage(NewMemUsageContext(vm, 100, 100000, 100, 100, nil)) if err != tc.errExpected { t.Fatalf("Unexpected error. Actual: %v Expected: %v", err, tc.errExpected) } diff --git a/value.go b/value.go index 15220bca..8a41c8fb 100644 --- a/value.go +++ b/value.go @@ -1182,6 +1182,12 @@ func (o *Object) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage ui return SizeEmptyStruct, SizeEmptyStruct, nil case *objectGoSliceReflect: return SizeEmptyStruct, SizeEmptyStruct, nil + case *objectGoMapSimple: + _, newMemUsage, err := x.MemUsage(ctx) + return SizeEmptyStruct, newMemUsage, err + case *objectGoSlice: + _, newMemUsage, err := x.MemUsage(ctx) + return SizeEmptyStruct, newMemUsage, err default: r, ok := x.(MemUsageReporter) if !ok { diff --git a/value_test.go b/value_test.go index 645ece8b..604dce1e 100644 --- a/value_test.go +++ b/value_test.go @@ -201,6 +201,7 @@ func TestValuePropertyMemUsage(t *testing.T) { } func TestObjectMemUsage(t *testing.T) { + vm := New() tests := []struct { name string val *Object @@ -244,19 +245,49 @@ func TestObjectMemUsage(t *testing.T) { errExpected: nil, }, { - name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoMapSimple", + name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoMapSimple empty", val: &Object{self: &objectGoMapSimple{}}, expectedMem: SizeEmptyStruct, expectedNewMem: SizeEmptyStruct, errExpected: nil, }, { - name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSlice", + name: "should have different values given an Object with self of type objectGoMapSimple non empty", + val: &Object{self: &objectGoMapSimple{ + baseObject: baseObject{ + val: &Object{runtime: vm}, + }, + data: map[string]interface{}{ + "test0": valueInt(99), + "test1": valueInt(99), + }, + }}, + expectedMem: SizeEmptyStruct, + expectedNewMem: SizeEmptyStruct + ((5+SizeString)+SizeInt)*2, + errExpected: nil, + }, + { + name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSlice empty", val: &Object{self: &objectGoSlice{}}, expectedMem: SizeEmptyStruct, expectedNewMem: SizeEmptyStruct, errExpected: nil, }, + { + name: "should have different values given an Object with self of type objectGoSlice non empty", + val: &Object{self: &objectGoSlice{ + baseObject: baseObject{ + val: &Object{runtime: vm}, + }, + data: &[]interface{}{ + valueInt(99), + valueInt(99), + }, + }}, + expectedMem: SizeEmptyStruct, + expectedNewMem: SizeEmptyStruct + SizeInt*2, + errExpected: nil, + }, { name: "should have a value of SizeEmptyStruct given an Object with self of type objectGoSliceReflect", val: &Object{self: &objectGoSliceReflect{}},