Containerizing small messages (< 8192 bytes) gives a perfomance improvment. Since most of requests and service messages like ACKs are quite small, mtproto.Conn
may have a buffer, which packs all messages queued to send as one container.
package mtproto
import (
"fmt"
"testing"
"time"
"go.uber.org/zap"
"github.com/gotd/neo"
"github.com/gotd/td/bin"
"github.com/gotd/td/internal/crypto"
"github.com/gotd/td/internal/proto"
)
func benchContainerize(packets, payloadSize int) func(b *testing.B) {
return func(b *testing.B) {
c := &Conn{
rand: Zero{},
log: zap.NewNop(),
cipher: crypto.NewClientCipher(Zero{}),
clock: neo.NewTime(time.Now()),
compressThreshold: -1,
}
for i := 0; i < 256; i++ {
c.authKey.Value[i] = byte(i)
}
buf := &bin.Buffer{Buf: make([]byte, 0, payloadSize)}
p := testPayload{Data: make([]byte, payloadSize-4)}
b.Run("Pure", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(payloadSize * packets))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for packet := 0; packet < packets; packet++ {
buf.Reset()
if err := c.newEncryptedMessage(12345, 0, p, buf); err != nil {
b.Fatal(err)
}
}
}
})
b.Run("Container", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(int64(payloadSize * packets))
b.ResetTimer()
for i := 0; i < b.N; i++ {
cnt := proto.MessageContainer{Messages: make([]proto.Message, 0, packets)}
for packet := 0; packet < packets; packet++ {
buf.Reset()
if err := buf.Encode(p); err != nil {
b.Fatal(err)
}
cnt.Messages = append(cnt.Messages, proto.Message{
ID: int64(packet),
SeqNo: 0,
Bytes: buf.Len(),
Body: buf.Copy(),
})
}
buf.Reset()
if err := c.newEncryptedMessage(12345, 0, &cnt, buf); err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkContainerize(b *testing.B) {
packetsCount := []int{1, 2, 5, 10, 20}
for _, containerSize := range packetsCount {
b.Run(fmt.Sprintf("%d packet", containerSize), func(b *testing.B) {
for _, payload := range []int{
16,
128,
1024,
8192,
} {
b.Run(fmt.Sprintf("%db", payload), benchContainerize(containerSize, payload))
}
})
}
}
BenchmarkContainerize
BenchmarkContainerize/1_packet
BenchmarkContainerize/1_packet/16b
BenchmarkContainerize/1_packet/16b/Pure
BenchmarkContainerize/1_packet/16b/Pure-12 729921 1535 ns/op 10.42 MB/s 744 B/op 7 allocs/op
BenchmarkContainerize/1_packet/16b/Container
BenchmarkContainerize/1_packet/16b/Container-12 669810 1812 ns/op 8.83 MB/s 824 B/op 9 allocs/op
BenchmarkContainerize/1_packet/128b
BenchmarkContainerize/1_packet/128b/Pure
BenchmarkContainerize/1_packet/128b/Pure-12 601675 2001 ns/op 63.95 MB/s 856 B/op 7 allocs/op
BenchmarkContainerize/1_packet/128b/Container
BenchmarkContainerize/1_packet/128b/Container-12 572652 2165 ns/op 59.13 MB/s 1048 B/op 9 allocs/op
BenchmarkContainerize/1_packet/1024b
BenchmarkContainerize/1_packet/1024b/Pure
BenchmarkContainerize/1_packet/1024b/Pure-12 223212 5429 ns/op 188.63 MB/s 1816 B/op 7 allocs/op
BenchmarkContainerize/1_packet/1024b/Container
BenchmarkContainerize/1_packet/1024b/Container-12 204759 5805 ns/op 176.40 MB/s 2889 B/op 9 allocs/op
BenchmarkContainerize/1_packet/8192b
BenchmarkContainerize/1_packet/8192b/Pure
BenchmarkContainerize/1_packet/8192b/Pure-12 36762 32553 ns/op 251.65 MB/s 10148 B/op 7 allocs/op
BenchmarkContainerize/1_packet/8192b/Container
BenchmarkContainerize/1_packet/8192b/Container-12 34714 34467 ns/op 237.67 MB/s 18401 B/op 9 allocs/op
BenchmarkContainerize/2_packet
BenchmarkContainerize/2_packet/16b
BenchmarkContainerize/2_packet/16b/Pure
BenchmarkContainerize/2_packet/16b/Pure-12 400753 3098 ns/op 10.33 MB/s 1488 B/op 14 allocs/op
BenchmarkContainerize/2_packet/16b/Container
BenchmarkContainerize/2_packet/16b/Container-12 633271 1957 ns/op 16.35 MB/s 920 B/op 10 allocs/op
BenchmarkContainerize/2_packet/128b
BenchmarkContainerize/2_packet/128b/Pure
BenchmarkContainerize/2_packet/128b/Pure-12 302653 4044 ns/op 63.31 MB/s 1712 B/op 14 allocs/op
BenchmarkContainerize/2_packet/128b/Container
BenchmarkContainerize/2_packet/128b/Container-12 409842 2943 ns/op 86.98 MB/s 1368 B/op 10 allocs/op
BenchmarkContainerize/2_packet/1024b
BenchmarkContainerize/2_packet/1024b/Pure
BenchmarkContainerize/2_packet/1024b/Pure-12 109617 10881 ns/op 188.22 MB/s 3633 B/op 14 allocs/op
BenchmarkContainerize/2_packet/1024b/Container
BenchmarkContainerize/2_packet/1024b/Container-12 116788 10152 ns/op 201.74 MB/s 5115 B/op 10 allocs/op
BenchmarkContainerize/2_packet/8192b
BenchmarkContainerize/2_packet/8192b/Pure
BenchmarkContainerize/2_packet/8192b/Pure-12 18438 65221 ns/op 251.21 MB/s 20297 B/op 14 allocs/op
BenchmarkContainerize/2_packet/8192b/Container
BenchmarkContainerize/2_packet/8192b/Container-12 17704 67509 ns/op 242.69 MB/s 35702 B/op 10 allocs/op
BenchmarkContainerize/5_packet
BenchmarkContainerize/5_packet/16b
BenchmarkContainerize/5_packet/16b/Pure
BenchmarkContainerize/5_packet/16b/Pure-12 156262 7738 ns/op 10.34 MB/s 3721 B/op 35 allocs/op
BenchmarkContainerize/5_packet/16b/Container
BenchmarkContainerize/5_packet/16b/Container-12 456072 2661 ns/op 30.06 MB/s 1208 B/op 13 allocs/op
BenchmarkContainerize/5_packet/128b
BenchmarkContainerize/5_packet/128b/Pure
BenchmarkContainerize/5_packet/128b/Pure-12 117830 10034 ns/op 63.78 MB/s 4281 B/op 35 allocs/op
BenchmarkContainerize/5_packet/128b/Container
BenchmarkContainerize/5_packet/128b/Container-12 250303 4831 ns/op 132.46 MB/s 2441 B/op 13 allocs/op
BenchmarkContainerize/5_packet/1024b
BenchmarkContainerize/5_packet/1024b/Pure
BenchmarkContainerize/5_packet/1024b/Pure-12 44348 27051 ns/op 189.27 MB/s 9084 B/op 35 allocs/op
BenchmarkContainerize/5_packet/1024b/Container
BenchmarkContainerize/5_packet/1024b/Container-12 52507 22537 ns/op 227.19 MB/s 11424 B/op 13 allocs/op
BenchmarkContainerize/5_packet/8192b
BenchmarkContainerize/5_packet/8192b/Pure
BenchmarkContainerize/5_packet/8192b/Pure-12 7506 162648 ns/op 251.83 MB/s 50739 B/op 35 allocs/op
BenchmarkContainerize/5_packet/8192b/Container
BenchmarkContainerize/5_packet/8192b/Container-12 7534 166697 ns/op 245.72 MB/s 92731 B/op 13 allocs/op
BenchmarkContainerize/10_packet
BenchmarkContainerize/10_packet/16b
BenchmarkContainerize/10_packet/16b/Pure
BenchmarkContainerize/10_packet/16b/Pure-12 78097 15449 ns/op 10.36 MB/s 7443 B/op 70 allocs/op
BenchmarkContainerize/10_packet/16b/Container
BenchmarkContainerize/10_packet/16b/Container-12 344941 3517 ns/op 45.50 MB/s 1689 B/op 18 allocs/op
BenchmarkContainerize/10_packet/128b
BenchmarkContainerize/10_packet/128b/Pure
BenchmarkContainerize/10_packet/128b/Pure-12 59557 20103 ns/op 63.67 MB/s 8563 B/op 70 allocs/op
BenchmarkContainerize/10_packet/128b/Container
BenchmarkContainerize/10_packet/128b/Container-12 146731 8191 ns/op 156.26 MB/s 3964 B/op 18 allocs/op
BenchmarkContainerize/10_packet/1024b
BenchmarkContainerize/10_packet/1024b/Pure
BenchmarkContainerize/10_packet/1024b/Pure-12 22119 54357 ns/op 188.38 MB/s 18169 B/op 70 allocs/op
BenchmarkContainerize/10_packet/1024b/Container
BenchmarkContainerize/10_packet/1024b/Container-12 27255 44071 ns/op 232.35 MB/s 22401 B/op 18 allocs/op
BenchmarkContainerize/10_packet/8192b
BenchmarkContainerize/10_packet/8192b/Pure
BenchmarkContainerize/10_packet/8192b/Pure-12 3759 329537 ns/op 248.59 MB/s 101476 B/op 70 allocs/op
BenchmarkContainerize/10_packet/8192b/Container
BenchmarkContainerize/10_packet/8192b/Container-12 3643 334902 ns/op 244.61 MB/s 180084 B/op 18 allocs/op
BenchmarkContainerize/20_packet
BenchmarkContainerize/20_packet/16b
BenchmarkContainerize/20_packet/16b/Pure
BenchmarkContainerize/20_packet/16b/Pure-12 38294 30865 ns/op 10.37 MB/s 14886 B/op 140 allocs/op
BenchmarkContainerize/20_packet/16b/Container
BenchmarkContainerize/20_packet/16b/Container-12 214332 5571 ns/op 57.44 MB/s 2714 B/op 28 allocs/op
BenchmarkContainerize/20_packet/128b
BenchmarkContainerize/20_packet/128b/Pure
BenchmarkContainerize/20_packet/128b/Pure-12 29757 40106 ns/op 63.83 MB/s 17127 B/op 140 allocs/op
BenchmarkContainerize/20_packet/128b/Container
BenchmarkContainerize/20_packet/128b/Container-12 82035 14562 ns/op 175.79 MB/s 7333 B/op 28 allocs/op
BenchmarkContainerize/20_packet/1024b
BenchmarkContainerize/20_packet/1024b/Pure
BenchmarkContainerize/20_packet/1024b/Pure-12 10000 108660 ns/op 188.48 MB/s 36339 B/op 140 allocs/op
BenchmarkContainerize/20_packet/1024b/Container
BenchmarkContainerize/20_packet/1024b/Container-12 13837 86377 ns/op 237.10 MB/s 44680 B/op 28 allocs/op
BenchmarkContainerize/20_packet/8192b
BenchmarkContainerize/20_packet/8192b/Pure
BenchmarkContainerize/20_packet/8192b/Pure-12 1850 647382 ns/op 253.08 MB/s 202955 B/op 140 allocs/op
BenchmarkContainerize/20_packet/8192b/Container
BenchmarkContainerize/20_packet/8192b/Container-12 1795 656087 ns/op 249.72 MB/s 350308 B/op 28 allocs/op
PASS
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too