Skip to content

Commit

Permalink
feat(tests): Run tests in parallel (#1572)
Browse files Browse the repository at this point in the history
Run tests in parallel. Break up the tests into 3 functions: manual, packages and root. Run those via GNU parallel. Moved some expensive tests to manual mode.

With these changes, the badger tests can now run in under 6 minutes on a fast machine.

Co-authored-by: Manish R Jain <[email protected]>
  • Loading branch information
rahulgurnani and manishrjain authored Nov 2, 2020
1 parent f1ca35f commit 774a99d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 35 deletions.
11 changes: 10 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func getTestOptions(dir string) Options {
WithMemTableSize(1 << 15).
WithBaseTableSize(1 << 15). // Force more compaction.
WithBaseLevelSize(4 << 15). // Force more compaction.
WithSyncWrites(false)
WithSyncWrites(false).
WithLoggingLevel(WARNING)
return opt
}

Expand Down Expand Up @@ -910,6 +911,10 @@ func TestIterateDeleted(t *testing.T) {
}

func TestIterateParallel(t *testing.T) {
if !*manual {
t.Skip("Skipping test meant to be run manually.")
return
}
key := func(account int) []byte {
var b [4]byte
binary.BigEndian.PutUint32(b[:], uint32(account))
Expand Down Expand Up @@ -1816,6 +1821,10 @@ func TestMinReadTs(t *testing.T) {
}

func TestGoroutineLeak(t *testing.T) {
if !*manual {
t.Skip("Skipping test meant to be run manually.")
return
}
test := func(t *testing.T, opt *Options) {
time.Sleep(1 * time.Second)
before := runtime.NumGoroutine()
Expand Down
8 changes: 4 additions & 4 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func TestPickSortTables(t *testing.T) {
}

func TestIteratePrefix(t *testing.T) {
if !*manual {
t.Skip("Skipping test meant to be run manually.")
return
}
testIteratorPrefix := func(t *testing.T, db *DB) {
bkey := func(i int) []byte {
return []byte(fmt.Sprintf("%04d", i))
Expand Down Expand Up @@ -199,14 +203,12 @@ func TestIteratePrefix(t *testing.T) {
}

t.Run("With Default options", func(t *testing.T) {
t.Parallel()
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
testIteratorPrefix(t, db)
})
})

t.Run("With Block Offsets in Cache", func(t *testing.T) {
t.Parallel()
opts := getTestOptions("")
opts.IndexCacheSize = 100 << 20
runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
Expand All @@ -215,7 +217,6 @@ func TestIteratePrefix(t *testing.T) {
})

t.Run("With Block Offsets and Blocks in Cache", func(t *testing.T) {
t.Parallel()
opts := getTestOptions("")
opts.BlockCacheSize = 100 << 20
opts.IndexCacheSize = 100 << 20
Expand All @@ -225,7 +226,6 @@ func TestIteratePrefix(t *testing.T) {
})

t.Run("With Blocks in Cache", func(t *testing.T) {
t.Parallel()
opts := getTestOptions("")
opts.BlockCacheSize = 100 << 20
runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
Expand Down
4 changes: 2 additions & 2 deletions merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,6 @@ func bytesToUint64(b []byte) uint64 {
}

// Merge function to add two uint64 numbers
func add(existing, new []byte) []byte {
return uint64ToBytes(bytesToUint64(existing) + bytesToUint64(new))
func add(existing, latest []byte) []byte {
return uint64ToBytes(bytesToUint64(existing) + bytesToUint64(latest))
}
6 changes: 4 additions & 2 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

bpb "github.com/dgraph-io/badger/v2/pb"
"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/ristretto/z"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -164,7 +163,6 @@ func TestStream(t *testing.T) {
require.Equal(t, 50, count, "Count mismatch for pred: %s", pred)
}
require.NoError(t, db.Close())
require.Equal(t, int64(0), z.NumAllocBytes())
}

func TestStreamWithThreadId(t *testing.T) {
Expand Down Expand Up @@ -214,6 +212,10 @@ func TestStreamWithThreadId(t *testing.T) {
}

func TestBigStream(t *testing.T) {
if !*manual {
t.Skip("Skipping test meant to be run manually.")
return
}
// Set the maxStreamSize to 1MB for the duration of the test so that the it can use a smaller
// dataset than it would otherwise need.
originalMaxStreamSize := maxStreamSize
Expand Down
72 changes: 46 additions & 26 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ set -e

go version

packages=$(go list ./... | grep github.com/dgraph-io/badger/v2/)

if [[ ! -z "$TEAMCITY_VERSION" ]]; then
export GOFLAGS="-json"
Expand Down Expand Up @@ -38,32 +37,53 @@ pushd badger
go build -v .
popd

# tags="-tags=jemalloc"
tags=""
tags="-tags=jemalloc"
# tags=""
InstallJemalloc

# Run the memory intensive tests first.
go test -v $tags -run='TestBigKeyValuePairs$' --manual=true
go test -v $tags -run='TestPushValueLogLimit' --manual=true
manual() {
echo "==> Running manual tests"
# Run the special Truncate test.
rm -rf p
go test -v $tags -run='TestTruncateVlogNoClose$' --manual=true
truncate --size=4096 p/000000.vlog
go test -v $tags -run='TestTruncateVlogNoClose2$' --manual=true
go test -v $tags -run='TestTruncateVlogNoClose3$' --manual=true
rm -rf p

# Run the special Truncate test.
rm -rf p
go test -v $tags -run='TestTruncateVlogNoClose$' --manual=true
truncate --size=4096 p/000000.vlog
go test -v $tags -run='TestTruncateVlogNoClose2$' --manual=true
go test -v $tags -run='TestTruncateVlogNoClose3$' --manual=true
rm -rf p
go test -v $tags -run='TestBigKeyValuePairs$' --manual=true
go test -v $tags -run='TestPushValueLogLimit' --manual=true
go test -v $tags -run='TestKeyCount' --manual=true
go test -v $tags -run='TestIteratePrefix' --manual=true
go test -v $tags -run='TestIterateParallel' --manual=true
go test -v $tags -run='TestBigStream' --manual=true
go test -v $tags -run='TestGoroutineLeak' --manual=true

InstallJemalloc
# Run the key count test for stream writer.
go test -v -tags jemalloc -run='TestKeyCount' --manual=true

# Run the normal tests.
echo "==> Starting tests.. "
# go test -timeout=25m -v -race github.com/dgraph-io/badger/v2/...
for pkg in $packages; do
echo "===> Testing $pkg"
go test $tags -timeout=25m -v -race $pkg
done

echo "===> Testing root level"
go test $tags -timeout=25m -v . -race
echo "==> DONE manual tests"
}

pkgs() {
packages=$(go list ./... | grep github.com/dgraph-io/badger/v2/)
echo "==> Running package tests for $packages"
for pkg in $packages; do
echo "===> Testing $pkg"
go test $tags -timeout=25m -v -race $pkg -parallel 16
done
echo "==> DONE package tests"
}

root() {
# Run the normal tests.
# go test -timeout=25m -v -race github.com/dgraph-io/badger/v2/...

echo "==> Running root level tests."
go test $tags -timeout=25m -v . -race -parallel 16
echo "==> DONE root level tests"
}

export -f manual
export -f pkgs
export -f root

parallel --progress --line-buffer ::: manual pkgs root

0 comments on commit 774a99d

Please sign in to comment.