2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2022-06-07 08:25:34 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-10-27 10:00:35 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func TestEncode(t *testing.T) {
|
|
|
|
testCases := []string{
|
|
|
|
`REP 1 IN X
|
|
|
|
CBF 1
|
|
|
|
SELECT 2 IN SAME Location FROM * AS X`,
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
`REP 1
|
|
|
|
SELECT 2 IN City FROM Good
|
|
|
|
FILTER Country EQ RU AS FromRU
|
|
|
|
FILTER @FromRU AND Rating GT 7 AS Good`,
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
`REP 7 IN SPB
|
|
|
|
SELECT 1 IN City FROM SPBSSD AS SPB
|
|
|
|
FILTER City EQ SPB AND SSD EQ true OR City EQ SPB AND Rating GE 5 AS SPBSSD`,
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for _, testCase := range testCases {
|
|
|
|
var p PlacementPolicy
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.NoError(t, p.DecodeString(testCase))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var b strings.Builder
|
|
|
|
require.NoError(t, p.WriteStringTo(&b))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
require.Equal(t, testCase, b.String())
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
type cache struct {
|
|
|
|
mtx sync.RWMutex
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
item map[string]struct{}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *cache) add(key string) {
|
|
|
|
x.mtx.Lock()
|
|
|
|
x.item[key] = struct{}{}
|
|
|
|
x.mtx.Unlock()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *cache) has(key string) bool {
|
|
|
|
x.mtx.RLock()
|
|
|
|
_, ok := x.item[key]
|
|
|
|
x.mtx.RUnlock()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return ok
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func BenchmarkCache(b *testing.B) {
|
|
|
|
c := cache{
|
|
|
|
item: make(map[string]struct{}),
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var key string
|
|
|
|
buf := make([]byte, 32)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
rand.Read(buf)
|
|
|
|
key = string(buf)
|
|
|
|
b.StartTimer()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c.add(key)
|
|
|
|
c.has(key)
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
type cacheP struct {
|
|
|
|
mtx *sync.RWMutex
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
item map[string]struct{}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x cacheP) add(key string) {
|
|
|
|
x.mtx.Lock()
|
|
|
|
x.item[key] = struct{}{}
|
|
|
|
x.mtx.Unlock()
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x cacheP) has(key string) bool {
|
|
|
|
x.mtx.RLock()
|
|
|
|
_, ok := x.item[key]
|
|
|
|
x.mtx.RUnlock()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return ok
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func BenchmarkCacheP(b *testing.B) {
|
|
|
|
c := cacheP{
|
|
|
|
mtx: &sync.RWMutex{},
|
|
|
|
item: make(map[string]struct{}),
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var key string
|
|
|
|
buf := make([]byte, 32)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
|
|
|
rand.Read(buf)
|
|
|
|
key = string(buf)
|
|
|
|
b.StartTimer()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
c.add(key)
|
|
|
|
c.has(key)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|