|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ext4 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | +) |
| 26 | + |
| 27 | +func diskUsage(t testing.TB, path string) (apparent, actual int64) { |
| 28 | + t.Helper() |
| 29 | + info, err := os.Stat(path) |
| 30 | + if err != nil { |
| 31 | + t.Fatal(err) |
| 32 | + } |
| 33 | + apparent = info.Size() |
| 34 | + |
| 35 | + out, err := exec.Command("du", "--block-size=1", path).CombinedOutput() |
| 36 | + if err == nil { |
| 37 | + fmt.Sscanf(string(out), "%d", &actual) |
| 38 | + } else { |
| 39 | + actual = apparent |
| 40 | + } |
| 41 | + return apparent, actual |
| 42 | +} |
| 43 | + |
| 44 | +var benchSizes = []struct { |
| 45 | + name string |
| 46 | + size int64 |
| 47 | +}{ |
| 48 | + {"64MiB", 64 * 1024 * 1024}, |
| 49 | + {"256MiB", 256 * 1024 * 1024}, |
| 50 | + {"1GiB", 1024 * 1024 * 1024}, |
| 51 | +} |
| 52 | + |
| 53 | +// BenchmarkCreate benchmarks the native Go Create function. |
| 54 | +func BenchmarkCreate(b *testing.B) { |
| 55 | + for _, sz := range benchSizes { |
| 56 | + b.Run(sz.name, func(b *testing.B) { |
| 57 | + outDir := b.TempDir() |
| 58 | + b.ResetTimer() |
| 59 | + for i := 0; i < b.N; i++ { |
| 60 | + imgPath := filepath.Join(outDir, fmt.Sprintf("test_%d.ext4", i)) |
| 61 | + if err := Create(imgPath, sz.size); err != nil { |
| 62 | + b.Fatal(err) |
| 63 | + } |
| 64 | + if i == b.N-1 { |
| 65 | + apparent, actual := diskUsage(b, imgPath) |
| 66 | + b.ReportMetric(float64(apparent), "apparent-bytes") |
| 67 | + b.ReportMetric(float64(actual), "disk-bytes") |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// BenchmarkMkfsOptimized benchmarks mkfs.ext4 with the same optimized flags. |
| 75 | +func BenchmarkMkfsOptimized(b *testing.B) { |
| 76 | + checkTools(b) |
| 77 | + |
| 78 | + for _, sz := range benchSizes { |
| 79 | + b.Run(sz.name, func(b *testing.B) { |
| 80 | + outDir := b.TempDir() |
| 81 | + b.ResetTimer() |
| 82 | + for i := 0; i < b.N; i++ { |
| 83 | + imgPath := filepath.Join(outDir, fmt.Sprintf("test_%d.ext4", i)) |
| 84 | + f, err := os.Create(imgPath) |
| 85 | + if err != nil { |
| 86 | + b.Fatal(err) |
| 87 | + } |
| 88 | + f.Truncate(sz.size) |
| 89 | + f.Close() |
| 90 | + cmd := exec.Command("mkfs.ext4", |
| 91 | + "-b", "4096", "-m", "0", |
| 92 | + "-O", "^has_journal,sparse_super2,^resize_inode", |
| 93 | + "-E", "lazy_itable_init=1,lazy_journal_init=1,nodiscard,assume_storage_prezeroed=1", |
| 94 | + "-F", "-q", imgPath) |
| 95 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 96 | + b.Fatalf("mkfs.ext4 failed: %v\n%s", err, out) |
| 97 | + } |
| 98 | + if i == b.N-1 { |
| 99 | + apparent, actual := diskUsage(b, imgPath) |
| 100 | + b.ReportMetric(float64(apparent), "apparent-bytes") |
| 101 | + b.ReportMetric(float64(actual), "disk-bytes") |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// BenchmarkMkfsDefaults benchmarks mkfs.ext4 with default options. |
| 109 | +func BenchmarkMkfsDefaults(b *testing.B) { |
| 110 | + checkTools(b) |
| 111 | + |
| 112 | + for _, sz := range benchSizes { |
| 113 | + b.Run(sz.name, func(b *testing.B) { |
| 114 | + outDir := b.TempDir() |
| 115 | + b.ResetTimer() |
| 116 | + for i := 0; i < b.N; i++ { |
| 117 | + imgPath := filepath.Join(outDir, fmt.Sprintf("test_%d.ext4", i)) |
| 118 | + f, err := os.Create(imgPath) |
| 119 | + if err != nil { |
| 120 | + b.Fatal(err) |
| 121 | + } |
| 122 | + f.Truncate(sz.size) |
| 123 | + f.Close() |
| 124 | + cmd := exec.Command("mkfs.ext4", "-F", "-q", imgPath) |
| 125 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 126 | + b.Fatalf("mkfs.ext4 failed: %v\n%s", err, out) |
| 127 | + } |
| 128 | + if i == b.N-1 { |
| 129 | + apparent, actual := diskUsage(b, imgPath) |
| 130 | + b.ReportMetric(float64(apparent), "apparent-bytes") |
| 131 | + b.ReportMetric(float64(actual), "disk-bytes") |
| 132 | + } |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
0 commit comments