-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cache_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/scorix/grib-go/pkg/grib2/cache" | ||
) | ||
|
||
// 实现一个简单的 GridDataSource | ||
type testDataSource struct{} | ||
|
||
func (t *testDataSource) ReadGridAt(ctx context.Context, index int) (float32, error) { | ||
return 1.0, nil | ||
} | ||
|
||
func BenchmarkCustom_ReadGridAt_Parallel(b *testing.B) { | ||
store := cache.NewMapStore() | ||
|
||
c := cache.NewCustom( | ||
func(lat, lon float32) bool { | ||
return true | ||
}, | ||
&testDataSource{}, | ||
store, | ||
) | ||
|
||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_, err := c.ReadGridAt(context.Background(), 1, 1, 1) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
}) | ||
} |
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,53 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func BenchmarkStore_Parallel(b *testing.B) { | ||
b.Run("LRU", func(b *testing.B) { | ||
store := NewLRUStore(1000) | ||
ctx := context.Background() | ||
|
||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
store.Set(ctx, 1, 1.0) | ||
_, _ = store.Get(ctx, 1) | ||
} | ||
}) | ||
}) | ||
|
||
b.Run("Map", func(b *testing.B) { | ||
store := NewMapStore() | ||
ctx := context.Background() | ||
|
||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
store.Set(ctx, 1, 1.0) | ||
_, _ = store.Get(ctx, 1) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
func TestLRUStore(t *testing.T) { | ||
store := NewLRUStore(1) | ||
ctx := context.Background() | ||
|
||
store.Set(ctx, 1, 1.0) | ||
v, ok := store.Get(ctx, 1) | ||
assert.True(t, ok) | ||
assert.Equal(t, float32(1.0), v) | ||
|
||
store.Set(ctx, 2, 2.0) | ||
v, ok = store.Get(ctx, 2) | ||
assert.True(t, ok) | ||
assert.Equal(t, float32(2.0), v) | ||
|
||
v, ok = store.Get(ctx, 3) | ||
assert.False(t, ok) | ||
assert.Equal(t, float32(0.0), v) | ||
} |