forked from smfrpc/smf
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go: header build performance and tests (smfrpc#273)
Just because we have some trickery with allocating more bytes and trimming first 4 bytes it's better to test it continously if go flatbuffers library would decide to change implementation and break it. I benchmarked few sizes and here are the results and they're pretty consistent, works best on 32 bytes buffer. BenchmarkBuildHeader_20-12 5000000 277 ns/op 176 B/op 4 allocs/op BenchmarkBuildHeader_24-12 5000000 276 ns/op 176 B/op 4 allocs/op BenchmarkBuildHeader_25-12 10000000 196 ns/op 96 B/op 2 allocs/op BenchmarkBuildHeader_26-12 10000000 196 ns/op 96 B/op 2 allocs/op BenchmarkBuildHeader_28-12 10000000 194 ns/op 96 B/op 2 allocs/op BenchmarkBuildHeader_32-12 10000000 194 ns/op 96 B/op 2 allocs/op BenchmarkBuildHeader_48-12 10000000 196 ns/op 112 B/op 2 allocs/op ... and worse from there if size increases. Benchmark is included.
- Loading branch information
Showing
3 changed files
with
65 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package smf | ||
|
||
import ( | ||
cryptorand "crypto/rand" | ||
"io" | ||
"math" | ||
"math/rand" | ||
. "testing" | ||
|
||
"github.com/cespare/xxhash" | ||
flatbuffers "github.com/google/flatbuffers/go" | ||
) | ||
|
||
func TestBuildHeader(t *T) { | ||
for index := 0; index < 10; index++ { | ||
meta := uint32(rand.Intn(65535)) | ||
sess := uint16(rand.Intn(65535)) | ||
body := make([]byte, rand.Intn(200)) | ||
io.ReadFull(cryptorand.Reader, body) | ||
hbody := BuildHeader(sess, body, meta) | ||
assertEqual(t, 16, len(hbody)) | ||
hdr := NewHeader(hbody) | ||
assertEqual(t, meta, hdr.Meta()) | ||
assertEqual(t, sess, hdr.Session()) | ||
assertEqual(t, uint32(len(body)), hdr.Size()) | ||
checksum := uint32(math.MaxUint32 & xxhash.Sum64(body)) | ||
assertEqual(t, checksum, hdr.Checksum()) | ||
} | ||
} | ||
|
||
func BenchmarkBuildHeader(b *B) { | ||
meta := uint32(rand.Intn(65535)) | ||
sess := uint16(rand.Intn(65535)) | ||
body := make([]byte, rand.Intn(200)) | ||
checksum := math.MaxUint32 & xxhash.Sum64(body) | ||
io.ReadFull(cryptorand.Reader, body) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
builder := flatbuffers.NewBuilder(32) // [1] | ||
res := CreateHeader(builder, | ||
0, // compression int8, | ||
0, // bitflags int8, | ||
sess, // session uint16, | ||
uint32(len(body)), // size uint32, | ||
uint32(checksum), // checksum uint32, | ||
meta, // meta uint32 | ||
) | ||
builder.Finish(res) | ||
// TODO(crackcomm): builder prepends 4 bytes | ||
// the header is the last 16 bytes of message | ||
// so I did [^1] 32 bytes allocation anyway | ||
// I have no idea why it does that | ||
// _ = builder.FinishedBytes() | ||
} | ||
} | ||
|
||
func assertEqual(t *T, in, out interface{}) { | ||
if in != out { | ||
t.Helper() | ||
t.Fatalf("expected %v got %v", in, out) | ||
} | ||
} |
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