Skip to content

Commit

Permalink
stability and ux improvements (#3)
Browse files Browse the repository at this point in the history
- ensure that input paths are always directories
- ensure that tests can be cancelled if ctrl+c is pressed
- print a error message if tests are cancelled in-flight
  • Loading branch information
wlan0 authored Apr 15, 2022
1 parent 1085878 commit 723adb1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
14 changes: 14 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -107,6 +108,19 @@ $ dperf --serial /mnt/drive{1..6}
if filepath.Clean(arg) == "/" {
return errors.New("not allowed to write at the root of the system, please choose a valid path")
}
path := filepath.Clean(arg)

stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return errors.New("directory at path '" + path + "' does not exist")
}
return err
}

if !stat.Mode().IsDir() {
return errors.New("path '" + path + "' is not a directory ")
}
paths = append(paths, filepath.Clean(arg))
}
return perf.RunAndRender(c.Context(), paths...)
Expand Down
22 changes: 12 additions & 10 deletions pkg/dperf/perf.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ func mustGetUUID() string {
return u.String()
}

func (d *DrivePerf) runTests(ctx context.Context, path string) (dr *DrivePerfResult) {
tmpPath := filepath.Join(path, ".writable-check.tmp")

writeThroughput, err := d.runWriteTest(ctx, tmpPath)
func (d *DrivePerf) runTests(ctx context.Context, path string, testUUID string) (dr *DrivePerfResult) {
testUUIDPath := filepath.Join(path, testUUID)
testPath := filepath.Join(testUUIDPath, ".writable-check.tmp")
defer os.RemoveAll(testUUIDPath)

writeThroughput, err := d.runWriteTest(ctx, testPath)
if err != nil {
return &DrivePerfResult{
Path: path,
Error: err,
}
}
readThroughput, err := d.runReadTest(ctx, tmpPath)
readThroughput, err := d.runReadTest(ctx, testPath)
if err != nil {
return &DrivePerfResult{
Path: path,
Expand Down Expand Up @@ -82,24 +84,24 @@ func (d *DrivePerf) Run(ctx context.Context, paths ...string) ([]*DrivePerfResul
results := make([]*DrivePerfResult, len(paths))
if d.Serial {
for i, path := range paths {
results[i] = d.runTests(childCtx, filepath.Join(path, uuidStr))
results[i] = d.runTests(childCtx, path, uuidStr)
}
} else {
var wg sync.WaitGroup
wg.Add(parallelism)
for i, path := range paths {
go func(idx int, path string) {
defer wg.Done()
results[idx] = d.runTests(childCtx, filepath.Join(path, uuidStr))
results[idx] = d.runTests(childCtx, path, uuidStr)
}(i, path)
}
wg.Wait()
}

for _, res := range results {
os.RemoveAll(res.Path)
if childCtx.Err() != nil {
return nil, childCtx.Err()
}

return results, nil
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/dperf/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ type odirectReader struct {
buf []byte
err error
seenRead bool

ctx context.Context
}

// Read - Implements Reader interface.
func (o *odirectReader) Read(buf []byte) (n int, err error) {
if o.ctx.Err() != nil {
return 0, o.ctx.Err()
}
if o.err != nil && (len(o.buf) == 0 || !o.seenRead) {
return 0, o.err
}
Expand Down Expand Up @@ -97,7 +102,11 @@ func (d *DrivePerf) runReadTest(ctx context.Context, path string) (float64, erro

// Read Aligned block upto a multiple of BlockSize
data := directio.AlignedBlock(int(d.BlockSize))
of := &odirectReader{File: f, Bufp: &data}
of := &odirectReader{
File: f,
Bufp: &data,
ctx: ctx,
}
n, err := io.Copy(ioutil.Discard, of)
if err != nil {
of.Close()
Expand Down Expand Up @@ -149,9 +158,14 @@ func fadviseDontNeed(f *os.File) error {
return unix.Fadvise(int(f.Fd()), 0, 0, unix.FADV_DONTNEED)
}

type nullReader struct{}
type nullReader struct {
ctx context.Context
}

func (n nullReader) Read(b []byte) (int, error) {
if n.ctx.Err() != nil {
return 0, n.ctx.Err()
}
return len(b), nil
}

Expand All @@ -173,7 +187,7 @@ func (d *DrivePerf) runWriteTest(ctx context.Context, path string) (float64, err

// Write Aligned block upto a multiple of BlockSize
data := alignedBlock(int(d.BlockSize))
n, err := io.CopyBuffer(f, io.LimitReader(&nullReader{}, int64(d.FileSize)), data)
n, err := io.CopyBuffer(f, io.LimitReader(&nullReader{ctx: ctx}, int64(d.FileSize)), data)
if err != nil {
sync()
return 0, err
Expand Down

0 comments on commit 723adb1

Please sign in to comment.