Skip to content

Instantly share code, notes, and snippets.

@x2c3z4
Forked from pfreixes/read.go
Created August 12, 2021 15:56
Show Gist options
  • Save x2c3z4/bb76c933e0ae190ec24b1855f7229eb0 to your computer and use it in GitHub Desktop.
Save x2c3z4/bb76c933e0ae190ec24b1855f7229eb0 to your computer and use it in GitHub Desktop.
Test for checking maximum throughput of random reads over a 40GB file stored in a SSD disk
package main
import (
"fmt"
"math/rand"
"os"
"sync"
"time"
)
const (
FILE_SIZE int64 = 42949672960
BUCKET_SIZE int64 = 512
BUCKETS int64 = FILE_SIZE / BUCKET_SIZE
Megabyte = 1 << 20
GOROUTINES int64 = 64
READ_BUCKETS int64 = 100_000
)
func main() {
fd, err := syscall.Open("file.bin", syscall.O_RDONLY, 0)
if err != nil {
panic(err)
}
_, _, e := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_NOCACHE, 1)
if e != 0 {
panic("Failed trying to disable the cache")
}
r := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
var wg sync.WaitGroup
start := time.Now()
// start N goroutines
for i := int64(0); i < GOROUTINES; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var offset int64
b := make([]byte, BUCKET_SIZE)
// read random buckets
for j := int64(0); j < READ_BUCKETS; j++ {
offset = r.Int63n(BUCKETS) * BUCKET_SIZE
if _, err := syscall.Pread(fd, b, offset); err != nil {
panic(fmt.Sprintf("%v %v", offset, err))
}
}
}()
}
wg.Wait()
elapsed := float64(time.Since(start)/ time.Second)
fmt.Printf("Total time in seconds %f\n", elapsed)
fmt.Printf("Block throughput %f blocks/second\n", float64(READ_BUCKETS * GOROUTINES) / elapsed)
fmt.Printf("Read throughput %f MB/second\n", float64((READ_BUCKETS * BUCKET_SIZE * GOROUTINES) / Megabyte) / elapsed)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment