forked from lni/vfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncing_file_linux_test.go
107 lines (98 loc) · 2.23 KB
/
syncing_file_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
// +build linux,!arm
package vfs
import (
"fmt"
"io/ioutil"
"os"
"syscall"
"testing"
"unsafe"
)
func TestSyncRangeSmokeTest(t *testing.T) {
testCases := []struct {
err error
expected bool
}{
{nil, true},
{syscall.EINVAL, true},
{syscall.ENOSYS, false},
}
for i, c := range testCases {
t.Run("", func(t *testing.T) {
ok := syncRangeSmokeTest(uintptr(i),
func(fd int, off int64, n int64, flags int) (err error) {
if i != fd {
t.Fatalf("expected fd %d, but got %d", i, fd)
}
return c.err
})
if c.expected != ok {
t.Fatalf("expected %t, but got %t: %v", c.expected, ok, c.err)
}
})
}
}
func BenchmarkDirectIOWrite(b *testing.B) {
const targetSize = 16 << 20
const alignment = 4096
var wsizes []int
if testing.Verbose() {
wsizes = []int{4 << 10, 8 << 10, 16 << 10, 32 << 10}
} else {
wsizes = []int{4096}
}
for _, wsize := range wsizes {
b.Run(fmt.Sprintf("wsize=%d", wsize), func(b *testing.B) {
tmpf, err := ioutil.TempFile("", "pebble-db-syncing-file-")
if err != nil {
b.Fatal(err)
}
filename := tmpf.Name()
_ = tmpf.Close()
defer os.Remove(filename)
var f *os.File
var size int
buf := make([]byte, wsize+alignment)
if a := uintptr(unsafe.Pointer(&buf[0])) & uintptr(alignment-1); a != 0 {
buf = buf[alignment-a:]
}
buf = buf[:wsize]
init := true
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
if f == nil {
b.StopTimer()
f, err = os.OpenFile(filename, syscall.O_DIRECT|os.O_RDWR, 0666)
if err != nil {
b.Fatal(err)
}
if init {
for size = 0; size < targetSize; size += len(buf) {
if _, err := f.WriteAt(buf, int64(size)); err != nil {
b.Fatal(err)
}
}
}
if err := f.Sync(); err != nil {
b.Fatal(err)
}
size = 0
b.StartTimer()
}
if _, err := f.WriteAt(buf, int64(size)); err != nil {
b.Fatal(err)
}
size += len(buf)
if size >= targetSize {
_ = f.Close()
f = nil
}
}
b.StopTimer()
})
}
}