disk_test.go 6.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package persist

// disk_test.go probes some of the disk operations that are very commonly used
// within Sia. Namely, Read, Write, Truncate, WriteAt(rand), ReadAt(rand).

import (
	"os"
	"path/filepath"
	"testing"

11
	"gitlab.com/NebulousLabs/Sia/build"
12

13
	"gitlab.com/NebulousLabs/fastrand"
14 15
)

David Vorick's avatar
David Vorick committed
16 17
// BenchmarkWrite512MiB checks how long it takes to write 512MiB sequentially.
func BenchmarkWrite512MiB(b *testing.B) {
18 19 20 21 22
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}
David Vorick's avatar
David Vorick committed
23 24 25 26

	b.SetBytes(1 << 29)
	filename := filepath.Join(testDir, "512MiB.file")
	b.ResetTimer()
27 28 29 30 31 32 33 34
	for i := 0; i < b.N; i++ {
		// Make the file.
		f, err := os.Create(filename)
		if err != nil {
			b.Fatal(err)
		}

		// 2^12 writes of 4MiB.
David Vorick's avatar
David Vorick committed
35
		for i := 0; i < 1<<7; i++ {
36 37 38 39 40 41 42 43 44 45 46
			// Get the entropy separate from the timer.
			b.StopTimer()
			data := fastrand.Bytes(1 << 22)
			b.StartTimer()
			_, err = f.Write(data)
			if err != nil {
				b.Fatal(err)
			}
			// Sync after every write.
			err = f.Sync()
			if err != nil {
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
				b.Fatal(err)
			}
		}

		// Close the file before iterating.
		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}

// BenchmarkWrite512MiBTrunc checks how long it takes to write 512MiB using
// stepwise truncate.
func BenchmarkWrite512MiBTrunc(b *testing.B) {
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}

	b.SetBytes(1 << 29)
	filename := filepath.Join(testDir, "512MiB.file")
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		// Make the file.
		f, err := os.Create(filename)
		if err != nil {
			b.Fatal(err)
		}

		// 2^12 writes of 4MiB.
		for i := 0; i < 1<<7; i++ {
			// Extend the file through truncation.
			err = f.Truncate(int64((i + 1) * 1 << 22))
			if err != nil {
				b.Fatal(err)
			}
			// Sync after every write.
			err = f.Sync()
			if err != nil {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
				b.Fatal(err)
			}
		}

		// Close the file before iterating.
		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}

David Vorick's avatar
David Vorick committed
110 111
// BenchmarkWrite512MiBRand checks how long it takes to write 512MiB randomly.
func BenchmarkWrite512MiBRand(b *testing.B) {
112 113 114 115 116
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}
David Vorick's avatar
David Vorick committed
117 118 119
	b.SetBytes(1 << 29)
	filename := filepath.Join(testDir, "512MiB.file")
	b.ResetTimer()
120 121 122 123 124 125 126
	for i := 0; i < b.N; i++ {
		// Make the file.
		f, err := os.Create(filename)
		if err != nil {
			b.Fatal(err)
		}

David Vorick's avatar
David Vorick committed
127 128
		// 2^7 writes of 4MiB.
		for i := 0; i < 1<<7; i++ {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			// Get the entropy separate from the timer.
			b.StopTimer()
			data := fastrand.Bytes(1 << 22)
			offset := int64(fastrand.Intn(1 << 6))
			offset *= 1 << 22
			b.StartTimer()
			_, err = f.WriteAt(data, offset)
			if err != nil {
				b.Fatal(err)
			}
			// Sync after every write.
			err = f.Sync()
			if err != nil {
				b.Fatal(err)
			}
		}

		// Close the file before iterating.
		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}

David Vorick's avatar
David Vorick committed
159 160
// BenchmarkRead512MiB checks how long it takes to read 512MiB sequentially.
func BenchmarkRead512MiB(b *testing.B) {
161 162 163 164 165
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}
David Vorick's avatar
David Vorick committed
166
	b.SetBytes(1 << 29)
167 168

	// Make the file.
David Vorick's avatar
David Vorick committed
169
	filename := filepath.Join(testDir, "512MiB.file")
170 171 172 173 174
	f, err := os.Create(filename)
	if err != nil {
		b.Fatal(err)
	}

David Vorick's avatar
David Vorick committed
175 176
	// 2^7 writes of 4MiB.
	for i := 0; i < 1<<7; i++ {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		// Get the entropy separate from the timer.
		b.StopTimer()
		data := fastrand.Bytes(1 << 22)
		b.StartTimer()
		_, err = f.Write(data)
		if err != nil {
			b.Fatal(err)
		}
		// Sync after every write.
		err = f.Sync()
		if err != nil {
			b.Fatal(err)
		}
	}

	// Close the file.
	err = f.Close()
	if err != nil {
		b.Fatal(err)
	}

	// Check the sequential read speed.
