| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package electron |
| 21 |
|
| 22 |
import ( |
| 23 |
"flag" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/apache/qpid-proton/go/pkg/amqp" |
| 29 |
"github.com/apache/qpid-proton/go/pkg/internal/test" |
| 30 |
) |
| 31 |
|
| 32 |
// To change capacity use |
| 33 |
// go test -bench=. -args -capacity 100 |
| 34 |
var capacity = flag.Int("capacity", 1000, "Prefetch capacity") |
| 35 |
var bodySize = flag.Int("bodySize", 1000, "Message body size") |
| 36 |
|
| 37 |
type bmCommon struct { |
| 38 |
b *testing.B |
| 39 |
p *pair |
| 40 |
s Sender |
| 41 |
r Receiver |
| 42 |
ack chan Outcome |
| 43 |
done sync.WaitGroup |
| 44 |
} |
| 45 |
|
| 46 |
func newBmCommon(p *pair, waitCount int) *bmCommon { |
| 47 |
bm := bmCommon{p: p, b: p.t.(*testing.B)} |
| 48 |
bm.p.capacity = *capacity |
| 49 |
bm.p.prefetch = true |
| 50 |
bm.s, bm.r = p.sender() |
| 51 |
bm.ack = make(chan Outcome, *capacity) |
| 52 |
bm.done.Add(waitCount) |
| 53 |
bm.b.ResetTimer() |
| 54 |
return &bm |
| 55 |
} |
| 56 |
|
| 57 |
func (bm *bmCommon) receiveAccept() { |
| 58 |
defer bm.done.Done() |
| 59 |
for n := 0; n < bm.b.N; n++ { |
| 60 |
if rm, err := bm.r.Receive(); err != nil { |
| 61 |
bm.b.Fatal(err) |
| 62 |
} else { |
| 63 |
test.FatalIf(bm.b, rm.Accept()) |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func (bm *bmCommon) outcomes() { |
| 69 |
defer bm.done.Done() |
| 70 |
for n := 0; n < bm.b.N; n++ { |
| 71 |
test.FatalIf(bm.b, (<-bm.ack).Error) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
var emptyMsg = amqp.NewMessage() |
| 76 |
|
| 77 |
func BenchmarkSendForget(b *testing.B) { |
| 78 |
bm := newBmCommon(newPipe(b, nil, nil), 1) |
| 79 |
defer bm.p.close() |
| 80 |
|
| 81 |
go func() { // Receive, no ack |
| 82 |
defer bm.done.Done() |
| 83 |
for n := 0; n < b.N; n++ { |
| 84 |
if _, err := bm.r.Receive(); err != nil { |
| 85 |
b.Fatal(err) |
| 86 |
} |
| 87 |
} |
| 88 |
}() |
| 89 |
|
| 90 |
for n := 0; n < b.N; n++ { |
| 91 |
bm.s.SendForget(emptyMsg) |
| 92 |
} |
| 93 |
bm.done.Wait() |
| 94 |
} |
| 95 |
|
| 96 |
func BenchmarkSendSync(b *testing.B) { |
| 97 |
bm := newBmCommon(newPipe(b, nil, nil), 1) |
| 98 |
defer bm.p.close() |
| 99 |
|
| 100 |
go bm.receiveAccept() |
| 101 |
for n := 0; n < b.N; n++ { |
| 102 |
test.FatalIf(b, bm.s.SendSync(emptyMsg).Error) |
| 103 |
} |
| 104 |
bm.done.Wait() |
| 105 |
} |
| 106 |
|
| 107 |
func BenchmarkSendAsync(b *testing.B) { |
| 108 |
bm := newBmCommon(newPipe(b, nil, nil), 2) |
| 109 |
defer bm.p.close() |
| 110 |
|
| 111 |
go bm.outcomes() // Handle outcomes |
| 112 |
go bm.receiveAccept() // Receive |
| 113 |
for n := 0; n < b.N; n++ { |
| 114 |
bm.s.SendAsync(emptyMsg, bm.ack, nil) |
| 115 |
} |
| 116 |
bm.done.Wait() |
| 117 |
} |
| 118 |
|
| 119 |
// Create a new message for each send, with body and property. |
| 120 |
func BenchmarkSendAsyncNewMessage(b *testing.B) { |
| 121 |
body := strings.Repeat("x", *bodySize) |
| 122 |
bm := newBmCommon(newPipe(b, nil, nil), 2) |
| 123 |
defer bm.p.close() |
| 124 |
|
| 125 |
go bm.outcomes() // Handle outcomes |
| 126 |
go bm.receiveAccept() // Receive |
| 127 |
for n := 0; n < b.N; n++ { |
| 128 |
msg := amqp.NewMessageWith(body) |
| 129 |
msg.SetApplicationProperties(map[string]interface{}{"prop": "value"}) |
| 130 |
bm.s.SendAsync(msg, bm.ack, nil) |
| 131 |
} |
| 132 |
bm.done.Wait() |
| 133 |
} |