mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
560 lines
14 KiB
560 lines
14 KiB
7 years ago
|
// Copyright 2017 The Prometheus Authors
|
||
|
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
2 years ago
|
package wlog
|
||
7 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
6 years ago
|
"fmt"
|
||
4 years ago
|
"io"
|
||
7 years ago
|
"math/rand"
|
||
|
"os"
|
||
6 years ago
|
"path/filepath"
|
||
7 years ago
|
"testing"
|
||
|
|
||
4 years ago
|
client_testutil "github.com/prometheus/client_golang/prometheus/testutil"
|
||
4 years ago
|
"github.com/stretchr/testify/require"
|
||
4 years ago
|
"go.uber.org/goleak"
|
||
5 years ago
|
|
||
4 years ago
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
||
5 years ago
|
"github.com/prometheus/prometheus/util/testutil"
|
||
7 years ago
|
)
|
||
|
|
||
4 years ago
|
func TestMain(m *testing.M) {
|
||
|
goleak.VerifyTestMain(m)
|
||
|
}
|
||
|
|
||
6 years ago
|
// TestWALRepair_ReadingError ensures that a repair is run for an error
|
||
|
// when reading a record.
|
||
|
func TestWALRepair_ReadingError(t *testing.T) {
|
||
6 years ago
|
for name, test := range map[string]struct {
|
||
|
corrSgm int // Which segment to corrupt.
|
||
|
corrFunc func(f *os.File) // Func that applies the corruption.
|
||
|
intactRecs int // Total expected records left after the repair.
|
||
|
}{
|
||
|
"torn_last_record": {
|
||
|
2,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize*2, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{byte(recFirst)})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
8,
|
||
|
},
|
||
|
// Ensures that the page buffer is big enough to fit
|
||
5 years ago
|
// an entire page size without panicking.
|
||
5 years ago
|
// https://github.com/prometheus/tsdb/pull/414
|
||
6 years ago
|
"bad_header": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{byte(recPageTerm)})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
6 years ago
|
},
|
||
6 years ago
|
"bad_fragment_sequence": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{byte(recLast)})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
7 years ago
|
},
|
||
6 years ago
|
"bad_fragment_flag": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{123})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
7 years ago
|
},
|
||
6 years ago
|
"bad_checksum": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize+4, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{0})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
7 years ago
|
},
|
||
6 years ago
|
"bad_length": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize+2, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte{0})
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
7 years ago
|
},
|
||
6 years ago
|
"bad_content": {
|
||
|
1,
|
||
|
func(f *os.File) {
|
||
|
_, err := f.Seek(pageSize+100, 0)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
_, err = f.Write([]byte("beef"))
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
},
|
||
|
4,
|
||
7 years ago
|
},
|
||
|
} {
|
||
|
t.Run(name, func(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
7 years ago
|
|
||
6 years ago
|
// We create 3 segments with 3 records each and
|
||
|
// then corrupt a given record in a given segment.
|
||
|
// As a result we want a repaired WAL with given intact records.
|
||
6 years ago
|
segSize := 3 * pageSize
|
||
6 years ago
|
w, err := NewSize(nil, nil, dir, segSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
|
var records [][]byte
|
||
|
|
||
|
for i := 1; i <= 9; i++ {
|
||
|
b := make([]byte, pageSize-recordHeaderSize)
|
||
|
b[0] = byte(i)
|
||
|
records = append(records, b)
|
||
4 years ago
|
require.NoError(t, w.Log(b))
|
||
7 years ago
|
}
|
||
4 years ago
|
first, last, err := Segments(w.Dir())
|
||
4 years ago
|
require.NoError(t, err)
|
||
2 years ago
|
require.Equal(t, 3, 1+last-first, "wlog creation didn't result in expected number of segments")
|
||
6 years ago
|
|
||
4 years ago
|
require.NoError(t, w.Close())
|
||
7 years ago
|
|
||
3 years ago
|
f, err := os.OpenFile(SegmentName(dir, test.corrSgm), os.O_RDWR, 0o666)
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
|
// Apply corruption function.
|
||
6 years ago
|
test.corrFunc(f)
|
||
7 years ago
|
|
||
4 years ago
|
require.NoError(t, f.Close())
|
||
7 years ago
|
|
||
6 years ago
|
w, err = NewSize(nil, nil, dir, segSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
defer w.Close()
|
||
7 years ago
|
|
||
4 years ago
|
first, last, err = Segments(w.Dir())
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
6 years ago
|
// Backfill segments from the most recent checkpoint onwards.
|
||
|
for i := first; i <= last; i++ {
|
||
|
s, err := OpenReadSegment(SegmentName(w.Dir(), i))
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
sr := NewSegmentBufReader(s)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
r := NewReader(sr)
|
||
|
for r.Next() {
|
||
|
}
|
||
|
|
||
3 years ago
|
// Close the segment so we don't break things on Windows.
|
||
6 years ago
|
s.Close()
|
||
|
|
||
|
// No corruption in this segment.
|
||
|
if r.Err() == nil {
|
||
|
continue
|
||
|
}
|
||
4 years ago
|
require.NoError(t, w.Repair(r.Err()))
|
||
6 years ago
|
break
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
sr, err := NewSegmentsReader(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
defer sr.Close()
|
||
6 years ago
|
r := NewReader(sr)
|
||
7 years ago
|
|
||
|
var result [][]byte
|
||
|
for r.Next() {
|
||
|
var b []byte
|
||
|
result = append(result, append(b, r.Record()...))
|
||
|
}
|
||
4 years ago
|
require.NoError(t, r.Err())
|
||
|
require.Equal(t, test.intactRecs, len(result), "Wrong number of intact records")
|
||
7 years ago
|
|
||
|
for i, r := range result {
|
||
|
if !bytes.Equal(records[i], r) {
|
||
|
t.Fatalf("record %d diverges: want %x, got %x", i, records[i][:10], r[:10])
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// Make sure there is a new 0 size Segment after the corrupted Segment.
|
||
4 years ago
|
_, last, err = Segments(w.Dir())
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, test.corrSgm+1, last)
|
||
6 years ago
|
fi, err := os.Stat(SegmentName(dir, last))
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, int64(0), fi.Size())
|
||
7 years ago
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// TestCorruptAndCarryOn writes a multi-segment WAL; corrupts the first segment and
|
||
|
// ensures that an error during reading that segment are correctly repaired before
|
||
|
// moving to write more records to the WAL.
|
||
|
func TestCorruptAndCarryOn(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
6 years ago
|
|
||
|
var (
|
||
|
logger = testutil.NewLogger(t)
|
||
|
segmentSize = pageSize * 3
|
||
|
recordSize = (pageSize / 3) - recordHeaderSize
|
||
|
)
|
||
|
|
||
|
// Produce a WAL with a two segments of 3 pages with 3 records each,
|
||
|
// so when we truncate the file we're guaranteed to split a record.
|
||
|
{
|
||
6 years ago
|
w, err := NewSize(logger, nil, dir, segmentSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
for i := 0; i < 18; i++ {
|
||
|
buf := make([]byte, recordSize)
|
||
|
_, err := rand.Read(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
err = w.Log(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
|
||
|
err = w.Close()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
|
||
|
// Check all the segments are the correct size.
|
||
|
{
|
||
|
segments, err := listSegments(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
for _, segment := range segments {
|
||
3 years ago
|
f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%08d", segment.index)), os.O_RDONLY, 0o666)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
fi, err := f.Stat()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
t.Log("segment", segment.index, "size", fi.Size())
|
||
4 years ago
|
require.Equal(t, int64(segmentSize), fi.Size())
|
||
6 years ago
|
|
||
|
err = f.Close()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Truncate the first file, splitting the middle record in the second
|
||
|
// page in half, leaving 4 valid records.
|
||
|
{
|
||
3 years ago
|
f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%08d", 0)), os.O_RDWR, 0o666)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
fi, err := f.Stat()
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.Equal(t, int64(segmentSize), fi.Size())
|
||
6 years ago
|
|
||
|
err = f.Truncate(int64(segmentSize / 2))
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
err = f.Close()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
|
||
|
// Now try and repair this WAL, and write 5 more records to it.
|
||
|
{
|
||
|
sr, err := NewSegmentsReader(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
reader := NewReader(sr)
|
||
|
i := 0
|
||
|
for ; i < 4 && reader.Next(); i++ {
|
||
4 years ago
|
require.Equal(t, recordSize, len(reader.Record()))
|
||
6 years ago
|
}
|
||
4 years ago
|
require.Equal(t, 4, i, "not enough records")
|
||
|
require.False(t, reader.Next(), "unexpected record")
|
||
6 years ago
|
|
||
|
corruptionErr := reader.Err()
|
||
4 years ago
|
require.Error(t, corruptionErr)
|
||
6 years ago
|
|
||
|
err = sr.Close()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
6 years ago
|
w, err := NewSize(logger, nil, dir, segmentSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
err = w.Repair(corruptionErr)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
5 years ago
|
// Ensure that we have a completely clean slate after repairing.
|
||
4 years ago
|
require.Equal(t, w.segment.Index(), 1) // We corrupted segment 0.
|
||
|
require.Equal(t, w.donePages, 0)
|
||
6 years ago
|
|
||
6 years ago
|
for i := 0; i < 5; i++ {
|
||
|
buf := make([]byte, recordSize)
|
||
|
_, err := rand.Read(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
err = w.Log(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
|
||
|
err = w.Close()
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
|
|
||
|
// Replay the WAL. Should get 9 records.
|
||
|
{
|
||
|
sr, err := NewSegmentsReader(dir)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
reader := NewReader(sr)
|
||
|
i := 0
|
||
|
for ; i < 9 && reader.Next(); i++ {
|
||
4 years ago
|
require.Equal(t, recordSize, len(reader.Record()))
|
||
6 years ago
|
}
|
||
4 years ago
|
require.Equal(t, 9, i, "wrong number of records")
|
||
|
require.False(t, reader.Next(), "unexpected record")
|
||
|
require.Equal(t, nil, reader.Err())
|
||
6 years ago
|
sr.Close()
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// TestClose ensures that calling Close more than once doesn't panic and doesn't block.
|
||
|
func TestClose(t *testing.T) {
|
||
3 years ago
|
dir := t.TempDir()
|
||
6 years ago
|
w, err := NewSize(nil, nil, dir, pageSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
|
require.NoError(t, w.Close())
|
||
|
require.Error(t, w.Close())
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func TestSegmentMetric(t *testing.T) {
|
||
|
var (
|
||
|
segmentSize = pageSize
|
||
|
recordSize = (pageSize / 2) - recordHeaderSize
|
||
|
)
|
||
|
|
||
3 years ago
|
dir := t.TempDir()
|
||
6 years ago
|
w, err := NewSize(nil, nil, dir, segmentSize, false)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
5 years ago
|
initialSegment := client_testutil.ToFloat64(w.metrics.currentSegment)
|
||
6 years ago
|
|
||
|
// Write 3 records, each of which is half the segment size, meaning we should rotate to the next segment.
|
||
|
for i := 0; i < 3; i++ {
|
||
|
buf := make([]byte, recordSize)
|
||
|
_, err := rand.Read(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
err = w.Log(buf)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
}
|
||
4 years ago
|
require.Equal(t, initialSegment+1, client_testutil.ToFloat64(w.metrics.currentSegment), "segment metric did not increment after segment rotation")
|
||
|
require.NoError(t, w.Close())
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
func TestCompression(t *testing.T) {
|
||
5 years ago
|
bootstrap := func(compressed bool) string {
|
||
6 years ago
|
const (
|
||
|
segmentSize = pageSize
|
||
|
recordSize = (pageSize / 2) - recordHeaderSize
|
||
|
records = 100
|
||
|
)
|
||
|
|
||
3 years ago
|
dirPath := t.TempDir()
|
||
6 years ago
|
|
||
|
w, err := NewSize(nil, nil, dirPath, segmentSize, compressed)
|
||
4 years ago
|
require.NoError(t, err)
|
||
6 years ago
|
|
||
|
buf := make([]byte, recordSize)
|
||
|
for i := 0; i < records; i++ {
|
||
4 years ago
|
require.NoError(t, w.Log(buf))
|
||
6 years ago
|
}
|
||
4 years ago
|
require.NoError(t, w.Close())
|
||
6 years ago
|
|
||
|
return dirPath
|
||
|
}
|
||
|
|
||
5 years ago
|
dirCompressed := bootstrap(true)
|
||
6 years ago
|
defer func() {
|
||
4 years ago
|
require.NoError(t, os.RemoveAll(dirCompressed))
|
||
6 years ago
|
}()
|
||
5 years ago
|
dirUnCompressed := bootstrap(false)
|
||
6 years ago
|
defer func() {
|
||
4 years ago
|
require.NoError(t, os.RemoveAll(dirUnCompressed))
|
||
6 years ago
|
}()
|
||
7 years ago
|
|
||
5 years ago
|
uncompressedSize, err := fileutil.DirSize(dirUnCompressed)
|
||
4 years ago
|
require.NoError(t, err)
|
||
5 years ago
|
compressedSize, err := fileutil.DirSize(dirCompressed)
|
||
4 years ago
|
require.NoError(t, err)
|
||
7 years ago
|
|
||
4 years ago
|
require.Greater(t, float64(uncompressedSize)*0.75, float64(compressedSize), "Compressing zeroes should save at least 25%% space - uncompressedSize: %d, compressedSize: %d", uncompressedSize, compressedSize)
|
||
6 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
func TestLogPartialWrite(t *testing.T) {
|
||
|
const segmentSize = pageSize * 2
|
||
|
record := []byte{1, 2, 3, 4, 5}
|
||
|
|
||
|
tests := map[string]struct {
|
||
|
numRecords int
|
||
|
faultyRecord int
|
||
|
}{
|
||
|
"partial write when logging first record in a page": {
|
||
|
numRecords: 10,
|
||
|
faultyRecord: 1,
|
||
|
},
|
||
|
"partial write when logging record in the middle of a page": {
|
||
|
numRecords: 10,
|
||
|
faultyRecord: 3,
|
||
|
},
|
||
|
"partial write when logging last record of a page": {
|
||
|
numRecords: (pageSize / (recordHeaderSize + len(record))) + 10,
|
||
|
faultyRecord: pageSize / (recordHeaderSize + len(record)),
|
||
|
},
|
||
|
// TODO the current implementation suffers this:
|
||
|
//"partial write when logging a record overlapping two pages": {
|
||
|
// numRecords: (pageSize / (recordHeaderSize + len(record))) + 10,
|
||
|
// faultyRecord: pageSize/(recordHeaderSize+len(record)) + 1,
|
||
|
//},
|
||
|
}
|
||
|
|
||
|
for testName, testData := range tests {
|
||
|
t.Run(testName, func(t *testing.T) {
|
||
3 years ago
|
dirPath := t.TempDir()
|
||
4 years ago
|
|
||
|
w, err := NewSize(nil, nil, dirPath, segmentSize, false)
|
||
|
require.NoError(t, err)
|
||
|
|
||
|
// Replace the underlying segment file with a mocked one that injects a failure.
|
||
|
w.segment.SegmentFile = &faultySegmentFile{
|
||
|
SegmentFile: w.segment.SegmentFile,
|
||
|
writeFailureAfter: ((recordHeaderSize + len(record)) * (testData.faultyRecord - 1)) + 2,
|
||
|
writeFailureErr: io.ErrShortWrite,
|
||
|
}
|
||
|
|
||
|
for i := 1; i <= testData.numRecords; i++ {
|
||
|
if err := w.Log(record); i == testData.faultyRecord {
|
||
|
require.Error(t, io.ErrShortWrite, err)
|
||
|
} else {
|
||
|
require.NoError(t, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
require.NoError(t, w.Close())
|
||
|
|
||
|
// Read it back. We expect no corruption.
|
||
|
s, err := OpenReadSegment(SegmentName(dirPath, 0))
|
||
|
require.NoError(t, err)
|
||
4 years ago
|
defer func() { require.NoError(t, s.Close()) }()
|
||
4 years ago
|
|
||
|
r := NewReader(NewSegmentBufReader(s))
|
||
|
for i := 0; i < testData.numRecords; i++ {
|
||
|
require.True(t, r.Next())
|
||
|
require.NoError(t, r.Err())
|
||
|
require.Equal(t, record, r.Record())
|
||
|
}
|
||
|
require.False(t, r.Next())
|
||
|
require.NoError(t, r.Err())
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
type faultySegmentFile struct {
|
||
|
SegmentFile
|
||
|
|
||
|
written int
|
||
|
writeFailureAfter int
|
||
|
writeFailureErr error
|
||
|
}
|
||
|
|
||
|
func (f *faultySegmentFile) Write(p []byte) (int, error) {
|
||
|
if f.writeFailureAfter >= 0 && f.writeFailureAfter < f.written+len(p) {
|
||
|
partialLen := f.writeFailureAfter - f.written
|
||
|
if partialLen <= 0 || partialLen >= len(p) {
|
||
|
partialLen = 1
|
||
|
}
|
||
|
|
||
|
// Inject failure.
|
||
|
n, _ := f.SegmentFile.Write(p[:partialLen])
|
||
|
f.written += n
|
||
|
f.writeFailureAfter = -1
|
||
|
|
||
|
return n, f.writeFailureErr
|
||
|
}
|
||
|
|
||
|
// Proxy the write to the underlying file.
|
||
|
n, err := f.SegmentFile.Write(p)
|
||
|
f.written += n
|
||
|
return n, err
|
||
|
}
|
||
|
|
||
6 years ago
|
func BenchmarkWAL_LogBatched(b *testing.B) {
|
||
|
for _, compress := range []bool{true, false} {
|
||
|
b.Run(fmt.Sprintf("compress=%t", compress), func(b *testing.B) {
|
||
3 years ago
|
dir := b.TempDir()
|
||
6 years ago
|
|
||
|
w, err := New(nil, nil, dir, compress)
|
||
4 years ago
|
require.NoError(b, err)
|
||
6 years ago
|
defer w.Close()
|
||
|
|
||
|
var buf [2048]byte
|
||
|
var recs [][]byte
|
||
|
b.SetBytes(2048)
|
||
|
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
recs = append(recs, buf[:])
|
||
|
if len(recs) < 1000 {
|
||
|
continue
|
||
|
}
|
||
|
err := w.Log(recs...)
|
||
4 years ago
|
require.NoError(b, err)
|
||
6 years ago
|
recs = recs[:0]
|
||
|
}
|
||
|
// Stop timer to not count fsync time on close.
|
||
|
// If it's counted batched vs. single benchmarks are very similar but
|
||
|
// do not show burst throughput well.
|
||
|
b.StopTimer()
|
||
|
})
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func BenchmarkWAL_Log(b *testing.B) {
|
||
6 years ago
|
for _, compress := range []bool{true, false} {
|
||
|
b.Run(fmt.Sprintf("compress=%t", compress), func(b *testing.B) {
|
||
3 years ago
|
dir := b.TempDir()
|
||
7 years ago
|
|
||
6 years ago
|
w, err := New(nil, nil, dir, compress)
|
||
4 years ago
|
require.NoError(b, err)
|
||
6 years ago
|
defer w.Close()
|
||
7 years ago
|
|
||
6 years ago
|
var buf [2048]byte
|
||
|
b.SetBytes(2048)
|
||
7 years ago
|
|
||
6 years ago
|
for i := 0; i < b.N; i++ {
|
||
|
err := w.Log(buf[:])
|
||
4 years ago
|
require.NoError(b, err)
|
||
6 years ago
|
}
|
||
|
// Stop timer to not count fsync time on close.
|
||
|
// If it's counted batched vs. single benchmarks are very similar but
|
||
|
// do not show burst throughput well.
|
||
|
b.StopTimer()
|
||
|
})
|
||
7 years ago
|
}
|
||
|
}
|