mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-03-24 13:35:39 +08:00
优化代码
This commit is contained in:
@@ -8,8 +8,8 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Stat device contains the path
|
||||
func Stat(path string) (*StatResult, error) {
|
||||
// StatDevice device contains the path
|
||||
func StatDevice(path string) (*StatResult, error) {
|
||||
var stat = &unix.Statfs_t{}
|
||||
err := unix.Statfs(path, stat)
|
||||
if err != nil {
|
||||
@@ -23,8 +23,8 @@ var cacheMap = map[string]*StatResult{} // path => StatResult
|
||||
|
||||
const cacheLife = 3 // seconds
|
||||
|
||||
// StatCache stat device with cache
|
||||
func StatCache(path string) (*StatResult, error) {
|
||||
// StatDeviceCache stat device with cache
|
||||
func StatDeviceCache(path string) (*StatResult, error) {
|
||||
locker.RLock()
|
||||
stat, ok := cacheMap[path]
|
||||
if ok && stat.updatedAt >= fasttime.Now().Unix()-cacheLife {
|
||||
@@ -36,7 +36,7 @@ func StatCache(path string) (*StatResult, error) {
|
||||
locker.Lock()
|
||||
defer locker.Unlock()
|
||||
|
||||
stat, err := Stat(path)
|
||||
stat, err := StatDevice(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestStat(t *testing.T) {
|
||||
stat, err := fsutils.Stat("/usr/local")
|
||||
stat, err := fsutils.StatDevice("/usr/local")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -19,7 +19,7 @@ func TestStat(t *testing.T) {
|
||||
|
||||
func TestStatCache(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
stat, err := fsutils.StatCache("/usr/local")
|
||||
stat, err := fsutils.StatDeviceCache("/usr/local")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -40,16 +40,16 @@ func TestConcurrent(t *testing.T) {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
_, _ = fsutils.Stat("/usr/local")
|
||||
_, _ = fsutils.StatDevice("/usr/local")
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func BenchmarkStat(b *testing.B) {
|
||||
func BenchmarkStatDevice(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_, err := fsutils.Stat("/usr/local")
|
||||
_, err := fsutils.StatDevice("/usr/local")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
@@ -57,10 +57,10 @@ func BenchmarkStat(b *testing.B) {
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStatCache(b *testing.B) {
|
||||
func BenchmarkStatCacheDevice(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_, err := fsutils.StatCache("/usr/local")
|
||||
_, err := fsutils.StatDeviceCache("/usr/local")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user