initial commit
This commit is contained in:
111
vfs/gc/gc_test.go
Normal file
111
vfs/gc/gc_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package gc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"s1d3sw1ped/SteamCache2/vfs"
|
||||
"s1d3sw1ped/SteamCache2/vfs/memory"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/rand"
|
||||
)
|
||||
|
||||
func TestGCSmallRandom(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := memory.New(1024 * 1024 * 16)
|
||||
gc := New(m, 10, func(vfs vfs.VFS, size int) {
|
||||
tstart := time.Now()
|
||||
deletions := 0
|
||||
targetreclaim := int64(size)
|
||||
var reclaimed int64
|
||||
|
||||
t.Logf("GC starting to reclaim %d bytes", targetreclaim)
|
||||
|
||||
stats := vfs.StatAll()
|
||||
sort.Slice(stats, func(i, j int) bool {
|
||||
// Sort by access time so we can remove the oldest files first.
|
||||
return stats[i].AccessTime().Before(stats[j].AccessTime())
|
||||
})
|
||||
|
||||
// Delete the oldest files until we've reclaimed enough space.
|
||||
for _, s := range stats {
|
||||
sz := s.Size() // Get the size of the file
|
||||
err := vfs.Delete(s.Name())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
reclaimed += sz // Track how much space we've reclaimed
|
||||
deletions++ // Track how many files we've deleted
|
||||
|
||||
// t.Logf("GC deleting %s, %v", s.Name(), s.AccessTime().Format(time.RFC3339Nano))
|
||||
|
||||
if reclaimed >= targetreclaim { // We've reclaimed enough space
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("GC took %v to reclaim %d bytes by deleting %d files", time.Since(tstart), reclaimed, deletions)
|
||||
})
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
if err := gc.Set(fmt.Sprintf("key:%d", i), genRandomData(1024*1, 1024*4)); err != nil {
|
||||
t.Errorf("Set failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if gc.Size() > 1024*1024*16 {
|
||||
t.Errorf("MemoryFS size is %d, want <= 1024", m.Size())
|
||||
}
|
||||
}
|
||||
|
||||
func genRandomData(min int, max int) []byte {
|
||||
data := make([]byte, rand.Intn(max-min)+min)
|
||||
rand.Read(data)
|
||||
return data
|
||||
}
|
||||
|
||||
func TestGCLargeRandom(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := memory.New(1024 * 1024 * 16) // 16MB
|
||||
gc := New(m, 10, func(vfs vfs.VFS, size int) {
|
||||
tstart := time.Now()
|
||||
deletions := 0
|
||||
targetreclaim := int64(size)
|
||||
var reclaimed int64
|
||||
|
||||
t.Logf("GC starting to reclaim %d bytes", targetreclaim)
|
||||
|
||||
stats := vfs.StatAll()
|
||||
sort.Slice(stats, func(i, j int) bool {
|
||||
// Sort by access time so we can remove the oldest files first.
|
||||
return stats[i].AccessTime().Before(stats[j].AccessTime())
|
||||
})
|
||||
|
||||
// Delete the oldest files until we've reclaimed enough space.
|
||||
for _, s := range stats {
|
||||
sz := s.Size() // Get the size of the file
|
||||
vfs.Delete(s.Name())
|
||||
reclaimed += sz // Track how much space we've reclaimed
|
||||
deletions++ // Track how many files we've deleted
|
||||
|
||||
if reclaimed >= targetreclaim { // We've reclaimed enough space
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("GC took %v to reclaim %d bytes by deleting %d files", time.Since(tstart), reclaimed, deletions)
|
||||
})
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
if err := gc.Set(fmt.Sprintf("key:%d", i), genRandomData(1024, 1024*1024)); err != nil {
|
||||
t.Errorf("Set failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if gc.Size() > 1024*1024*16 {
|
||||
t.Errorf("MemoryFS size is %d, want <= 1024", m.Size())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user