File: bench_create_test.go

package info (click to toggle)
golang-github-anacrolix-fuse 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,000 kB
  • sloc: makefile: 5; sh: 3
file content (107 lines) | stat: -rw-r--r-- 2,414 bytes parent folder | download
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
package bench_test

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"sync"
	"testing"

	"github.com/anacrolix/fuse"
	"github.com/anacrolix/fuse/fs"
	"github.com/anacrolix/fuse/fs/fstestutil"
	"github.com/anacrolix/fuse/fs/fstestutil/spawntest/httpjson"
)

type dummyFile struct {
	fstestutil.File
}

type benchCreateDir struct {
	fstestutil.Dir
}

var _ fs.NodeCreater = (*benchCreateDir)(nil)

func (f *benchCreateDir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
	child := &dummyFile{}
	return child, child, nil
}

type benchCreateHelp struct {
	mu    sync.Mutex
	n     int
	names []string
}

func (b *benchCreateHelp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	switch req.URL.Path {
	case "/init":
		httpjson.ServePOST(b.doInit).ServeHTTP(w, req)
	case "/bench":
		httpjson.ServePOST(b.doBench).ServeHTTP(w, req)
	default:
		http.NotFound(w, req)
	}
}

type benchCreateInitRequest struct {
	Dir string
	N   int
}

func (b *benchCreateHelp) doInit(ctx context.Context, req benchCreateInitRequest) (*struct{}, error) {
	b.mu.Lock()
	defer b.mu.Unlock()
	// prepare file names to decrease test overhead
	names := make([]string, 0, req.N)
	for i := 0; i < req.N; i++ {
		// zero-padded so cost stays the same on every iteration
		names = append(names, req.Dir+"/"+fmt.Sprintf("%08x", i))
	}
	b.n = req.N
	b.names = names
	return &struct{}{}, nil
}

func (b *benchCreateHelp) doBench(ctx context.Context, _ struct{}) (*struct{}, error) {
	for i := 0; i < b.n; i++ {
		f, err := os.Create(b.names[i])
		if err != nil {
			log.Fatalf("Create: %v", err)
		}
		f.Close()
	}
	return &struct{}{}, nil
}

var benchCreateHelper = helpers.Register("benchCreate", &benchCreateHelp{})

func BenchmarkCreate(b *testing.B) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	f := &benchCreateDir{}
	mnt, err := fstestutil.MountedT(b, fstestutil.SimpleFS{f}, nil)
	if err != nil {
		b.Fatal(err)
	}
	defer mnt.Close()
	control := benchCreateHelper.Spawn(ctx, b)
	defer control.Close()

	req := benchCreateInitRequest{
		Dir: mnt.Dir,
		N:   b.N,
	}
	var nothing struct{}
	if err := control.JSON("/init").Call(ctx, req, &nothing); err != nil {
		b.Fatalf("calling helper: %v", err)
	}
	b.ResetTimer()

	if err := control.JSON("/bench").Call(ctx, struct{}{}, &nothing); err != nil {
		b.Fatalf("calling helper: %v", err)
	}
}