695 lines
19 KiB
Go
695 lines
19 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"math"
|
|
"math/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type WorkloadResult struct {
|
|
Workload string `json:"workload"`
|
|
Phase string `json:"phase"`
|
|
OS string `json:"os"`
|
|
Arch string `json:"arch"`
|
|
FullFsyncSupported bool `json:"fullfsync_supported"`
|
|
Ops int `json:"ops"`
|
|
Bytes int64 `json:"bytes"`
|
|
TotalMS float64 `json:"total_ms"`
|
|
OpsPerSec float64 `json:"ops_per_sec"`
|
|
MeanUS float64 `json:"mean_us"`
|
|
P50US float64 `json:"p50_us"`
|
|
P95US float64 `json:"p95_us"`
|
|
P99US float64 `json:"p99_us"`
|
|
MaxUS float64 `json:"max_us"`
|
|
Meta map[string]any `json:"meta,omitempty"`
|
|
}
|
|
|
|
type BenchOutput struct {
|
|
Timestamp string `json:"timestamp"`
|
|
WorkDir string `json:"work_dir"`
|
|
Results []WorkloadResult `json:"results"`
|
|
}
|
|
|
|
type config struct {
|
|
workload string
|
|
phase string
|
|
dir string
|
|
files int
|
|
fileSize int
|
|
largeOps int
|
|
scanRoots string
|
|
largeFile string
|
|
seed int64
|
|
keep bool
|
|
}
|
|
|
|
func main() {
|
|
var cfg config
|
|
flag.StringVar(&cfg.workload, "workload", "all", "workload: create-fsync|create-fullfsync|read-seq|read-rand|stat-tree|rand-read-large|all")
|
|
flag.StringVar(&cfg.phase, "phase", "all", "phase: all|prepare|run|cleanup")
|
|
flag.StringVar(&cfg.dir, "dir", "", "working directory on target volume (default: $HOME/.reims-io-bench)")
|
|
flag.IntVar(&cfg.files, "files", 1000, "number of small files")
|
|
flag.IntVar(&cfg.fileSize, "size", 4096, "small file size in bytes")
|
|
flag.IntVar(&cfg.largeOps, "large-ops", 2000, "number of 4 KiB random reads for rand-read-large")
|
|
flag.StringVar(&cfg.scanRoots, "scan-roots", "/System/Library,/Applications,/Library,/usr/lib", "comma-separated roots for rand-read-large file pool")
|
|
flag.StringVar(&cfg.largeFile, "large-file", "", "optional single large file path for rand-read-large")
|
|
flag.Int64Var(&cfg.seed, "seed", 20261129, "deterministic PRNG seed")
|
|
flag.BoolVar(&cfg.keep, "keep", false, "keep prepared files when phase=all")
|
|
flag.Parse()
|
|
|
|
if cfg.dir == "" {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
fatalf("resolve home dir: %v", err)
|
|
}
|
|
cfg.dir = filepath.Join(home, ".reims-io-bench")
|
|
}
|
|
|
|
out, err := execute(cfg)
|
|
if err != nil {
|
|
fatalf("%v", err)
|
|
}
|
|
enc := json.NewEncoder(os.Stdout)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(out); err != nil {
|
|
fatalf("encode json: %v", err)
|
|
}
|
|
}
|
|
|
|
func fatalf(format string, args ...any) {
|
|
fmt.Fprintf(os.Stderr, "guest-bench: "+format+"\n", args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func execute(cfg config) (BenchOutput, error) {
|
|
if cfg.files <= 0 {
|
|
return BenchOutput{}, fmt.Errorf("files must be > 0")
|
|
}
|
|
if cfg.fileSize <= 0 {
|
|
return BenchOutput{}, fmt.Errorf("size must be > 0")
|
|
}
|
|
if cfg.largeOps <= 0 {
|
|
return BenchOutput{}, fmt.Errorf("large-ops must be > 0")
|
|
}
|
|
|
|
var workloads []string
|
|
if cfg.workload == "all" {
|
|
workloads = []string{"create-fsync", "create-fullfsync", "read-seq", "read-rand", "stat-tree", "rand-read-large"}
|
|
} else {
|
|
for _, w := range strings.Split(cfg.workload, ",") {
|
|
w = strings.TrimSpace(w)
|
|
if w != "" {
|
|
workloads = append(workloads, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
out := BenchOutput{
|
|
Timestamp: time.Now().UTC().Format(time.RFC3339Nano),
|
|
WorkDir: cfg.dir,
|
|
}
|
|
|
|
for _, w := range workloads {
|
|
res, err := runWorkload(cfg, w)
|
|
if err != nil {
|
|
return out, fmt.Errorf("%s (%s): %w", w, cfg.phase, err)
|
|
}
|
|
out.Results = append(out.Results, res)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func runWorkload(cfg config, w string) (WorkloadResult, error) {
|
|
switch w {
|
|
case "create-fsync":
|
|
return runCreate(cfg, w, false)
|
|
case "create-fullfsync":
|
|
return runCreate(cfg, w, true)
|
|
case "read-seq":
|
|
return runSmallRead(cfg, w, false)
|
|
case "read-rand":
|
|
return runSmallRead(cfg, w, true)
|
|
case "stat-tree":
|
|
return runStatTree(cfg, w)
|
|
case "rand-read-large":
|
|
return runRandReadLarge(cfg, w)
|
|
default:
|
|
return WorkloadResult{}, fmt.Errorf("unknown workload %q", w)
|
|
}
|
|
}
|
|
|
|
func payloadBytes(size int, seed int64) []byte {
|
|
rng := rand.New(rand.NewSource(seed))
|
|
buf := make([]byte, size)
|
|
for i := range buf {
|
|
buf[i] = byte(rng.Intn(251) + 1)
|
|
}
|
|
return buf
|
|
}
|
|
|
|
func runCreate(cfg config, name string, fullSync bool) (WorkloadResult, error) {
|
|
sub := filepath.Join(cfg.dir, name)
|
|
switch cfg.phase {
|
|
case "prepare":
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if err := os.MkdirAll(sub, 0o755); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub}), nil
|
|
case "cleanup":
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub}), nil
|
|
case "all", "run":
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if err := os.MkdirAll(sub, 0o755); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if cfg.phase == "all" && !cfg.keep {
|
|
defer os.RemoveAll(sub)
|
|
}
|
|
data := payloadBytes(cfg.fileSize, cfg.seed)
|
|
lats := make([]time.Duration, cfg.files)
|
|
startAll := time.Now()
|
|
for i := 0; i < cfg.files; i++ {
|
|
p := filepath.Join(sub, fmt.Sprintf("f_%05d.dat", i))
|
|
t0 := time.Now()
|
|
f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if _, err := f.Write(data); err != nil {
|
|
_ = f.Close()
|
|
return WorkloadResult{}, err
|
|
}
|
|
if fullSync {
|
|
err = syncFull(f)
|
|
} else {
|
|
err = f.Sync()
|
|
}
|
|
if err != nil {
|
|
_ = f.Close()
|
|
return WorkloadResult{}, err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
lats[i] = time.Since(t0)
|
|
}
|
|
total := time.Since(startAll)
|
|
return summarize(name, cfg.phase, int64(cfg.files)*int64(cfg.fileSize), total, lats, map[string]any{
|
|
"file_size": cfg.fileSize,
|
|
"full_sync": fullSync,
|
|
}), nil
|
|
default:
|
|
return WorkloadResult{}, fmt.Errorf("unknown phase %q", cfg.phase)
|
|
}
|
|
}
|
|
|
|
func prepareSmallReadFiles(sub string, count, size int, seed int64) error {
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(sub, 0o755); err != nil {
|
|
return err
|
|
}
|
|
data := payloadBytes(size, seed)
|
|
for i := 0; i < count; i++ {
|
|
p := filepath.Join(sub, fmt.Sprintf("r_%05d.dat", i))
|
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runSmallRead(cfg config, name string, randomOrder bool) (WorkloadResult, error) {
|
|
sub := filepath.Join(cfg.dir, "small-read-set")
|
|
switch cfg.phase {
|
|
case "prepare":
|
|
if err := prepareSmallReadFiles(sub, cfg.files, cfg.fileSize, cfg.seed); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub, "files": cfg.files, "file_size": cfg.fileSize}), nil
|
|
case "cleanup":
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub}), nil
|
|
case "all", "run":
|
|
if cfg.phase == "all" {
|
|
if err := prepareSmallReadFiles(sub, cfg.files, cfg.fileSize, cfg.seed); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if !cfg.keep {
|
|
defer os.RemoveAll(sub)
|
|
}
|
|
}
|
|
order := make([]int, cfg.files)
|
|
for i := range order {
|
|
order[i] = i
|
|
}
|
|
if randomOrder {
|
|
rng := rand.New(rand.NewSource(cfg.seed + 17))
|
|
rng.Shuffle(len(order), func(i, j int) { order[i], order[j] = order[j], order[i] })
|
|
}
|
|
|
|
buf := make([]byte, cfg.fileSize)
|
|
lats := make([]time.Duration, cfg.files)
|
|
var totalBytes int64
|
|
startAll := time.Now()
|
|
for idx, fileIdx := range order {
|
|
p := filepath.Join(sub, fmt.Sprintf("r_%05d.dat", fileIdx))
|
|
t0 := time.Now()
|
|
f, err := os.Open(p)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
n, err := io.ReadFull(f, buf)
|
|
cerr := f.Close()
|
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if cerr != nil {
|
|
return WorkloadResult{}, cerr
|
|
}
|
|
lats[idx] = time.Since(t0)
|
|
totalBytes += int64(n)
|
|
}
|
|
total := time.Since(startAll)
|
|
return summarize(name, cfg.phase, totalBytes, total, lats, map[string]any{
|
|
"file_size": cfg.fileSize,
|
|
"random_order": randomOrder,
|
|
}), nil
|
|
default:
|
|
return WorkloadResult{}, fmt.Errorf("unknown phase %q", cfg.phase)
|
|
}
|
|
}
|
|
|
|
func treeDims(total int) (int, int) {
|
|
side := int(math.Ceil(math.Cbrt(float64(total))))
|
|
if side < 1 {
|
|
side = 1
|
|
}
|
|
return side, side
|
|
}
|
|
|
|
func prepareStatTree(sub string, count, size int, seed int64) (int, error) {
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return 0, err
|
|
}
|
|
if err := os.MkdirAll(sub, 0o755); err != nil {
|
|
return 0, err
|
|
}
|
|
d1Count, d2Count := treeDims(count)
|
|
perLeaf := (count + d1Count*d2Count - 1) / (d1Count * d2Count)
|
|
if perLeaf < 1 {
|
|
perLeaf = 1
|
|
}
|
|
data := payloadBytes(min(size, 256), seed)
|
|
created := 0
|
|
dirs := 0
|
|
for a := 0; a < d1Count && created < count; a++ {
|
|
for b := 0; b < d2Count && created < count; b++ {
|
|
for c := 0; c < d1Count && created < count; c++ {
|
|
dirPath := filepath.Join(sub, fmt.Sprintf("l1_%02d", a), fmt.Sprintf("l2_%02d", b), fmt.Sprintf("l3_%02d", c))
|
|
if err := os.MkdirAll(dirPath, 0o755); err != nil {
|
|
return 0, err
|
|
}
|
|
dirs++
|
|
for k := 0; k < perLeaf && created < count; k++ {
|
|
p := filepath.Join(dirPath, fmt.Sprintf("f_%05d.txt", created))
|
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
|
return 0, err
|
|
}
|
|
created++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return dirs, nil
|
|
}
|
|
|
|
func runStatTree(cfg config, name string) (WorkloadResult, error) {
|
|
sub := filepath.Join(cfg.dir, "stat-tree")
|
|
switch cfg.phase {
|
|
case "prepare":
|
|
dirs, err := prepareStatTree(sub, cfg.files, cfg.fileSize, cfg.seed)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub, "files": cfg.files, "leaf_dirs": dirs, "depth": 3}), nil
|
|
case "cleanup":
|
|
if err := os.RemoveAll(sub); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"dir": sub}), nil
|
|
case "all", "run":
|
|
if cfg.phase == "all" {
|
|
if _, err := prepareStatTree(sub, cfg.files, cfg.fileSize, cfg.seed); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if !cfg.keep {
|
|
defer os.RemoveAll(sub)
|
|
}
|
|
}
|
|
var lats []time.Duration
|
|
fileCount := 0
|
|
dirCount := 0
|
|
startAll := time.Now()
|
|
err := filepath.WalkDir(sub, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t0 := time.Now()
|
|
info, serr := os.Lstat(path)
|
|
if serr != nil {
|
|
return serr
|
|
}
|
|
lats = append(lats, time.Since(t0))
|
|
if info.IsDir() {
|
|
dirCount++
|
|
} else {
|
|
fileCount++
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
total := time.Since(startAll)
|
|
return summarize(name, cfg.phase, 0, total, lats, map[string]any{
|
|
"files_seen": fileCount,
|
|
"dirs_seen": dirCount,
|
|
"depth": 3,
|
|
}), nil
|
|
default:
|
|
return WorkloadResult{}, fmt.Errorf("unknown phase %q", cfg.phase)
|
|
}
|
|
}
|
|
|
|
type poolFile struct {
|
|
Path string `json:"path"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
func collectLargePool(roots []string, minSize int64, maxFiles int) []poolFile {
|
|
var pool []poolFile
|
|
seen := make(map[string]struct{})
|
|
for _, root := range roots {
|
|
root = strings.TrimSpace(root)
|
|
if root == "" {
|
|
continue
|
|
}
|
|
_ = filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if len(pool) >= maxFiles {
|
|
return fs.SkipAll
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
if d.Type()&os.ModeSymlink != 0 {
|
|
return nil
|
|
}
|
|
info, ierr := d.Info()
|
|
if ierr != nil || !info.Mode().IsRegular() || info.Size() < minSize {
|
|
return nil
|
|
}
|
|
if _, ok := seen[p]; ok {
|
|
return nil
|
|
}
|
|
seen[p] = struct{}{}
|
|
pool = append(pool, poolFile{Path: p, Size: info.Size()})
|
|
return nil
|
|
})
|
|
if len(pool) >= maxFiles {
|
|
break
|
|
}
|
|
}
|
|
return pool
|
|
}
|
|
|
|
func poolCachePath(workDir string) string {
|
|
return filepath.Join(workDir, "rand-read-large-pool.json")
|
|
}
|
|
|
|
func buildLargePool(cfg config) ([]poolFile, string, error) {
|
|
roots := strings.Split(cfg.scanRoots, ",")
|
|
// Prefer >= 1 MiB files first so a compact set of open fds spans GiBs of
|
|
// virtual LBA space without open()/close() overhead masking L2 misses.
|
|
pool := collectLargePool(roots, 1<<20, 4096)
|
|
mode := "multi-file-open-handles"
|
|
if len(pool) < 8 {
|
|
pool = collectLargePool(roots, 4096, 4096)
|
|
mode = "multi-file-fallback"
|
|
}
|
|
if len(pool) == 0 {
|
|
return nil, "", fmt.Errorf("no readable files >= 4 KiB found under scan-roots %q", cfg.scanRoots)
|
|
}
|
|
return pool, mode, nil
|
|
}
|
|
|
|
func runRandReadLarge(cfg config, name string) (WorkloadResult, error) {
|
|
cacheFile := poolCachePath(cfg.dir)
|
|
if cfg.phase == "cleanup" {
|
|
_ = os.Remove(cacheFile)
|
|
return emptyResult(name, cfg.phase, map[string]any{"cache_file": cacheFile}), nil
|
|
}
|
|
if cfg.phase == "prepare" {
|
|
if cfg.largeFile != "" {
|
|
return emptyResult(name, cfg.phase, map[string]any{"mode": "single-file", "large_file": cfg.largeFile}), nil
|
|
}
|
|
pool, mode, err := buildLargePool(cfg)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if err := os.MkdirAll(cfg.dir, 0o755); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
b, err := json.Marshal(pool)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
if err := os.WriteFile(cacheFile, b, 0o644); err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
return emptyResult(name, cfg.phase, map[string]any{"mode": mode, "pool_files": len(pool), "cache_file": cacheFile}), nil
|
|
}
|
|
if cfg.phase != "all" && cfg.phase != "run" {
|
|
return WorkloadResult{}, fmt.Errorf("unknown phase %q", cfg.phase)
|
|
}
|
|
|
|
const blockSize = int64(4096)
|
|
rng := rand.New(rand.NewSource(cfg.seed + 99))
|
|
|
|
if cfg.largeFile != "" {
|
|
f, err := os.Open(cfg.largeFile)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
defer f.Close()
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
blocks := info.Size() / blockSize
|
|
if blocks <= 0 {
|
|
return WorkloadResult{}, fmt.Errorf("large-file %s is smaller than 4096 bytes", cfg.largeFile)
|
|
}
|
|
buf := make([]byte, blockSize)
|
|
lats := make([]time.Duration, cfg.largeOps)
|
|
startAll := time.Now()
|
|
for i := 0; i < cfg.largeOps; i++ {
|
|
off := rng.Int63n(blocks) * blockSize
|
|
t0 := time.Now()
|
|
if _, err := f.ReadAt(buf, off); err != nil && !errors.Is(err, io.EOF) {
|
|
return WorkloadResult{}, err
|
|
}
|
|
lats[i] = time.Since(t0)
|
|
}
|
|
total := time.Since(startAll)
|
|
return summarize(name, cfg.phase, int64(cfg.largeOps)*blockSize, total, lats, map[string]any{
|
|
"mode": "single-file",
|
|
"large_file": cfg.largeFile,
|
|
"large_file_size": info.Size(),
|
|
}), nil
|
|
}
|
|
|
|
var pool []poolFile
|
|
mode := "multi-file-cached-pool"
|
|
if cfg.phase == "run" {
|
|
if raw, err := os.ReadFile(cacheFile); err == nil {
|
|
_ = json.Unmarshal(raw, &pool)
|
|
}
|
|
}
|
|
if len(pool) == 0 {
|
|
var err error
|
|
pool, mode, err = buildLargePool(cfg)
|
|
if err != nil {
|
|
return WorkloadResult{}, err
|
|
}
|
|
}
|
|
|
|
// Shuffle and pick up to 96 distinct open handles (stays well below macOS default 256 fd limit).
|
|
rng.Shuffle(len(pool), func(i, j int) { pool[i], pool[j] = pool[j], pool[i] })
|
|
const maxOpenHandles = 96
|
|
type openTarget struct {
|
|
f *os.File
|
|
path string
|
|
blocks int64
|
|
size int64
|
|
}
|
|
var targets []openTarget
|
|
var totalSpanBytes int64
|
|
for _, pf := range pool {
|
|
if len(targets) >= maxOpenHandles {
|
|
break
|
|
}
|
|
f, err := os.Open(pf.Path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
blocks := pf.Size / blockSize
|
|
if blocks <= 0 {
|
|
_ = f.Close()
|
|
continue
|
|
}
|
|
targets = append(targets, openTarget{f: f, path: pf.Path, blocks: blocks, size: pf.Size})
|
|
totalSpanBytes += pf.Size
|
|
}
|
|
defer func() {
|
|
for _, t := range targets {
|
|
_ = t.f.Close()
|
|
}
|
|
}()
|
|
if len(targets) == 0 {
|
|
return WorkloadResult{}, fmt.Errorf("failed to open any candidate file under scan-roots %q", cfg.scanRoots)
|
|
}
|
|
|
|
// Weight selection by file size so reads sample uniformly across bytes spanned.
|
|
cumWeights := make([]int64, len(targets))
|
|
var acc int64
|
|
for i, t := range targets {
|
|
acc += t.blocks
|
|
cumWeights[i] = acc
|
|
}
|
|
|
|
buf := make([]byte, blockSize)
|
|
lats := make([]time.Duration, cfg.largeOps)
|
|
hitFiles := make(map[int]struct{})
|
|
startAll := time.Now()
|
|
for i := 0; i < cfg.largeOps; i++ {
|
|
pick := rng.Int63n(acc)
|
|
idx := sort.Search(len(cumWeights), func(j int) bool { return cumWeights[j] > pick })
|
|
if idx >= len(targets) {
|
|
idx = len(targets) - 1
|
|
}
|
|
hitFiles[idx] = struct{}{}
|
|
t := targets[idx]
|
|
off := rng.Int63n(t.blocks) * blockSize
|
|
t0 := time.Now()
|
|
if _, err := t.f.ReadAt(buf, off); err != nil && !errors.Is(err, io.EOF) {
|
|
return WorkloadResult{}, err
|
|
}
|
|
lats[i] = time.Since(t0)
|
|
}
|
|
total := time.Since(startAll)
|
|
return summarize(name, cfg.phase, int64(cfg.largeOps)*blockSize, total, lats, map[string]any{
|
|
"mode": mode,
|
|
"pool_files": len(pool),
|
|
"open_handles": len(targets),
|
|
"unique_files_hit": len(hitFiles),
|
|
"span_bytes": totalSpanBytes,
|
|
}), nil
|
|
}
|
|
|
|
func emptyResult(name, phase string, meta map[string]any) WorkloadResult {
|
|
return WorkloadResult{
|
|
Workload: name,
|
|
Phase: phase,
|
|
OS: runtime.GOOS,
|
|
Arch: runtime.GOARCH,
|
|
FullFsyncSupported: fullFsyncSupported,
|
|
Meta: meta,
|
|
}
|
|
}
|
|
|
|
func summarize(name, phase string, bytes int64, total time.Duration, lats []time.Duration, meta map[string]any) WorkloadResult {
|
|
n := len(lats)
|
|
sorted := make([]float64, n)
|
|
var sum float64
|
|
for i, d := range lats {
|
|
us := float64(d.Nanoseconds()) / 1000.0
|
|
sorted[i] = us
|
|
sum += us
|
|
}
|
|
sort.Float64s(sorted)
|
|
|
|
totalMS := float64(total.Nanoseconds()) / 1e6
|
|
opsPerSec := 0.0
|
|
if total > 0 {
|
|
opsPerSec = float64(n) / total.Seconds()
|
|
}
|
|
meanUS := 0.0
|
|
maxUS := 0.0
|
|
if n > 0 {
|
|
meanUS = sum / float64(n)
|
|
maxUS = sorted[n-1]
|
|
}
|
|
|
|
return WorkloadResult{
|
|
Workload: name,
|
|
Phase: phase,
|
|
OS: runtime.GOOS,
|
|
Arch: runtime.GOARCH,
|
|
FullFsyncSupported: fullFsyncSupported,
|
|
Ops: n,
|
|
Bytes: bytes,
|
|
TotalMS: round3(totalMS),
|
|
OpsPerSec: round3(opsPerSec),
|
|
MeanUS: round3(meanUS),
|
|
P50US: round3(percentile(sorted, 0.50)),
|
|
P95US: round3(percentile(sorted, 0.95)),
|
|
P99US: round3(percentile(sorted, 0.99)),
|
|
MaxUS: round3(maxUS),
|
|
Meta: meta,
|
|
}
|
|
}
|
|
|
|
func percentile(sorted []float64, p float64) float64 {
|
|
if len(sorted) == 0 {
|
|
return 0
|
|
}
|
|
idx := int(math.Ceil(p*float64(len(sorted)))) - 1
|
|
if idx < 0 {
|
|
idx = 0
|
|
}
|
|
if idx >= len(sorted) {
|
|
idx = len(sorted) - 1
|
|
}
|
|
return sorted[idx]
|
|
}
|
|
|
|
func round3(v float64) float64 {
|
|
return math.Round(v*1000) / 1000
|
|
}
|