Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
refactoring handle constrains of lru cache
Browse files Browse the repository at this point in the history
adding benchmark tests
  • Loading branch information
willie68 committed Jan 9, 2022
1 parent 6a42591 commit ac95f94
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
17 changes: 5 additions & 12 deletions internal/celproc/celproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ func ProcCel(celModel model.CelModel) (model.CelResult, error) {
var prg cel.Program
id := celModel.Identifier
if id != "" {
entry, ok := lrucache.Get(id)
var entry LRUEntry
entry, ok = lrucache.Get(id)
if ok {
prg = entry.Program
}
Expand Down Expand Up @@ -115,26 +116,18 @@ func ProcCel(celModel model.CelModel) (model.CelResult, error) {
ID: id,
Program: prg,
})
for {
id := lrucache.HandleContrains()
if id != "" {
lrucache.Delete(id)
} else {
break
}
}

go lrucache.HandleContrains()
}
}
out, details, err := prg.Eval(context)
fmt.Printf("result: %v\ndetails: %v\nerror: %v\n", out, details, err)
//fmt.Printf("result: %v\ndetails: %v\nerror: %v\n", out, details, err)

if err != nil {
log.Logger.Errorf("program evaluation error: %v", err)

return model.CelResult{
Error: fmt.Sprintf("%v", err),
Message: fmt.Sprintf("program evaluation error: %s", err.Error()),
Message: fmt.Sprintf("program evaluation error: %s\r\ndetails: %s", err.Error(), details),
}, err
}
switch v := out.(type) {
Expand Down
45 changes: 42 additions & 3 deletions internal/celproc/celproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"testing"
"time"

"google.golang.org/protobuf/types/known/structpb"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -44,11 +45,33 @@ func TestJson(t *testing.T) {
}
}

func TestJsonMany(t *testing.T) {
func BenchmarkJsonManyWithoutCache(t *testing.B) {
ast := assert.New(t)

celModels := readJson("../../test/data/data1.json", t)
for i := 0; i < 1000; i++ {
celModels := readJsonB("../../test/data/data1.json", t)
stt := time.Now()
for i := 0; i < 10000; i++ {
for _, cm := range celModels {
cm.Request.Context = convertJson2Map(cm.Request.Context)
cm.Request.Identifier = "" //fmt.Sprintf("%d_%d", i, x)
result, err := ProcCel(cm.Request)
ast.Nil(err)
ast.NotNil(result)

ast.Equal(cm.Result, result.Result)
}
}
ste := time.Now()

t.Logf("execution: %d", ste.Sub(stt).Milliseconds())
}

func BenchmarkJsonManyWithCache(t *testing.B) {
ast := assert.New(t)

celModels := readJsonB("../../test/data/data1.json", t)
stt := time.Now()
for i := 0; i < 10000; i++ {
for _, cm := range celModels {
cm.Request.Context = convertJson2Map(cm.Request.Context)
result, err := ProcCel(cm.Request)
Expand All @@ -58,6 +81,9 @@ func TestJsonMany(t *testing.T) {
ast.Equal(cm.Result, result.Result)
}
}
ste := time.Now()

t.Logf("execution: %d", ste.Sub(stt).Milliseconds())
}

func TestGRPCJson(t *testing.T) {
Expand Down Expand Up @@ -104,3 +130,16 @@ func readJson(filename string, t *testing.T) []model.TestCelModel {
ast.Nil(err)
return celModels
}

func readJsonB(filename string, t *testing.B) []model.TestCelModel {
ast := assert.New(t)
ya, err := ioutil.ReadFile(filename)
ast.Nil(err)
var celModels []model.TestCelModel
decoder := json.NewDecoder(bytes.NewReader(ya))
decoder.UseNumber()
err = decoder.Decode(&celModels)
//err = json.Unmarshal(ya, &celModels)
ast.Nil(err)
return celModels
}
16 changes: 15 additions & 1 deletion internal/celproc/lrulist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type LRUList struct {
MaxCount int
entries []LRUEntry
dmu sync.Mutex
cstLock sync.Mutex
}

func (l *LRUList) Init() {
Expand Down Expand Up @@ -66,7 +67,20 @@ func (l *LRUList) GetFullIDList() []string {
return ids
}

func (l *LRUList) HandleContrains() string {
func (l *LRUList) HandleContrains() {
l.cstLock.Lock()
defer l.cstLock.Unlock()
for {
id := l.getSingleContrained()
if id != "" {
l.Delete(id)
} else {
break
}
}
}

func (l *LRUList) getSingleContrained() string {
var id string
l.dmu.Lock()
defer l.dmu.Unlock()
Expand Down

0 comments on commit ac95f94

Please sign in to comment.