Skip to content

Commit

Permalink
Feature/cache config value (#153)
Browse files Browse the repository at this point in the history
* Cache config value to optimize Get config API

* wrap up cache operations

* refactory test

* re-factory event notification for final consistency

* rm unused code

* rm cyclo

Co-authored-by: zhuohuang yu <[email protected]>
  • Loading branch information
x-xy-y and zhuohuang yu authored Feb 27, 2022
1 parent 2df34b1 commit e216fe5
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 117 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/static_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ jobs:
run: |
go get -u golang.org/x/lint/golint
bash scripts/ci/goLintChecker.sh
- name: Cyclo
run: |
go get github.com/fzipp/gocyclo
bash scripts/ci/goCycloChecker.sh
- name: UT
run: |
go get github.com/stretchr/testify
Expand Down
11 changes: 6 additions & 5 deletions archaius_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package archaius_test
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-archaius/event"
"github.com/go-chassis/openlog"
"github.com/stretchr/testify/assert"
"io"
"os"
"path/filepath"
"testing"
)

type EListener struct{}
Expand Down Expand Up @@ -58,6 +57,8 @@ exist: true
t.Run("add mem config", func(t *testing.T) {
archaius.Set("age", "16")
assert.Equal(t, "16", archaius.Get("age"))
archaius.Set("age", "17")
assert.Equal(t, "17", archaius.Get("age"))
})
t.Run("delete mem config", func(t *testing.T) {
archaius.Delete("age")
Expand Down
58 changes: 58 additions & 0 deletions benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package benchmark

import (
"bytes"
"fmt"
"github.com/go-chassis/go-archaius"
"github.com/stretchr/testify/assert"
"io"
"os"
"path/filepath"
"testing"
)

func createFile(content string, name string, dir string) string {
filename := filepath.Join(dir, name)
f1, _ := os.Create(filename)
_, _ = io.WriteString(f1, content)
f1.Close()
return filename
}

func TestMain(m *testing.M) {
var buff bytes.Buffer
for i:=0;i<99;i++ {
buff.WriteString(fmt.Sprintf("age%d: 1\n", i))
}
buff.WriteString(fmt.Sprintf("age99: 0\n"))
filename := createFile(buff.String(), "f1.yaml", "/tmp")
defer os.Remove(filename)

err := archaius.Init(archaius.WithOptionalFiles([]string{filename}))
if err != nil {
os.Exit(-1)
}
os.Exit(m.Run())
}

func BenchmarkFileSource(b *testing.B) {
s := 0
b.ResetTimer()

for i := 0; i < b.N; i++ {
s += archaius.GetInt("age99", 1)
}
assert.Zero(b, s)
}

func BenchmarkFileSourceParallelism(b *testing.B) {
s := 0
b.ResetTimer()
b.SetParallelism(200)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
s += archaius.GetInt("age99", 1)
}
})
assert.Zero(b, s)
}
78 changes: 78 additions & 0 deletions consistencytest/consistency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package consistencytest

import (
"fmt"
"github.com/go-chassis/go-archaius"
"github.com/go-chassis/go-archaius/source"
"github.com/go-chassis/go-archaius/source/mem"
"github.com/stretchr/testify/assert"
"math/rand"
"sync"
"testing"
"time"
)

type TestSource struct {
Name string
source.ConfigSource
}

func (t *TestSource) GetSourceName() string {
return t.Name
}

func TestFinalConsistencyAfterConcurrentUpdateDelete(t *testing.T) {
err := archaius.Init()
assert.NoError(t, err)

var ms []source.ConfigSource = make([]source.ConfigSource, 3)
for i:=0;i<len(ms);i++ {
ms[i] = &TestSource{
fmt.Sprintf("testSource%d", i),
mem.NewMemoryConfigurationSource(),
}
ms[i].SetPriority(i)
err = archaius.AddSource(ms[i])
assert.NoError(t, err)
}

assert.NoError(t, err)
type configOp func (src source.ConfigSource, i int)
var ops []configOp
ops = append(ops, func (src source.ConfigSource, i int) {
src.Set("age", i)
})
ops = append(ops, func (src source.ConfigSource, i int) {
src.Delete("age")
})
oneTestRun := func () bool {
wg := sync.WaitGroup{}
wg.Add(len(ms))
for _, src := range ms {
go func() {
for i:=0;i<10;i++ {
ops[rand.Int()%len(ops)](src, i)
}
wg.Done()
}()
}
wg.Wait()
time.Sleep(10*time.Microsecond)
var finalVal interface{}
for _, src := range ms {
tmp, err := src.GetConfigurationByKey("age")
if err == nil {
finalVal = tmp
break
}
}
val := archaius.Get("age")
return assert.Equal(t, finalVal, val)
}
for i:=0;i<10000;i++ {
t.Logf("test run %05d", i)
if !oneTestRun() {
break
}
}
}
Loading

0 comments on commit e216fe5

Please sign in to comment.