This file is indexed.

/usr/share/gocode/src/bazil.org/fuse/fs/bench/bench_readwrite_test.go is in golang-bazil-fuse-dev 0.0~git20160811.0.371fbbd-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package bench_test

import (
	"io"
	"io/ioutil"
	"os"
	"path"
	"testing"

	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	"bazil.org/fuse/fs/fstestutil"
	"golang.org/x/net/context"
)

type benchConfig struct {
	directIO bool
}

type benchFS struct {
	conf *benchConfig
}

var _ = fs.FS(benchFS{})

func (f benchFS) Root() (fs.Node, error) {
	return benchDir{conf: f.conf}, nil
}

type benchDir struct {
	conf *benchConfig
}

var _ = fs.Node(benchDir{})
var _ = fs.NodeStringLookuper(benchDir{})
var _ = fs.Handle(benchDir{})
var _ = fs.HandleReadDirAller(benchDir{})

func (benchDir) Attr(ctx context.Context, a *fuse.Attr) error {
	a.Inode = 1
	a.Mode = os.ModeDir | 0555
	return nil
}

func (d benchDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
	if name == "bench" {
		return benchFile{conf: d.conf}, nil
	}
	return nil, fuse.ENOENT
}

func (benchDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
	l := []fuse.Dirent{
		{Inode: 2, Name: "bench", Type: fuse.DT_File},
	}
	return l, nil
}

type benchFile struct {
	conf *benchConfig
}

var _ = fs.Node(benchFile{})
var _ = fs.NodeOpener(benchFile{})
var _ = fs.NodeFsyncer(benchFile{})
var _ = fs.Handle(benchFile{})
var _ = fs.HandleReader(benchFile{})
var _ = fs.HandleWriter(benchFile{})

func (benchFile) Attr(ctx context.Context, a *fuse.Attr) error {
	a.Inode = 2
	a.Mode = 0644
	a.Size = 9999999999999999
	return nil
}

func (f benchFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
	if f.conf.directIO {
		resp.Flags |= fuse.OpenDirectIO
	}
	// TODO configurable?
	resp.Flags |= fuse.OpenKeepCache
	return f, nil
}

func (benchFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
	resp.Data = resp.Data[:cap(resp.Data)]
	return nil
}

func (benchFile) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
	resp.Size = len(req.Data)
	return nil
}

func (benchFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
	return nil
}

func benchmark(b *testing.B, fn func(b *testing.B, mnt string), conf *benchConfig) {
	filesys := benchFS{
		conf: conf,
	}
	mnt, err := fstestutil.Mounted(filesys, nil,
		fuse.MaxReadahead(64*1024*1024),
		fuse.AsyncRead(),
		fuse.WritebackCache(),
	)
	if err != nil {
		b.Fatal(err)
	}
	defer mnt.Close()

	fn(b, mnt.Dir)
}

type zero struct{}

func (zero) Read(p []byte) (n int, err error) {
	return len(p), nil
}

var Zero io.Reader = zero{}

func doWrites(size int64) func(b *testing.B, mnt string) {
	return func(b *testing.B, mnt string) {
		p := path.Join(mnt, "bench")

		f, err := os.Create(p)
		if err != nil {
			b.Fatalf("create: %v", err)
		}
		defer f.Close()

		b.ResetTimer()
		b.SetBytes(size)

		for i := 0; i < b.N; i++ {
			_, err = io.CopyN(f, Zero, size)
			if err != nil {
				b.Fatalf("write: %v", err)
			}
		}
	}
}

func BenchmarkWrite100(b *testing.B) {
	benchmark(b, doWrites(100), &benchConfig{})
}

func BenchmarkWrite10MB(b *testing.B) {
	benchmark(b, doWrites(10*1024*1024), &benchConfig{})
}

func BenchmarkWrite100MB(b *testing.B) {
	benchmark(b, doWrites(100*1024*1024), &benchConfig{})
}

func BenchmarkDirectWrite100(b *testing.B) {
	benchmark(b, doWrites(100), &benchConfig{
		directIO: true,
	})
}

func BenchmarkDirectWrite10MB(b *testing.B) {
	benchmark(b, doWrites(10*1024*1024), &benchConfig{
		directIO: true,
	})
}

func BenchmarkDirectWrite100MB(b *testing.B) {
	benchmark(b, doWrites(100*1024*1024), &benchConfig{
		directIO: true,
	})
}

func doWritesSync(size int64) func(b *testing.B, mnt string) {
	return func(b *testing.B, mnt string) {
		p := path.Join(mnt, "bench")

		f, err := os.Create(p)
		if err != nil {
			b.Fatalf("create: %v", err)
		}
		defer f.Close()

		b.ResetTimer()
		b.SetBytes(size)

		for i := 0; i < b.N; i++ {
			_, err = io.CopyN(f, Zero, size)
			if err != nil {
				b.Fatalf("write: %v", err)
			}

			if err := f.Sync(); err != nil {
				b.Fatalf("sync: %v", err)
			}
		}
	}
}

func BenchmarkWriteSync100(b *testing.B) {
	benchmark(b, doWritesSync(100), &benchConfig{})
}

func BenchmarkWriteSync10MB(b *testing.B) {
	benchmark(b, doWritesSync(10*1024*1024), &benchConfig{})
}

func BenchmarkWriteSync100MB(b *testing.B) {
	benchmark(b, doWritesSync(100*1024*1024), &benchConfig{})
}

func doReads(size int64) func(b *testing.B, mnt string) {
	return func(b *testing.B, mnt string) {
		p := path.Join(mnt, "bench")

		f, err := os.Open(p)
		if err != nil {
			b.Fatalf("close: %v", err)
		}
		defer f.Close()

		b.ResetTimer()
		b.SetBytes(size)

		for i := 0; i < b.N; i++ {
			n, err := io.CopyN(ioutil.Discard, f, size)
			if err != nil {
				b.Fatalf("read: %v", err)
			}
			if n != size {
				b.Errorf("unexpected size: %d != %d", n, size)
			}
		}
	}
}

func BenchmarkRead100(b *testing.B) {
	benchmark(b, doReads(100), &benchConfig{})
}

func BenchmarkRead10MB(b *testing.B) {
	benchmark(b, doReads(10*1024*1024), &benchConfig{})
}

func BenchmarkRead100MB(b *testing.B) {
	benchmark(b, doReads(100*1024*1024), &benchConfig{})
}

func BenchmarkDirectRead100(b *testing.B) {
	benchmark(b, doReads(100), &benchConfig{
		directIO: true,
	})
}

func BenchmarkDirectRead10MB(b *testing.B) {
	benchmark(b, doReads(10*1024*1024), &benchConfig{
		directIO: true,
	})
}

func BenchmarkDirectRead100MB(b *testing.B) {
	benchmark(b, doReads(100*1024*1024), &benchConfig{
		directIO: true,
	})
}