github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/fs/stat_test.go (about) 1 // Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package fsutils_test 4 5 import ( 6 fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs" 7 "sync" 8 "testing" 9 "time" 10 ) 11 12 func TestStat(t *testing.T) { 13 stat, err := fsutils.StatDevice("/usr/local") 14 if err != nil { 15 t.Fatal(err) 16 } 17 t.Log("free:", stat.FreeSize()/(1<<30), "total:", stat.TotalSize()/(1<<30), "used:", stat.UsedSize()/(1<<30)) 18 } 19 20 func TestStatCache(t *testing.T) { 21 for i := 0; i < 10; i++ { 22 stat, err := fsutils.StatDeviceCache("/usr/local") 23 if err != nil { 24 t.Fatal(err) 25 } 26 t.Log("free:", stat.FreeSize()/(1<<30), "total:", stat.TotalSize()/(1<<30), "used:", stat.UsedSize()/(1<<30)) 27 } 28 } 29 30 func TestConcurrent(t *testing.T) { 31 var before = time.Now() 32 defer func() { 33 t.Log(time.Since(before).Seconds()*1000, "ms") 34 }() 35 36 var count = 10000 37 var wg = sync.WaitGroup{} 38 wg.Add(count) 39 for i := 0; i < count; i++ { 40 go func() { 41 defer wg.Done() 42 43 _, _ = fsutils.StatDevice("/usr/local") 44 }() 45 } 46 wg.Wait() 47 } 48 49 func BenchmarkStatDevice(b *testing.B) { 50 b.RunParallel(func(pb *testing.PB) { 51 for pb.Next() { 52 _, err := fsutils.StatDevice("/usr/local") 53 if err != nil { 54 b.Fatal(err) 55 } 56 } 57 }) 58 } 59 60 func BenchmarkStatCacheDevice(b *testing.B) { 61 b.RunParallel(func(pb *testing.PB) { 62 for pb.Next() { 63 _, err := fsutils.StatDeviceCache("/usr/local") 64 if err != nil { 65 b.Fatal(err) 66 } 67 } 68 }) 69 }