Skip to content

Commit

Permalink
update benchmarks and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
steviebps committed Jun 23, 2023
1 parent 398626a commit 1591f93
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 35 deletions.
61 changes: 33 additions & 28 deletions pkg/chamber.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package realm

import (
"encoding/json"
"sync"
)

Expand Down Expand Up @@ -56,16 +55,6 @@ func NewChamberEntry(c *Chamber, version string) *ChamberEntry {
}
}

// GetToggleValue returns the toggle with the specified toggleName at the specified version.
// Will return nil if the toggle does not exist
func (c *ChamberEntry) GetToggleValue(toggleName string) interface{} {
t := c.Get(toggleName)
if t == nil {
return nil
}
return t.GetValueAt(c.version)
}

// Get returns the toggle with the specified toggleName.
// Will return nil if the toggle does not exist
func (c *ChamberEntry) Get(toggleName string) *OverrideableToggle {
Expand All @@ -79,40 +68,56 @@ func (c *ChamberEntry) Get(toggleName string) *OverrideableToggle {

// StringValue retrieves a string by the key of the toggle
// and returns the default value if it does not exist and a bool on whether or not the toggle exists
func (c *ChamberEntry) StringValue(toggleKey string, defaultValue string) (string, bool) {
cStr, ok := c.GetToggleValue(toggleKey).(string)
func (c *ChamberEntry) StringValue(toggleKey string, defaultValue string) (string, error) {
t := c.Get(toggleKey)
if t == nil {
return defaultValue, &ErrToggleNotFound{toggleKey}
}
v, ok := t.StringValue(c.version, defaultValue)
if !ok {
return defaultValue, ok
return defaultValue, &ErrCouldNotConvertToggle{toggleKey, t.Type}
}
return cStr, ok
return v, nil
}

// BoolValue retrieves a bool by the key of the toggle
// and returns the default value if it does not exist and a bool on whether or not the toggle exists
func (c *ChamberEntry) BoolValue(toggleKey string, defaultValue bool) (bool, bool) {
cBool, ok := c.GetToggleValue(toggleKey).(bool)
func (c *ChamberEntry) BoolValue(toggleKey string, defaultValue bool) (bool, error) {
t := c.Get(toggleKey)
if t == nil {
return defaultValue, &ErrToggleNotFound{toggleKey}
}
v, ok := t.BoolValue(c.version, defaultValue)
if !ok {
return defaultValue, ok
return defaultValue, &ErrCouldNotConvertToggle{toggleKey, t.Type}
}
return cBool, ok
return v, nil
}

// Float64Value retrieves a float64 by the key of the toggle
// and returns the default value if it does not exist and a bool on whether or not the toggle exists
func (c *ChamberEntry) Float64Value(toggleKey string, defaultValue float64) (float64, bool) {
cFloat64, ok := c.GetToggleValue(toggleKey).(float64)
func (c *ChamberEntry) Float64Value(toggleKey string, defaultValue float64) (float64, error) {
t := c.Get(toggleKey)
if t == nil {
return defaultValue, &ErrToggleNotFound{toggleKey}
}
v, ok := t.Float64Value(c.version, defaultValue)
if !ok {
return defaultValue, ok
return defaultValue, &ErrCouldNotConvertToggle{toggleKey, t.Type}
}
return cFloat64, ok
return v, nil
}

// CustomValue retrieves a json.RawMessage by the key of the toggle
// and returns a bool on whether or not the toggle exists and is the proper type
func (c *ChamberEntry) CustomValue(toggleKey string, version string) (*json.RawMessage, bool) {
t, ok := c.GetToggleValue(toggleKey).(*json.RawMessage)
if !ok {
return nil, ok
func (c *ChamberEntry) CustomValue(toggleKey string, v any) error {
t := c.Get(toggleKey)
if t == nil {
return &ErrToggleNotFound{toggleKey}
}
err := t.CustomValue(c.version, v)
if err != nil {
return &ErrCouldNotConvertToggle{toggleKey, t.Type}
}
return t, ok
return nil
}
70 changes: 63 additions & 7 deletions pkg/chamber_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package realm

import (
"encoding/json"
"strconv"
"sync"
"testing"
Expand Down Expand Up @@ -59,8 +60,8 @@ func TestInheritWith(t *testing.T) {
}
}

func BenchmarkStringValue(b *testing.B) {
m := make(map[string]*OverrideableToggle)
func BenchmarkChamberStringValue(b *testing.B) {
m := make(map[string]*OverrideableToggle, 100000)
for i := 1; i < 10000; i++ {
m[strconv.Itoa(i)] = &OverrideableToggle{Toggle: &Toggle{Type: "string", Value: "string"}}
}
Expand All @@ -72,14 +73,16 @@ func BenchmarkStringValue(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
chamber.StringValue("1", "")
_, err := chamber.StringValue("1", "")
if err != nil {
b.Errorf("should not be failing benchmark with error: %v", err)
}
}
})
}

func BenchmarkBoolValue(b *testing.B) {

m := make(map[string]*OverrideableToggle)
func BenchmarkChamberBoolValue(b *testing.B) {
m := make(map[string]*OverrideableToggle, 100000)
for i := 1; i < 100000; i++ {
m[strconv.Itoa(i)] = &OverrideableToggle{Toggle: &Toggle{Type: "boolean", Value: false}}
}
Expand All @@ -90,7 +93,60 @@ func BenchmarkBoolValue(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
chamber.BoolValue("1", false)
_, err := chamber.BoolValue("1", false)
if err != nil {
b.Errorf("should not be failing benchmark with error: %v", err)
}
}
})
}

func BenchmarkChamberFloat64Value(b *testing.B) {
m := make(map[string]*OverrideableToggle, 100000)
for i := 1; i < 100000; i++ {
m[strconv.Itoa(i)] = &OverrideableToggle{Toggle: &Toggle{Type: "number", Value: float64(10)}}
}
chamber := NewChamberEntry(&Chamber{
Toggles: m,
}, "")

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, err := chamber.Float64Value("1", 100)
if err != nil {
b.Errorf("should not be failing benchmark with error: %v", err)
}
}
})
}

func BenchmarkChamberCustomValue(b *testing.B) {
type CustomStruct struct {
Test string
}

m := make(map[string]*OverrideableToggle, 100000)
for i := 0; i < 100000; i++ {
raw := json.RawMessage(`{"Test":"test"}`)
m[strconv.Itoa(i)] = &OverrideableToggle{Toggle: &Toggle{Type: "custom", Value: &raw}}
}

chamber := NewChamberEntry(&Chamber{
Toggles: m,
}, "")

b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var v CustomStruct
err := chamber.CustomValue("1", &v)
if err != nil {
b.Errorf("should not be failing benchmark with error: %v", err)
}
if i == 99999 {
i = 0
}
}
})
}

0 comments on commit 1591f93

Please sign in to comment.