-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
6 changed files
with
212 additions
and
117 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
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) | ||
} |
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,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 | ||
} | ||
} | ||
} |
Oops, something went wrong.