-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove nvm and rename tests * fix loading cache race * update ci * update Readme * fix queue race * fix update race * fix policy weight * update race test * fix policy/hashmap inconsistent * fix persist tests * update some comments * minor improvement * atomic sketch * fix test * fix test * fix race * update ci * minor improve * reduce allocation * add a simple run script * minor fix
- Loading branch information
Showing
55 changed files
with
1,154 additions
and
3,086 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package theine | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Yiling-J/theine-go/internal" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func keyGen() []uint64 { | ||
keys := []uint64{} | ||
r := rand.New(rand.NewSource(0)) | ||
z := rand.NewZipf(r, 1.01, 9.0, 200000) | ||
for i := 0; i < 2<<16; i++ { | ||
keys = append(keys, z.Uint64()) | ||
} | ||
return keys | ||
} | ||
|
||
func TestCacheRace_GetSet(t *testing.T) { | ||
for _, size := range []int{500, 2000, 10000, 50000} { | ||
builder := NewBuilder[uint64, uint64](int64(size)) | ||
builder.RemovalListener(func(key, value uint64, reason RemoveReason) {}) | ||
client, err := builder.Build() | ||
require.Nil(t, err) | ||
var wg sync.WaitGroup | ||
keys := keyGen() | ||
|
||
for i := 1; i <= 20; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
rd := rand.Intn(2 << 16) | ||
for i := 0; i < 100000; i++ { | ||
keyGet := keys[(i+rd)&(2<<16-1)] | ||
keyUpdate := keys[(i+3*rd)&(2<<16-1)] | ||
|
||
v, ok := client.Get(keyGet) | ||
if ok && v != keyGet { | ||
panic(keyGet) | ||
} | ||
if !ok { | ||
client.SetWithTTL(keyGet, keyGet, 1, 0) | ||
} | ||
|
||
client.SetWithTTL(keyUpdate, keyUpdate, int64(i%10+1), 0) | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
client.store.Wait() | ||
|
||
require.True( | ||
t, client.Len() < size+internal.WriteBufferSize, | ||
) | ||
|
||
di := client.store.DebugInfo() | ||
|
||
require.Equal(t, client.Len(), int(di.TotalCount())) | ||
require.True(t, di.TotalWeight() <= int64(size+size/10)) | ||
require.Equal(t, di.ProbationWeight, di.ProbationWeightField) | ||
require.Equal(t, di.ProtectedWeight, di.ProtectedWeightField) | ||
|
||
for i := 0; i < len(di.QueueWeight); i++ { | ||
require.Equal(t, di.QueueWeight[i], di.QueueWeightField[i]) | ||
} | ||
|
||
client.store.RangeEntry(func(entry *internal.Entry[uint64, uint64]) { | ||
require.Equal(t, entry.Weight(), entry.PolicyWeight(), entry.Position()) | ||
}) | ||
|
||
client.Close() | ||
} | ||
} | ||
|
||
func TestCacheRace_GetSetDeleteExpire(t *testing.T) { | ||
for _, size := range []int{500, 2000, 10000, 50000} { | ||
builder := NewBuilder[uint64, uint64](int64(size)) | ||
builder.RemovalListener(func(key, value uint64, reason RemoveReason) {}) | ||
client, err := builder.Build() | ||
require.Nil(t, err) | ||
var wg sync.WaitGroup | ||
keys := keyGen() | ||
|
||
for i := 1; i <= 20; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
rd := rand.Intn(2 << 16) | ||
for i := 0; i < 100000; i++ { | ||
key := keys[(i+rd)&(2<<16-1)] | ||
v, ok := client.Get(key) | ||
if ok && v != key { | ||
panic(key) | ||
} | ||
if i%3 == 0 { | ||
client.SetWithTTL(key, key, int64(i%10+1), time.Second*time.Duration(i%25+5)) | ||
} | ||
if i%5 == 0 { | ||
client.Delete(key) | ||
} | ||
if i%5000 == 0 { | ||
client.Range(func(key, value uint64) bool { | ||
return true | ||
}) | ||
} | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
client.store.Wait() | ||
|
||
require.True( | ||
t, client.Len() < size+internal.WriteBufferSize, | ||
) | ||
|
||
di := client.store.DebugInfo() | ||
|
||
require.Equal(t, client.Len(), int(di.TotalCount())) | ||
require.True(t, di.TotalWeight() <= int64(size+size/10)) | ||
require.Equal(t, di.ProbationWeight, di.ProbationWeightField) | ||
require.Equal(t, di.ProtectedWeight, di.ProtectedWeightField) | ||
|
||
for i := 0; i < len(di.QueueWeight); i++ { | ||
require.Equal(t, di.QueueWeight[i], di.QueueWeightField[i]) | ||
} | ||
|
||
client.store.RangeEntry(func(entry *internal.Entry[uint64, uint64]) { | ||
require.Equal(t, entry.Weight(), entry.PolicyWeight(), entry.Position()) | ||
}) | ||
|
||
client.Close() | ||
|
||
} | ||
} |
Oops, something went wrong.