David Vorick's avatar
David Vorick committed
199
	b.ResetTimer()
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	for i := 0; i < b.N; i++ {
		// Open the file.
		f, err := os.Open(filename)
		if err != nil {
			b.Fatal(err)
		}

		// Read the file 4 MiB at a time.
		for i := 0; i < 1<<6; i++ {
			data := make([]byte, 1<<22)
			_, err = f.Read(data)
			if err != nil {
				b.Fatal(err)
			}
		}
David Vorick's avatar
David Vorick committed
215 216 217 218 219

		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
220 221 222 223 224 225 226 227
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}

David Vorick's avatar
David Vorick committed
228 229
// BenchmarkRead512MiBRand checks how long it takes to read 512MiB randomly.
func BenchmarkRead512MiBRand(b *testing.B) {
230 231 232 233 234
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}
David Vorick's avatar
David Vorick committed
235
	b.SetBytes(1 << 29)
236 237

	// Make the file.
David Vorick's avatar
David Vorick committed
238
	filename := filepath.Join(testDir, "512MiB.file")
239 240 241 242 243
	f, err := os.Create(filename)
	if err != nil {
		b.Fatal(err)
	}

David Vorick's avatar
David Vorick committed
244 245
	// 2^7 writes of 4MiB.
	for i := 0; i < 1<<7; i++ {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
		// Get the entropy separate from the timer.
		b.StopTimer()
		data := fastrand.Bytes(1 << 22)
		b.StartTimer()
		_, err = f.Write(data)
		if err != nil {
			b.Fatal(err)
		}
		// Sync after every write.
		err = f.Sync()
		if err != nil {
			b.Fatal(err)
		}
	}

	// Close the file.
	err = f.Close()
	if err != nil {
		b.Fatal(err)
	}

	// Check the sequential read speed.
David Vorick's avatar
David Vorick committed
268
	b.ResetTimer()
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	for i := 0; i < b.N; i++ {
		// Open the file.
		f, err := os.Open(filename)
		if err != nil {
			b.Fatal(err)
		}

		// Read the file 4 MiB at a time.
		for i := 0; i < 1<<6; i++ {
			offset := int64(fastrand.Intn(1 << 6))
			offset *= 1 << 22
			data := make([]byte, 1<<22)
			_, err = f.ReadAt(data, offset)
			if err != nil {
				b.Fatal(err)
			}
		}
David Vorick's avatar
David Vorick committed
286 287 288 289 290

		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
291 292 293 294 295 296 297 298
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}

David Vorick's avatar
David Vorick committed
299 300
// BenchmarkTruncate512MiB checks how long it takes to truncate a 512 MiB file.
func BenchmarkTruncate512MiB(b *testing.B) {
301 302 303 304 305
	testDir := build.TempDir("persist", b.Name())
	err := os.MkdirAll(testDir, 0700)
	if err != nil {
		b.Fatal(err)
	}
David Vorick's avatar
David Vorick committed
306 307
	b.SetBytes(1 << 29)
	filename := filepath.Join(testDir, "512MiB.file")
308
	// Check the truncate speed.
David Vorick's avatar
David Vorick committed
309
	b.ResetTimer()
310 311 312 313 314 315 316 317
	for i := 0; i < b.N; i++ {
		// Make the file separate from the timer.
		b.StopTimer()
		f, err := os.Create(filename)
		if err != nil {
			b.Fatal(err)
		}

David Vorick's avatar
David Vorick committed
318 319
		// 2^7 writes of 4MiB.
		for i := 0; i < 1<<7; i++ {
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
			// Get the entropy separate from the timer.
			b.StopTimer()
			data := fastrand.Bytes(1 << 22)
			b.StartTimer()
			_, err = f.Write(data)
			if err != nil {
				b.Fatal(err)
			}
		}
		// Sync after writing.
		err = f.Sync()
		if err != nil {
			b.Fatal(err)
		}

		// Close the file.
		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
		b.StartTimer()

		// Open the file.
		f, err = os.OpenFile(filename, os.O_RDWR, 0600)
		if err != nil {
			b.Fatal(err)
		}

		// Truncate the file.
		err = f.Truncate(0)
		if err != nil {
			b.Fatal(err)
		}

		// Sync.
		err = f.Sync()
		if err != nil {
			b.Fatal(err)
		}

		// Close.
		err = f.Close()
		if err != nil {
			b.Fatal(err)
		}
	}

	err = os.Remove(filename)
	if err != nil {
		b.Fatal(err)
	}
}