test compares normal vs canceled compaction times

Signed-off-by: Krasi Georgiev <kgeorgie@redhat.com>
This commit is contained in:
Krasi Georgiev 2019-01-24 12:48:56 +02:00
parent 9874377ead
commit 9638c13ec8

View file

@ -15,6 +15,7 @@ package tsdb
import ( import (
"context" "context"
"fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
@ -747,34 +748,70 @@ func TestDisableAutoCompactions(t *testing.T) {
// TestCancelCompactions ensures that when the db is closed // TestCancelCompactions ensures that when the db is closed
// any running compaction is cancelled to unblock closing the db. // any running compaction is cancelled to unblock closing the db.
func TestCancelCompactions(t *testing.T) { func TestCancelCompactions(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "test") createTestDb := func() (*DB, func()) {
testutil.Ok(t, err) tmpdir, err := ioutil.TempDir("", "testCancelCompaction")
defer os.RemoveAll(tmpdir) testutil.Ok(t, err)
// Create some blocks to fall within the compaction range. // Create some blocks to fall within the compaction range.
createBlock(t, tmpdir, 3000, 0, 1000) createBlock(t, tmpdir, 4000, 0, 1000)
createBlock(t, tmpdir, 3000, 1000, 2000) createBlock(t, tmpdir, 4000, 1000, 2000)
createBlock(t, tmpdir, 1, 2000, 2001) // The most recent block is ignored so can be e small one. createBlock(t, tmpdir, 1, 2000, 2001) // The most recent block is ignored so can be e small one.
db, err := Open(tmpdir, log.NewNopLogger(), nil, &Options{BlockRanges: []int64{1, 2000}})
testutil.Ok(t, err)
testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch")
testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch")
db, err := Open(tmpdir, log.NewNopLogger(), nil, &Options{BlockRanges: []int64{1, 2000}}) return db, func() {
testutil.Ok(t, err) os.RemoveAll(tmpdir)
db.compactc <- struct{}{} // Trigger a compaction.
dbClosed := make(chan struct{})
for {
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
time.Sleep(3 * time.Millisecond)
go func() {
testutil.Ok(t, db.Close())
close(dbClosed)
}()
break
} }
} }
// First lets mesure the compaction time without interupting it.
var timeCompactionUninterrupted time.Duration
{
db, delete := createTestDb()
defer delete()
db.compactc <- struct{}{} // Trigger a compaction.
var start time.Time
for {
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
start = time.Now()
break
}
time.Sleep(3 * time.Millisecond)
}
start := time.Now() for {
<-dbClosed if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran) == 1 {
actT := time.Since(start) timeCompactionUninterrupted = time.Since(start)
expT := time.Duration(100000000) break
testutil.Assert(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT) }
time.Sleep(3 * time.Millisecond)
}
}
// Closing the db in the middle of compaction should take half the time.
{
db, delete := createTestDb()
defer delete()
db.compactc <- struct{}{} // Trigger a compaction.
dbClosed := make(chan struct{})
for {
if prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) > 0 {
time.Sleep(3 * time.Millisecond)
go func() {
testutil.Ok(t, db.Close())
close(dbClosed)
}()
break
}
}
start := time.Now()
<-dbClosed
actT := time.Since(start)
fmt.Println(timeCompactionUninterrupted)
fmt.Println(actT)
expT := time.Duration(timeCompactionUninterrupted / 2)
testutil.Assert(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT)
}
} }