mirror of
https://github.com/octoleo/restic.git
synced 2024-11-11 07:41:03 +00:00
tests: Add benchmarks to test suite
This commit is contained in:
parent
1c9159d6a0
commit
f142b1c22f
@ -19,3 +19,8 @@ var testFunctions = []struct {
|
|||||||
{"Backend", BackendTestBackend},
|
{"Backend", BackendTestBackend},
|
||||||
{"Delete", BackendTestDelete},
|
{"Delete", BackendTestDelete},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var benchmarkFunctions = []struct {
|
||||||
|
Name string
|
||||||
|
Fn func(t testing.TB, suite *Suite)
|
||||||
|
}{}
|
||||||
|
@ -4,12 +4,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"go/format"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -19,7 +20,8 @@ import (
|
|||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Package string
|
Package string
|
||||||
Funcs []string
|
TestFuncs []string
|
||||||
|
BenchmarkFuncs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var testTemplate = `
|
var testTemplate = `
|
||||||
@ -35,10 +37,19 @@ var testFunctions = []struct {
|
|||||||
Name string
|
Name string
|
||||||
Fn func(t testing.TB, suite *Suite)
|
Fn func(t testing.TB, suite *Suite)
|
||||||
}{
|
}{
|
||||||
{{ range $f := .Funcs -}}
|
{{ range $f := .TestFuncs -}}
|
||||||
{"{{ $f }}", BackendTest{{ $f }},},
|
{"{{ $f }}", BackendTest{{ $f }},},
|
||||||
{{ end }}
|
{{ end }}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var benchmarkFunctions = []struct {
|
||||||
|
Name string
|
||||||
|
Fn func(t testing.TB, suite *Suite)
|
||||||
|
}{
|
||||||
|
{{ range $f := .BenchmarkFuncs -}}
|
||||||
|
{"{{ $f }}", BackendBenchmark{{ $f }},},
|
||||||
|
{{ end }}
|
||||||
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
var testFile = flag.String("testfile", "tests.go", "file to search test functions in")
|
var testFile = flag.String("testfile", "tests.go", "file to search test functions in")
|
||||||
@ -56,17 +67,23 @@ func errx(err error) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var funcRegex = regexp.MustCompile(`^func\s+BackendTest(.+)\s*\(`)
|
var testFuncRegex = regexp.MustCompile(`^func\s+BackendTest(.+)\s*\(`)
|
||||||
|
var benchmarkFuncRegex = regexp.MustCompile(`^func\s+BackendBenchmark(.+)\s*\(`)
|
||||||
|
|
||||||
func findTestFunctions() (funcs []string) {
|
func findFunctions() (testFuncs, benchmarkFuncs []string) {
|
||||||
f, err := os.Open(*testFile)
|
f, err := os.Open(*testFile)
|
||||||
errx(err)
|
errx(err)
|
||||||
|
|
||||||
sc := bufio.NewScanner(f)
|
sc := bufio.NewScanner(f)
|
||||||
for sc.Scan() {
|
for sc.Scan() {
|
||||||
match := funcRegex.FindStringSubmatch(sc.Text())
|
match := testFuncRegex.FindStringSubmatch(sc.Text())
|
||||||
if len(match) > 0 {
|
if len(match) > 0 {
|
||||||
funcs = append(funcs, match[1])
|
testFuncs = append(testFuncs, match[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
match = benchmarkFuncRegex.FindStringSubmatch(sc.Text())
|
||||||
|
if len(match) > 0 {
|
||||||
|
benchmarkFuncs = append(benchmarkFuncs, match[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,20 +92,20 @@ func findTestFunctions() (funcs []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errx(f.Close())
|
errx(f.Close())
|
||||||
return funcs
|
return testFuncs, benchmarkFuncs
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateOutput(wr io.Writer, data interface{}) {
|
func generateOutput(wr io.Writer, data interface{}) {
|
||||||
t := template.Must(template.New("backendtest").Parse(testTemplate))
|
t := template.Must(template.New("backendtest").Parse(testTemplate))
|
||||||
|
|
||||||
cmd := exec.Command("gofmt")
|
buf := bytes.NewBuffer(nil)
|
||||||
cmd.Stdout = wr
|
errx(t.Execute(buf, data))
|
||||||
in, err := cmd.StdinPipe()
|
|
||||||
|
source, err := format.Source(buf.Bytes())
|
||||||
|
errx(err)
|
||||||
|
|
||||||
|
_, err = wr.Write(source)
|
||||||
errx(err)
|
errx(err)
|
||||||
errx(cmd.Start())
|
|
||||||
errx(t.Execute(in, data))
|
|
||||||
errx(in.Close())
|
|
||||||
errx(cmd.Wait())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func packageTestFunctionPrefix(pkg string) string {
|
func packageTestFunctionPrefix(pkg string) string {
|
||||||
@ -120,7 +137,7 @@ func main() {
|
|||||||
errx(err)
|
errx(err)
|
||||||
|
|
||||||
data.Package = pkg
|
data.Package = pkg
|
||||||
data.Funcs = findTestFunctions()
|
data.TestFuncs, data.BenchmarkFuncs = findFunctions()
|
||||||
generateOutput(f, data)
|
generateOutput(f, data)
|
||||||
|
|
||||||
errx(f.Close())
|
errx(f.Close())
|
||||||
|
@ -68,6 +68,34 @@ func (s *Suite) RunTests(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunBenchmarks executes all defined benchmarks as subtests of b.
|
||||||
|
func (s *Suite) RunBenchmarks(b *testing.B) {
|
||||||
|
var err error
|
||||||
|
s.Config, err = s.NewConfig()
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// test create/open functions first
|
||||||
|
be := s.create(b)
|
||||||
|
s.close(b, be)
|
||||||
|
|
||||||
|
for _, test := range benchmarkFunctions {
|
||||||
|
b.Run(test.Name, func(b *testing.B) {
|
||||||
|
test.Fn(b, s)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !test.TestCleanupTempDirs {
|
||||||
|
b.Logf("not cleaning up backend")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.Cleanup(s.Config); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Suite) create(t testing.TB) restic.Backend {
|
func (s *Suite) create(t testing.TB) restic.Backend {
|
||||||
be, err := s.Create(s.Config)
|
be, err := s.Create(s.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user