summaryrefslogtreecommitdiffhomepage
path: root/pkg/p9/messages_test.go
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2019-04-29 15:32:45 -0700
committerShentubot <shentubot@google.com>2019-04-29 15:33:47 -0700
commitddab854b9a895603664fa4abfa525f6a29047083 (patch)
tree349b428a042c7e7e0d7de71e56a319f7df3ae29b /pkg/p9/messages_test.go
parent4d52a5520101a88424fb63dd99412a1db33fbd06 (diff)
Reduce memory allocations on serving path
Cache last used messages and reuse them for subsequent requests. If more messages are needed, they are created outside the cache on demand. PiperOrigin-RevId: 245836910 Change-Id: Icf099ddff95df420db8e09f5cdd41dcdce406c61
Diffstat (limited to 'pkg/p9/messages_test.go')
-rw-r--r--pkg/p9/messages_test.go46
1 files changed, 42 insertions, 4 deletions
diff --git a/pkg/p9/messages_test.go b/pkg/p9/messages_test.go
index 10a0587cf..513b30e8b 100644
--- a/pkg/p9/messages_test.go
+++ b/pkg/p9/messages_test.go
@@ -399,8 +399,9 @@ func TestEncodeDecode(t *testing.T) {
}
func TestMessageStrings(t *testing.T) {
- for typ, fn := range messageRegistry {
- if fn != nil {
+ for typ := range msgRegistry.factories {
+ entry := &msgRegistry.factories[typ]
+ if entry.create != nil {
name := fmt.Sprintf("%+v", typ)
t.Run(name, func(t *testing.T) {
defer func() { // Ensure no panic.
@@ -408,7 +409,7 @@ func TestMessageStrings(t *testing.T) {
t.Errorf("printing %s failed: %v", name, r)
}
}()
- m := fn()
+ m := entry.create()
_ = fmt.Sprintf("%v", m)
err := ErrInvalidMsgType{MsgType(typ)}
_ = err.Error()
@@ -426,5 +427,42 @@ func TestRegisterDuplicate(t *testing.T) {
}()
// Register a duplicate.
- register(MsgRlerror, func() message { return &Rlerror{} })
+ msgRegistry.register(MsgRlerror, func() message { return &Rlerror{} })
+}
+
+func TestMsgCache(t *testing.T) {
+ // Cache starts empty.
+ if got, want := len(msgRegistry.factories[MsgRlerror].cache), 0; got != want {
+ t.Errorf("Wrong cache size, got: %d, want: %d", got, want)
+ }
+
+ // Message can be created with an empty cache.
+ msg, err := msgRegistry.get(0, MsgRlerror)
+ if err != nil {
+ t.Errorf("msgRegistry.get(): %v", err)
+ }
+ if got, want := len(msgRegistry.factories[MsgRlerror].cache), 0; got != want {
+ t.Errorf("Wrong cache size, got: %d, want: %d", got, want)
+ }
+
+ // Check that message is added to the cache when returned.
+ msgRegistry.put(msg)
+ if got, want := len(msgRegistry.factories[MsgRlerror].cache), 1; got != want {
+ t.Errorf("Wrong cache size, got: %d, want: %d", got, want)
+ }
+
+ // Check that returned message is reused.
+ if got, err := msgRegistry.get(0, MsgRlerror); err != nil {
+ t.Errorf("msgRegistry.get(): %v", err)
+ } else if msg != got {
+ t.Errorf("Message not reused, got: %d, want: %d", got, msg)
+ }
+
+ // Check that cache doesn't grow beyond max size.
+ for i := 0; i < maxCacheSize+1; i++ {
+ msgRegistry.put(&Rlerror{})
+ }
+ if got, want := len(msgRegistry.factories[MsgRlerror].cache), maxCacheSize; got != want {
+ t.Errorf("Wrong cache size, got: %d, want: %d", got, want)
+ }
}