github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/internal/sysfs/bench_test.go (about) 1 package sysfs 2 3 import ( 4 "io" 5 "io/fs" 6 "os" 7 "path" 8 "testing" 9 "time" 10 11 "github.com/wasilibs/wazerox/experimental/sys" 12 ) 13 14 func BenchmarkFsFileUtimesNs(b *testing.B) { 15 f, errno := OpenOSFile(path.Join(b.TempDir(), "file"), sys.O_CREAT, 0) 16 if errno != 0 { 17 b.Fatal(errno) 18 } 19 defer f.Close() 20 21 atim := int64(123*time.Second + 4*time.Microsecond) 22 mtim := atim 23 24 b.ResetTimer() 25 for i := 0; i < b.N; i++ { 26 if errno := f.Utimens(atim, mtim); errno != 0 { 27 b.Fatal(errno) 28 } 29 } 30 } 31 32 func BenchmarkFsFileRead(b *testing.B) { 33 dirFS := os.DirFS("testdata") 34 embedFS, err := fs.Sub(testdata, "testdata") 35 if err != nil { 36 b.Fatal(err) 37 } 38 39 benches := []struct { 40 name string 41 fs fs.FS 42 pread bool 43 }{ 44 {name: "os.DirFS Read", fs: dirFS, pread: false}, 45 {name: "os.DirFS Pread", fs: dirFS, pread: true}, 46 {name: "embed.api.FS Read", fs: embedFS, pread: false}, 47 {name: "embed.api.FS Pread", fs: embedFS, pread: true}, 48 } 49 50 buf := make([]byte, 3) 51 52 for _, bc := range benches { 53 bc := bc 54 55 b.Run(bc.name, func(b *testing.B) { 56 name := "wazero.txt" 57 f, errno := OpenFSFile(bc.fs, name, sys.O_RDONLY, 0) 58 if errno != 0 { 59 b.Fatal(errno) 60 } 61 defer f.Close() 62 63 b.ResetTimer() 64 for i := 0; i < b.N; i++ { 65 b.StopTimer() 66 67 var n int 68 var errno sys.Errno 69 70 // Reset the read position back to the beginning of the file. 71 if _, errno = f.Seek(0, io.SeekStart); errno != 0 { 72 b.Fatal(errno) 73 } 74 75 b.StartTimer() 76 if bc.pread { 77 n, errno = f.Pread(buf, 3) 78 } else { 79 n, errno = f.Read(buf) 80 } 81 b.StopTimer() 82 83 if errno != 0 { 84 b.Fatal(errno) 85 } else if bufLen := len(buf); n < bufLen { 86 b.Fatalf("nread %d less than capacity %d", n, bufLen) 87 } 88 } 89 }) 90 } 91 }