Skip to content

Instantly share code, notes, and snippets.

@tobowers
Created July 17, 2018 10:04
Show Gist options
  • Save tobowers/d7a0e05d68459da54808a65c145c50d5 to your computer and use it in GitHub Desktop.
Save tobowers/d7a0e05d68459da54808a65c145c50d5 to your computer and use it in GitHub Desktop.
package serialization
import (
"github.com/polydawn/refmt/obj/atlas"
"testing"
"time"
"github.com/polydawn/refmt/cbor"
"github.com/stretchr/testify/assert"
)
var cborAtlas atlas.Atlas
//var atlasEntries = []*atlas.AtlasEntry{cidAtlasEntry, bigIntAtlasEntry}
var atlasEntries = make([]*atlas.AtlasEntry,0)
func init() {
cborAtlas = atlas.MustBuild()
RegisterCborType(testEncodeStruct{})
}
// RegisterCborType allows to register a custom cbor type
func RegisterCborType(i interface{}) {
var entry *atlas.AtlasEntry
if ae, ok := i.(*atlas.AtlasEntry); ok {
entry = ae
} else {
entry = atlas.BuildEntry(i).StructMap().Autogenerate().Complete()
}
atlasEntries = append(atlasEntries, entry)
cborAtlas = atlas.MustBuild(atlasEntries...)
}
type testEncodeStruct struct {
Id string
Number int
}
func TestParallelWrap(t *testing.T) {
times := 10000
obj := &testEncodeStruct{Id: "test", Number: 100}
responses := make(chan int, times)
for i := 0; i < times; i++ {
go func() {
var m interface{}
now := time.Now()
data, err := cbor.MarshalAtlased(obj, cborAtlas)
if err != nil {
panic("error marshaling")
}
cbor.UnmarshalAtlased(data, &m, cborAtlas)
end := int(time.Now().Sub(now))
assert.Nil(t, err)
assert.Len(t, data, 18)
responses <- end
}()
}
respSlice := make([]int, times)
for i := 0; i < times; i++ {
respSlice[i] = <-responses
}
t.Logf("max was: %v", max(respSlice))
assert.True(t, max(respSlice)< int(100 * time.Millisecond))
}
func max(ints []int) int {
max := 0
for i := 0; i < len(ints); i++ {
if ints[i] > max {
max = ints[i]
}
}
return max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment