2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux || freebsd
|
2016-04-14 21:14:42 -04:00
|
|
|
// +build linux freebsd
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package graphtest // import "github.com/docker/docker/daemon/graphdriver/graphtest"
|
2016-04-14 21:14:42 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
contdriver "github.com/containerd/continuity/driver"
|
2016-04-14 21:14:42 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-04-14 21:14:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// DriverBenchExists benchmarks calls to exist
|
|
|
|
func DriverBenchExists(b *testing.B, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
|
|
|
|
base := stringid.GenerateRandomID()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !driver.Exists(base) {
|
|
|
|
b.Fatal("Newly created image doesn't exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchGetEmpty benchmarks calls to get on an empty layer
|
|
|
|
func DriverBenchGetEmpty(b *testing.B, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
|
|
|
|
base := stringid.GenerateRandomID()
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := driver.Get(base, "")
|
|
|
|
b.StopTimer()
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Error getting mount: %s", err)
|
|
|
|
}
|
|
|
|
if err := driver.Put(base); err != nil {
|
|
|
|
b.Fatalf("Error putting mount: %s", err)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchDiffBase benchmarks calls to diff on a root layer
|
|
|
|
func DriverBenchDiffBase(b *testing.B, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
|
|
|
|
base := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addFiles(driver, base, 3); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
arch, err := driver.Diff(base, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, arch)
|
2016-04-14 21:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Error copying archive: %s", err)
|
|
|
|
}
|
|
|
|
arch.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchDiffN benchmarks calls to diff on two layers with
|
|
|
|
// a provided number of files on the lower and upper layers.
|
|
|
|
func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
base := stringid.GenerateRandomID()
|
|
|
|
upper := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addManyFiles(driver, base, bottom, 3); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(upper, base, nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addManyFiles(driver, upper, top, 6); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
arch, err := driver.Diff(upper, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, arch)
|
2016-04-14 21:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Error copying archive: %s", err)
|
|
|
|
}
|
|
|
|
arch.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchDiffApplyN benchmarks calls to diff and apply together
|
|
|
|
func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
base := stringid.GenerateRandomID()
|
|
|
|
upper := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addManyFiles(driver, base, fileCount, 3); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(upper, base, nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addManyFiles(driver, upper, fileCount, 6); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
diffSize, err := driver.DiffSize(upper, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
b.StopTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
diff := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(diff, base, nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkManyFiles(driver, diff, fileCount, 3); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
arch, err := driver.Diff(upper, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
applyDiffSize, err := driver.ApplyDiff(diff, "", arch)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
arch.Close()
|
|
|
|
|
2019-08-05 09:24:38 -04:00
|
|
|
// suppressing "SA9003: empty branch (staticcheck)" instead of commenting-out/removing
|
|
|
|
// these lines because removing/commenting these lines causes a ripple effect
|
|
|
|
// of changes, and there's still a to-do below
|
|
|
|
//nolint:staticcheck
|
2016-04-14 21:14:42 -04:00
|
|
|
if applyDiffSize != diffSize {
|
|
|
|
// TODO: enforce this
|
|
|
|
//b.Fatalf("Apply diff size different, got %d, expected %s", applyDiffSize, diffSize)
|
|
|
|
}
|
|
|
|
if err := checkManyFiles(driver, diff, fileCount, 6); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchDeepLayerDiff benchmarks calls to diff on top of a given number of layers.
|
|
|
|
func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
|
|
|
|
base := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := addFiles(driver, base, 50); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topLayer, err := addManyLayers(driver, base, layerCount)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
arch, err := driver.Diff(topLayer, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, arch)
|
2016-04-14 21:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Error copying archive: %s", err)
|
|
|
|
}
|
|
|
|
arch.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverBenchDeepLayerRead benchmarks calls to read a file under a given number of layers.
|
|
|
|
func DriverBenchDeepLayerRead(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
|
|
|
|
driver := GetDriver(b, drivername, driveroptions...)
|
|
|
|
defer PutDriver(b)
|
|
|
|
|
|
|
|
base := stringid.GenerateRandomID()
|
2016-11-09 15:59:58 -05:00
|
|
|
if err := driver.Create(base, "", nil); err != nil {
|
2016-04-14 21:14:42 -04:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content := []byte("test content")
|
|
|
|
if err := addFile(driver, base, "testfile.txt", content); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
topLayer, err := addManyLayers(driver, base, layerCount)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := driver.Get(topLayer, "")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer driver.Put(topLayer)
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
|
|
|
// Read content
|
2017-08-03 20:22:00 -04:00
|
|
|
c, err := contdriver.ReadFile(root, root.Join(root.Path(), "testfile.txt"))
|
2016-04-14 21:14:42 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.StopTimer()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.DeepEqual(b, content, c)
|
2016-04-14 21:14:42 -04:00
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|