Skip to content

Commit

Permalink
feat: add benchmarks (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hchenn authored Aug 15, 2022
1 parent d4ce3a9 commit f6138a7
Show file tree
Hide file tree
Showing 20 changed files with 6,615 additions and 83 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: typos-action
uses: crate-ci/typos@master

staticcheck:
runs-on: self-hosted
steps:
Expand Down
2 changes: 2 additions & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ header:
paths-ignore:
- wire.go
- examples/fastpb_gen/**
- benchmark/fastpb_gen/**
- benchmark/gogo_gen/**

comment: on-failure
98 changes: 98 additions & 0 deletions benchmark/benchmark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package benchmark

import (
"math/rand"
)

const (
MockLen = 1024
MockCap = 16
)

func randListInt32(list []int32) {
for i := range list {
list[i] = rand.Int31()
}
}

func randListInt64(list []int64) {
for i := range list {
list[i] = rand.Int63()
}
}

func randListUint32(list []uint32) {
for i := range list {
list[i] = rand.Uint32()
}
}

func randListUint64(list []uint64) {
for i := range list {
list[i] = rand.Uint64()
}
}

func randListBool(list []bool) {
for i := range list {
list[i] = rand.Intn(2) == 1
}
}

func randListFloat32(list []float32) {
for i := range list {
list[i] = rand.Float32()
}
}

func randListFloat64(list []float64) {
for i := range list {
list[i] = rand.Float64()
}
}

func randListString(list []string) {
for i := range list {
list[i] = string(make([]byte, rand.Intn(MockLen)))
}
}

func randListBytes(list [][]byte) {
for i := range list {
list[i] = make([]byte, rand.Intn(MockLen))
}
}

func randMapInt32Int64(m map[int32]int64) {
for i := 0; i < MockCap; i++ {
m[rand.Int31()] = rand.Int63()
}
}

func randMapUint32Uint64(m map[uint32]uint64) {
for i := 0; i < MockCap; i++ {
m[rand.Uint32()] = rand.Uint64()
}
}

func randMapStringBytes(m map[string][]byte) {
for i := 0; i < MockCap; i++ {
m[string(make([]byte, rand.Intn(MockLen)))] = make([]byte, rand.Intn(MockLen))
}
}
100 changes: 100 additions & 0 deletions benchmark/benchmark_fastpb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package benchmark

import (
fastpb_gen "github.com/cloudwego/fastpb/benchmark/fastpb_gen"
)

var (
_fastpbNumber *fastpb_gen.Number
_fastpbString *fastpb_gen.String
_fastpbList *fastpb_gen.List
_fastpbMap *fastpb_gen.Map
)

func fastpbNumber() *fastpb_gen.Number {
if _fastpbNumber != nil {
return _fastpbNumber
}
_fastpbNumber = &fastpb_gen.Number{
Field1: _goNumber.Field1,
Field2: _goNumber.Field2,
Field3: _goNumber.Field3,
Field4: _goNumber.Field4,
Field5: _goNumber.Field5,
Field6: _goNumber.Field6,
Field7: _goNumber.Field7,
Field8: _goNumber.Field8,
Field9: _goNumber.Field9,
Field10: _goNumber.Field10,
Field11: _goNumber.Field11,
Field12: _goNumber.Field12,
Field13: _goNumber.Field13,
}
return _fastpbNumber
}

func fastpbString() *fastpb_gen.String {
if _fastpbString != nil {
return _fastpbString
}
_fastpbString = &fastpb_gen.String{
Field1: _goString.Field1,
Field2: _goString.Field2,
}
return _fastpbString
}

func fastpbList() *fastpb_gen.List {
if _fastpbList != nil {
return _fastpbList
}
_fastpbList = &fastpb_gen.List{
Field1: _goList.Field1,
Field2: _goList.Field2,
Field3: _goList.Field3,
Field4: _goList.Field4,
Field5: _goList.Field5,
Field6: _goList.Field6,
Field7: _goList.Field7,
Field8: _goList.Field8,
Field9: _goList.Field9,
Field10: _goList.Field10,
Field11: _goList.Field11,
Field12: _goList.Field12,
Field13: _goList.Field13,
Field14: _goList.Field14,
Field15: _goList.Field15,
}
return _fastpbList
}

func fastpbMap() *fastpb_gen.Map {
if _fastpbMap != nil {
return _fastpbMap
}
_fastpbMap = &fastpb_gen.Map{
Field1: _goMap.Field1,
Field2: _goMap.Field2,
Field3: _goMap.Field3,
Field4: _goMap.Field4,
Field5: _goMap.Field5,
Field6: _goMap.Field6,
}
return _fastpbMap
}
113 changes: 113 additions & 0 deletions benchmark/benchmark_fastpb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package benchmark

import (
"testing"

"github.com/cloudwego/fastpb"

fastpb_gen "github.com/cloudwego/fastpb/benchmark/fastpb_gen"
)

func Benchmark_marshal_number_fastpb(b *testing.B) {
encode := fastpbNumber()
buf := make([]byte, encode.Size())

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
encode.FastWrite(buf)
}
}

func Benchmark_unmarshal_number_fastpb(b *testing.B) {
buf := bytesNumber()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var decode fastpb_gen.Number
_, _ = fastpb.ReadMessage(buf, int8(fastpb.SkipTypeCheck), &decode)
}
}

func Benchmark_marshal_string_fastpb(b *testing.B) {
encode := fastpbString()
buf := make([]byte, encode.Size())

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
encode.FastWrite(buf)
}
}

func Benchmark_unmarshal_string_fastpb(b *testing.B) {
buf := bytesString()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var decode fastpb_gen.String
_, _ = fastpb.ReadMessage(buf, int8(fastpb.SkipTypeCheck), &decode)
}
}

func Benchmark_marshal_list_fastpb(b *testing.B) {
encode := fastpbList()
buf := make([]byte, encode.Size())

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
encode.FastWrite(buf)
}
}

func Benchmark_unmarshal_list_fastpb(b *testing.B) {
buf := bytesList()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var decode fastpb_gen.List
_, _ = fastpb.ReadMessage(buf, int8(fastpb.SkipTypeCheck), &decode)
}
}

func Benchmark_marshal_map_fastpb(b *testing.B) {
encode := fastpbMap()
buf := make([]byte, encode.Size())

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
encode.FastWrite(buf)
}
}

func Benchmark_unmarshal_map_fastpb(b *testing.B) {
buf := bytesMap()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var decode fastpb_gen.Map
_, _ = fastpb.ReadMessage(buf, int8(fastpb.SkipTypeCheck), &decode)
}
}
Loading

0 comments on commit f6138a7

Please sign in to comment.