github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/graphdriver/graphtest/graphbench_unix.go (about) 1 // +build linux freebsd 2 3 package graphtest // import "github.com/demonoid81/moby/daemon/graphdriver/graphtest" 4 5 import ( 6 "io" 7 "io/ioutil" 8 "testing" 9 10 contdriver "github.com/containerd/continuity/driver" 11 "github.com/demonoid81/moby/pkg/stringid" 12 "gotest.tools/v3/assert" 13 ) 14 15 // DriverBenchExists benchmarks calls to exist 16 func DriverBenchExists(b *testing.B, drivername string, driveroptions ...string) { 17 driver := GetDriver(b, drivername, driveroptions...) 18 defer PutDriver(b) 19 20 base := stringid.GenerateRandomID() 21 22 if err := driver.Create(base, "", nil); err != nil { 23 b.Fatal(err) 24 } 25 26 b.ResetTimer() 27 for i := 0; i < b.N; i++ { 28 if !driver.Exists(base) { 29 b.Fatal("Newly created image doesn't exist") 30 } 31 } 32 } 33 34 // DriverBenchGetEmpty benchmarks calls to get on an empty layer 35 func DriverBenchGetEmpty(b *testing.B, drivername string, driveroptions ...string) { 36 driver := GetDriver(b, drivername, driveroptions...) 37 defer PutDriver(b) 38 39 base := stringid.GenerateRandomID() 40 41 if err := driver.Create(base, "", nil); err != nil { 42 b.Fatal(err) 43 } 44 45 b.ResetTimer() 46 for i := 0; i < b.N; i++ { 47 _, err := driver.Get(base, "") 48 b.StopTimer() 49 if err != nil { 50 b.Fatalf("Error getting mount: %s", err) 51 } 52 if err := driver.Put(base); err != nil { 53 b.Fatalf("Error putting mount: %s", err) 54 } 55 b.StartTimer() 56 } 57 } 58 59 // DriverBenchDiffBase benchmarks calls to diff on a root layer 60 func DriverBenchDiffBase(b *testing.B, drivername string, driveroptions ...string) { 61 driver := GetDriver(b, drivername, driveroptions...) 62 defer PutDriver(b) 63 64 base := stringid.GenerateRandomID() 65 if err := driver.Create(base, "", nil); err != nil { 66 b.Fatal(err) 67 } 68 69 if err := addFiles(driver, base, 3); err != nil { 70 b.Fatal(err) 71 } 72 73 b.ResetTimer() 74 for i := 0; i < b.N; i++ { 75 arch, err := driver.Diff(base, "") 76 if err != nil { 77 b.Fatal(err) 78 } 79 _, err = io.Copy(ioutil.Discard, arch) 80 if err != nil { 81 b.Fatalf("Error copying archive: %s", err) 82 } 83 arch.Close() 84 } 85 } 86 87 // DriverBenchDiffN benchmarks calls to diff on two layers with 88 // a provided number of files on the lower and upper layers. 89 func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driveroptions ...string) { 90 driver := GetDriver(b, drivername, driveroptions...) 91 defer PutDriver(b) 92 base := stringid.GenerateRandomID() 93 upper := stringid.GenerateRandomID() 94 if err := driver.Create(base, "", nil); err != nil { 95 b.Fatal(err) 96 } 97 98 if err := addManyFiles(driver, base, bottom, 3); err != nil { 99 b.Fatal(err) 100 } 101 102 if err := driver.Create(upper, base, nil); err != nil { 103 b.Fatal(err) 104 } 105 106 if err := addManyFiles(driver, upper, top, 6); err != nil { 107 b.Fatal(err) 108 } 109 b.ResetTimer() 110 for i := 0; i < b.N; i++ { 111 arch, err := driver.Diff(upper, "") 112 if err != nil { 113 b.Fatal(err) 114 } 115 _, err = io.Copy(ioutil.Discard, arch) 116 if err != nil { 117 b.Fatalf("Error copying archive: %s", err) 118 } 119 arch.Close() 120 } 121 } 122 123 // DriverBenchDiffApplyN benchmarks calls to diff and apply together 124 func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, driveroptions ...string) { 125 driver := GetDriver(b, drivername, driveroptions...) 126 defer PutDriver(b) 127 base := stringid.GenerateRandomID() 128 upper := stringid.GenerateRandomID() 129 if err := driver.Create(base, "", nil); err != nil { 130 b.Fatal(err) 131 } 132 133 if err := addManyFiles(driver, base, fileCount, 3); err != nil { 134 b.Fatal(err) 135 } 136 137 if err := driver.Create(upper, base, nil); err != nil { 138 b.Fatal(err) 139 } 140 141 if err := addManyFiles(driver, upper, fileCount, 6); err != nil { 142 b.Fatal(err) 143 } 144 diffSize, err := driver.DiffSize(upper, "") 145 if err != nil { 146 b.Fatal(err) 147 } 148 b.ResetTimer() 149 b.StopTimer() 150 for i := 0; i < b.N; i++ { 151 diff := stringid.GenerateRandomID() 152 if err := driver.Create(diff, base, nil); err != nil { 153 b.Fatal(err) 154 } 155 156 if err := checkManyFiles(driver, diff, fileCount, 3); err != nil { 157 b.Fatal(err) 158 } 159 160 b.StartTimer() 161 162 arch, err := driver.Diff(upper, "") 163 if err != nil { 164 b.Fatal(err) 165 } 166 167 applyDiffSize, err := driver.ApplyDiff(diff, "", arch) 168 if err != nil { 169 b.Fatal(err) 170 } 171 172 b.StopTimer() 173 arch.Close() 174 175 // suppressing "SA9003: empty branch (staticcheck)" instead of commenting-out/removing 176 // these lines because removing/commenting these lines causes a ripple effect 177 // of changes, and there's still a to-do below 178 //nolint:staticcheck 179 if applyDiffSize != diffSize { 180 // TODO: enforce this 181 //b.Fatalf("Apply diff size different, got %d, expected %s", applyDiffSize, diffSize) 182 } 183 if err := checkManyFiles(driver, diff, fileCount, 6); err != nil { 184 b.Fatal(err) 185 } 186 } 187 } 188 189 // DriverBenchDeepLayerDiff benchmarks calls to diff on top of a given number of layers. 190 func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, driveroptions ...string) { 191 driver := GetDriver(b, drivername, driveroptions...) 192 defer PutDriver(b) 193 194 base := stringid.GenerateRandomID() 195 if err := driver.Create(base, "", nil); err != nil { 196 b.Fatal(err) 197 } 198 199 if err := addFiles(driver, base, 50); err != nil { 200 b.Fatal(err) 201 } 202 203 topLayer, err := addManyLayers(driver, base, layerCount) 204 if err != nil { 205 b.Fatal(err) 206 } 207 208 b.ResetTimer() 209 for i := 0; i < b.N; i++ { 210 arch, err := driver.Diff(topLayer, "") 211 if err != nil { 212 b.Fatal(err) 213 } 214 _, err = io.Copy(ioutil.Discard, arch) 215 if err != nil { 216 b.Fatalf("Error copying archive: %s", err) 217 } 218 arch.Close() 219 } 220 } 221 222 // DriverBenchDeepLayerRead benchmarks calls to read a file under a given number of layers. 223 func DriverBenchDeepLayerRead(b *testing.B, layerCount int, drivername string, driveroptions ...string) { 224 driver := GetDriver(b, drivername, driveroptions...) 225 defer PutDriver(b) 226 227 base := stringid.GenerateRandomID() 228 if err := driver.Create(base, "", nil); err != nil { 229 b.Fatal(err) 230 } 231 232 content := []byte("test content") 233 if err := addFile(driver, base, "testfile.txt", content); err != nil { 234 b.Fatal(err) 235 } 236 237 topLayer, err := addManyLayers(driver, base, layerCount) 238 if err != nil { 239 b.Fatal(err) 240 } 241 242 root, err := driver.Get(topLayer, "") 243 if err != nil { 244 b.Fatal(err) 245 } 246 defer driver.Put(topLayer) 247 248 b.ResetTimer() 249 for i := 0; i < b.N; i++ { 250 251 // Read content 252 c, err := contdriver.ReadFile(root, root.Join(root.Path(), "testfile.txt")) 253 if err != nil { 254 b.Fatal(err) 255 } 256 257 b.StopTimer() 258 assert.DeepEqual(b, content, c) 259 b.StartTimer() 260 } 261 }