-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from orbs-network/bugfix/memory-leak-test
Add memory leak test
- Loading branch information
Showing
5 changed files
with
98 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/sh | ||
|
||
rm -rf /tmp/*.prof | ||
|
||
go test ./test/_manual -count 1 > test.out | ||
|
||
go tool pprof --inuse_space -nodecount 10 -weblist orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-tx-before.prof /tmp/mem-tx-after.prof | ||
go tool pprof --inuse_space -nodecount 20 -weblist orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-shutdown-before.prof /tmp/mem-shutdown-after.prof | ||
|
||
echo "" | ||
echo "" | ||
echo "TestMemoryLeaks_AfterSomeTransactions:" | ||
echo "" | ||
|
||
go tool pprof --inuse_space -nodecount 10 -top -show orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-tx-before.prof /tmp/mem-tx-after.prof | ||
|
||
echo "" | ||
echo "" | ||
echo "TestMemoryLeaks_OnSystemShutdown:" | ||
echo "" | ||
|
||
go tool pprof --inuse_space -nodecount 20 -top --base /tmp/mem-shutdown-before.prof /tmp/mem-shutdown-after.prof |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
#!/bin/sh | ||
|
||
rm -rf /tmp/*.prof | ||
go test ./test/acceptance -tags memoryleak -run TestMemoryLeaks -count 1 > test.out | ||
|
||
go test ./test/_manual -count 1 > test.out | ||
export EXIT_CODE=$? | ||
|
||
go tool pprof --inuse_space -nodecount 10 -weblist orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-tx-before.prof /tmp/mem-tx-after.prof | ||
go tool pprof --inuse_space -nodecount 20 -weblist orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-shutdown-before.prof /tmp/mem-shutdown-after.prof | ||
if [ $EXIT_CODE != 0 ]; then | ||
echo "Test failed! Found leaking memory" | ||
|
||
echo "" | ||
echo "" | ||
echo "TestMemoryLeaks_AfterSomeTransactions:" | ||
echo "" | ||
echo "" | ||
echo "" | ||
echo "****** Memory delta:" | ||
echo "" | ||
go tool pprof --inuse_space -nodecount 10 -top --base /tmp/mem-shutdown-before.prof /tmp/mem-shutdown-after.prof | ||
|
||
go tool pprof --inuse_space -nodecount 10 -top -show orbs-network-go -hide /orbs-network-go/test/ --base /tmp/mem-tx-before.prof /tmp/mem-tx-after.prof | ||
|
||
echo "" | ||
echo "" | ||
echo "TestMemoryLeaks_OnSystemShutdown:" | ||
echo "" | ||
|
||
go tool pprof --inuse_space -nodecount 20 -top --base /tmp/mem-shutdown-before.prof /tmp/mem-shutdown-after.prof | ||
exit $EXIT_CODE | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//+build memoryleak | ||
|
||
package acceptance | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"runtime" | ||
"runtime/pprof" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// this test should not run in parallel with any other test (even package parallel) since it's examining shared global system state (num goroutines) | ||
// if another test is running, the other test may create goroutines which we may mistake as leaks because the numbers won't add up | ||
// therefore, this file is marked on top with a build flag ("memoryleak") meaning without this flag it won't build or run | ||
// to run this test, add to the go command "-tags memoryleak", this is done in test.sh while making sure it's the only test running | ||
func TestMemoryLeaks_OnSystemShutdown(t *testing.T) { | ||
|
||
before, _ := os.Create("/tmp/mem-shutdown-before.prof") | ||
defer before.Close() | ||
after, _ := os.Create("/tmp/mem-shutdown-after.prof") | ||
defer after.Close() | ||
|
||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDuplicatingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDuplicatingRandomMessages) | ||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDroppingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDroppingRandomMessages) | ||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDelayingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDelayingRandomMessages) | ||
|
||
runtime.GC() | ||
runtime.GC() | ||
runtime.GC() | ||
runtime.GC() | ||
time.Sleep(50 * time.Millisecond) | ||
|
||
memUsageBefore := getMemUsage() | ||
pprof.WriteHeapProfile(before) | ||
|
||
for i := 0; i < 10; i++ { | ||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDuplicatingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDuplicatingRandomMessages) | ||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDroppingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDroppingRandomMessages) | ||
t.Run("TestCreateGazillionTransactionsWhileTransportIsDelayingRandomMessages", TestCreateGazillionTransactionsWhileTransportIsDelayingRandomMessages) | ||
} | ||
|
||
runtime.GC() | ||
runtime.GC() | ||
runtime.GC() | ||
runtime.GC() | ||
time.Sleep(50 * time.Millisecond) | ||
|
||
memUsageAfter := getMemUsage() | ||
pprof.WriteHeapProfile(after) | ||
|
||
require.InDelta(t, memUsageAfter, memUsageBefore, 0.1*float64(memUsageBefore), "added memory should be around than 10%, compare /tmp/mem-shutdown-before.prof and /tmp/mem-shutdown-after.prof to see memory consumers") | ||
} | ||
|
||
func getMemUsage() uint64 { | ||
var m runtime.MemStats | ||
runtime.ReadMemStats(&m) | ||
|
||
return m.Alloc | ||
} |