Skip to content

Commit

Permalink
Add more tests and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
guelfey committed Apr 2, 2013
1 parent ffe274e commit 4a4a64b
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 20 deletions.
79 changes: 59 additions & 20 deletions proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestProtoVariantStruct(t *testing.T) {
}
}

func TestStructTag(t *testing.T) {
func TestProtoStructTag(t *testing.T) {
type Bar struct {
A int32
B chan interface{} `dbus:"-"`
Expand All @@ -151,31 +151,52 @@ func TestStructTag(t *testing.T) {
}
}

var someMessage = &Message{
// ordinary org.freedesktop.DBus.Hello call
var smallMessage = &Message{
Order: binary.LittleEndian,
Type: TypeMethodCall,
Flags: 0,
serial: 42,
serial: 1,
Headers: map[HeaderField]Variant{
FieldPath: MakeVariant(ObjectPath("/foo/bar")),
FieldInterface: MakeVariant("foo.bar"),
FieldMember: MakeVariant("Baz"),
FieldDestination: MakeVariant("foo.bar"),
FieldSignature: MakeVariant(Signature{"ai(uu)"}),
FieldDestination: MakeVariant("org.freedesktop.DBus"),
FieldPath: MakeVariant(ObjectPath("/org/freedesktop/DBus")),
FieldInterface: MakeVariant("org.freedesktop.DBus"),
FieldMember: MakeVariant("Hello"),
},
}

// org.freedesktop.Notifications.Notify
var bigMessage = &Message{
Order: binary.LittleEndian,
Type: TypeMethodCall,
serial: 2,
Headers: map[HeaderField]Variant{
FieldDestination: MakeVariant("org.freedesktop.Notifications"),
FieldPath: MakeVariant(ObjectPath("/org/freedesktop/Notifications")),
FieldInterface: MakeVariant("org.freedesktop.Notifications"),
FieldMember: MakeVariant("Notify"),
FieldSignature: MakeVariant(Signature{"susssasa{sv}i"}),
},
Body: []interface{}{
[]int32{12643, 17888, 20569},
struct{ n, m uint32 }{0, 1},
"app_name",
uint32(0),
"dialog-information",
"Notification",
"This is the body of a notification",
[]string{"ok", "Ok"},
map[string]Variant{
"sound-name": MakeVariant("dialog-information"),
},
int32(-1),
},
}

func BenchmarkDecodeMessage(b *testing.B) {
func BenchmarkDecodeMessageSmall(b *testing.B) {
var err error
var rd *bytes.Reader

b.StopTimer()
buf := new(bytes.Buffer)
err = someMessage.EncodeTo(buf)
err = smallMessage.EncodeTo(buf)
if err != nil {
b.Fatal(err)
}
Expand All @@ -190,25 +211,43 @@ func BenchmarkDecodeMessage(b *testing.B) {
}
}

func BenchmarkEncodeMessage(b *testing.B) {
func BenchmarkDecodeMessageBig(b *testing.B) {
var err error
var rd *bytes.Reader

b.StopTimer()
buf := new(bytes.Buffer)
err = bigMessage.EncodeTo(buf)
if err != nil {
b.Fatal(err)
}
decoded := buf.Bytes()
b.StartTimer()
for i := 0; i < b.N; i++ {
err = someMessage.EncodeTo(ioutil.Discard)
rd = bytes.NewReader(decoded)
_, err = DecodeMessage(rd)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkGetSignature(b *testing.B) {
func BenchmarkEncodeMessageSmall(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
GetSignature(someMessage.Body...)
err = smallMessage.EncodeTo(ioutil.Discard)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkSignatureValues(b *testing.B) {
s := Signature{"a(bits)a{sv}"}
func BenchmarkEncodeMessageBig(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
s.Values()
err = bigMessage.EncodeTo(ioutil.Discard)
if err != nil {
b.Fatal(err)
}
}
}
63 changes: 63 additions & 0 deletions sig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ var sigTests = []struct {
[]interface{}{new(int32)},
Signature{"i"},
},
{
[]interface{}{new(string)},
Signature{"s"},
},
{
[]interface{}{new(Signature)},
Signature{"g"},
},
{
[]interface{}{new([]int16)},
Signature{"an"},
},
{
[]interface{}{new(int16), new(uint32)},
Signature{"nu"},
},
{
[]interface{}{new(map[byte]Variant)},
Signature{"a{yv}"},
},
{
[]interface{}{new(Variant), new([]map[int32]string)},
Signature{"vaa{is}"},
Expand All @@ -37,3 +57,46 @@ func TestSig(t *testing.T) {
}
}
}

func TestSigStructSlice(t *testing.T) {
sig := Signature{"a(i)"}
if reflect.TypeOf(sig.Values()[0]) != reflect.TypeOf(new([][]interface{})) {
t.Errorf("got type: %s", reflect.TypeOf(sig.Values()[0]))
}
}

var getSigTest = []interface{}{
[]struct {
b byte
i int32
t uint64
s string
}{},
map[string]Variant{},
}

func BenchmarkGetSignatureSimple(b *testing.B) {
for i := 0; i < b.N; i++ {
GetSignature("", int32(0))
}
}

func BenchmarkGetSignatureLong(b *testing.B) {
for i := 0; i < b.N; i++ {
GetSignature(getSigTest...)
}
}

func BenchmarkSignatureValuesSimple(b *testing.B) {
s := Signature{"si"}
for i := 0; i < b.N; i++ {
s.Values()
}
}

func BenchmarkSignatureValuesLong(b *testing.B) {
s := Signature{"a(bits)a{sv}"}
for i := 0; i < b.N; i++ {
s.Values()
}
}

0 comments on commit 4a4a64b

Please sign in to comment.