Index: impl/memory/datastore_test.go |
diff --git a/impl/memory/datastore_test.go b/impl/memory/datastore_test.go |
index c9580f0f7f1af8cad4585884b1ab52948557a426..529494159ef574f6bb30ad672d63cc08cae1bc73 100644 |
--- a/impl/memory/datastore_test.go |
+++ b/impl/memory/datastore_test.go |
@@ -6,7 +6,9 @@ package memory |
import ( |
"errors" |
+ "flag" |
"fmt" |
+ "runtime" |
"testing" |
"time" |
@@ -20,6 +22,8 @@ import ( |
. "github.com/smartystreets/goconvey/convey" |
) |
+var enableMemoryTest = flag.Bool("test.memusage", false, "Enable memory usage test.") |
+ |
type MetaGroup struct { |
_id int64 `gae:"$id,1"` |
_kind string `gae:"$kind,__entity_group__"` |
@@ -815,3 +819,55 @@ func TestAddIndexes(t *testing.T) { |
}) |
}) |
} |
+ |
+// TestMemoryUsage performs a series of datastore operations, profiling the |
+// memory usage before and after. |
+// |
+// This is an expensive test, and it's not really sufficiently deterministic to |
+// be enabled by default. If you want to run this, use the '-test.memusage' |
+// flag. |
+func TestMemoryUsage(t *testing.T) { |
+ if !*enableMemoryTest { |
+ return |
+ } |
+ |
+ Convey("A datastore instance does not leak memory.", t, func() { |
+ const rounds, items = 10, 200 |
+ |
+ // Initial memory stats. |
+ var ms runtime.MemStats |
+ runtime.MemProfileRate = 1 |
+ runtime.GC() |
+ runtime.ReadMemStats(&ms) |
+ initialAlloc := ms.Alloc |
+ |
+ // Run the memory test entirely within its own function scope. This will |
+ // provide a hard boundary where all associated resources should be |
+ // free-able. |
+ func() { |
+ c := Use(context.Background()) |
+ for i := 0; i < rounds; i++ { |
+ toPut := make([]ds.PropertyMap, items) |
+ for j := range toPut { |
+ toPut[j] = ds.PropertyMap{ |
+ "$kind": ds.MkProperty(fmt.Sprintf("kind-%d", j)), |
+ "$id": ds.MkProperty(1), |
+ } |
+ } |
+ if err := ds.Put(c, toPut); err != nil { |
+ panic(err) |
+ } |
+ |
+ runtime.GC() |
+ } |
+ }() |
+ |
+ for i := 0; i < 3; i++ { |
+ time.Sleep(100 * time.Millisecond) |
+ runtime.GC() |
+ } |
+ runtime.ReadMemStats(&ms) |
+ |
+ t.Logf("Memory alloc difference is: %d", ms.Alloc-initialAlloc) |
+ }) |
+} |