Commit 0aba0aa4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent bfa03189
package main
import (
"crypto/rand"
"fmt"
"os"
"testing"
)
// xpread is pread(2) but aborts on an error or when fewer bytes was read
func xpread(f *os.File, buf []byte, offset int64) {
n, err := f.ReadAt(buf, offset)
if err != nil {
panic(err)
}
if n != len(buf) {
panic(fmt.Errorf("pread(%v) -> %v", len(buf), n))
}
}
// BenchmarkPread1 benchmarks pread syscall when reading 1 byte from a file on tmpfs
func BenchmarkPread1(b *testing.B) {
// setup 4K file on a tmpfs
f, err := os.Create("/dev/shm/x.dat")
if err != nil {
b.Fatal(err)
}
buf4K := make([]byte, 4096)
_, err = rand.Read(buf4K)
if err != nil {
// rand promises n = len(buf) <=> err == nil
b.Fatal(err)
}
_, err = f.Write(buf4K)
if err != nil {
b.Fatal(err)
}
buf1B := make([]byte, 1)
// warm up
xpread(f, buf1B, 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
xpread(f, buf1B, 0)
}
b.StopTimer()
err = f.Close()
if err != nil {
b.Fatal(f)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment