mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network/stream: reduce memory consumption when testing data races
This commit is contained in:
parent
b151dc25a6
commit
4f5807be94
4 changed files with 104 additions and 5 deletions
|
|
@ -18,6 +18,7 @@ package stream
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -92,6 +93,15 @@ func TestSyncingViaGlobalSync(t *testing.T) {
|
||||||
if *longrunning {
|
if *longrunning {
|
||||||
chnkCnt = []int{1, 8, 32, 256, 1024}
|
chnkCnt = []int{1, 8, 32, 256, 1024}
|
||||||
nodeCnt = []int{16, 32, 64, 128, 256}
|
nodeCnt = []int{16, 32, 64, 128, 256}
|
||||||
|
} else if raceTest {
|
||||||
|
// TestSyncingViaGlobalSync allocates a lot of memory
|
||||||
|
// with race detector. By reducing the number of chunks
|
||||||
|
// and nodes, memory consumption is lower and data races
|
||||||
|
// are still checked, while correctness of syncing is
|
||||||
|
// tested with more chunks and nodes in regular (!race)
|
||||||
|
// tests.
|
||||||
|
chnkCnt = []int{4}
|
||||||
|
nodeCnt = []int{16}
|
||||||
} else {
|
} else {
|
||||||
//default test
|
//default test
|
||||||
chnkCnt = []int{4, 32}
|
chnkCnt = []int{4, 32}
|
||||||
|
|
@ -113,7 +123,23 @@ var simServiceMap = map[string]simulation.ServiceFunc{
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
|
var dir string
|
||||||
|
var store *state.DBStore
|
||||||
|
if raceTest {
|
||||||
|
// Use on-disk DBStore to reduce memory consumption in race tests.
|
||||||
|
dir, err = ioutil.TempDir("", "swarm-stream-")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
store, err = state.NewDBStore(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
store = state.NewInmemoryStore()
|
||||||
|
}
|
||||||
|
|
||||||
|
r := NewRegistry(addr.ID(), delivery, netStore, store, &RegistryOptions{
|
||||||
Retrieval: RetrievalDisabled,
|
Retrieval: RetrievalDisabled,
|
||||||
Syncing: SyncingAutoSubscribe,
|
Syncing: SyncingAutoSubscribe,
|
||||||
SyncUpdateDelay: 3 * time.Second,
|
SyncUpdateDelay: 3 * time.Second,
|
||||||
|
|
|
||||||
24
swarm/network/stream/stream_not_race_test.go
Normal file
24
swarm/network/stream/stream_not_race_test.go
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Copyright 2019 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// +build !race
|
||||||
|
|
||||||
|
package stream
|
||||||
|
|
||||||
|
// Provide a flag to reduce the scope of tests when running them
|
||||||
|
// with race detector. Some of the tests are doing a lot of allocations
|
||||||
|
// on the heap, and race detector uses much more memory to track them.
|
||||||
|
var raceTest = false
|
||||||
23
swarm/network/stream/stream_race_test.go
Normal file
23
swarm/network/stream/stream_race_test.go
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
// Copyright 2019 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// +build race
|
||||||
|
|
||||||
|
package stream
|
||||||
|
|
||||||
|
// Reduce the scope of some tests when running with race detector,
|
||||||
|
// as it raises the memory consumption significantly.
|
||||||
|
var raceTest = true
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -44,10 +45,16 @@ const dataChunkCount = 200
|
||||||
|
|
||||||
func TestSyncerSimulation(t *testing.T) {
|
func TestSyncerSimulation(t *testing.T) {
|
||||||
testSyncBetweenNodes(t, 2, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 2, dataChunkCount, true, 1)
|
||||||
|
// This test uses much more memory when running with
|
||||||
|
// race detector. Allow it to finish successfully by
|
||||||
|
// reducing it s scope, and still check for data races
|
||||||
|
// with the smallest number of nodes.
|
||||||
|
if !raceTest {
|
||||||
testSyncBetweenNodes(t, 4, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 4, dataChunkCount, true, 1)
|
||||||
testSyncBetweenNodes(t, 8, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 8, dataChunkCount, true, 1)
|
||||||
testSyncBetweenNodes(t, 16, dataChunkCount, true, 1)
|
testSyncBetweenNodes(t, 16, dataChunkCount, true, 1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createMockStore(globalStore mock.GlobalStorer, id enode.ID, addr *network.BzzAddr) (lstore storage.ChunkStore, datadir string, err error) {
|
func createMockStore(globalStore mock.GlobalStorer, id enode.ID, addr *network.BzzAddr) (lstore storage.ChunkStore, datadir string, err error) {
|
||||||
address := common.BytesToAddress(id.Bytes())
|
address := common.BytesToAddress(id.Bytes())
|
||||||
|
|
@ -80,7 +87,23 @@ func testSyncBetweenNodes(t *testing.T, nodes, chunkCount int, skipCheck bool, p
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := NewRegistry(addr.ID(), delivery, netStore, state.NewInmemoryStore(), &RegistryOptions{
|
var dir string
|
||||||
|
var store *state.DBStore
|
||||||
|
if raceTest {
|
||||||
|
// Use on-disk DBStore to reduce memory consumption in race tests.
|
||||||
|
dir, err = ioutil.TempDir("", "swarm-stream-")
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
store, err = state.NewDBStore(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
store = state.NewInmemoryStore()
|
||||||
|
}
|
||||||
|
|
||||||
|
r := NewRegistry(addr.ID(), delivery, netStore, store, &RegistryOptions{
|
||||||
Retrieval: RetrievalDisabled,
|
Retrieval: RetrievalDisabled,
|
||||||
Syncing: SyncingAutoSubscribe,
|
Syncing: SyncingAutoSubscribe,
|
||||||
SkipCheck: skipCheck,
|
SkipCheck: skipCheck,
|
||||||
|
|
@ -89,6 +112,9 @@ func testSyncBetweenNodes(t *testing.T, nodes, chunkCount int, skipCheck bool, p
|
||||||
cleanup = func() {
|
cleanup = func() {
|
||||||
r.Close()
|
r.Close()
|
||||||
clean()
|
clean()
|
||||||
|
if dir != "" {
|
||||||
|
os.RemoveAll(dir)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, cleanup, nil
|
return r, cleanup, nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue