Skip to content

Commit

Permalink
Added benchmark testings
Browse files Browse the repository at this point in the history
Signed-off-by: boot-go <[email protected]>
  • Loading branch information
boot-go committed Sep 4, 2023
1 parent a68f368 commit 73b5ebb
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 16 deletions.
29 changes: 14 additions & 15 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ jobs:
uses: golangci/golangci-lint-action@v3

- name: Test
run: go test -cpu 2 -timeout 2m -race -v ./...
run: |
n=0
until go test -cpu 2 -timeout 2m -race -v ./...; do
if [ $n -ge 3 ]; then
exit 1
fi
echo Test coverage failed, retrying in 3 seconds...
n=$(($n+1))
sleep 3
done
- name: Perform benchmark testing
run: go test -bench=Benchmark . -run Benchmark

- name: Test coverage
run: |
Expand All @@ -45,7 +57,7 @@ jobs:
auth: ${{ secrets.GIST_SECRET }}
gistID: c77b22000b3e249510dfb4542847c708
filename: test_coverage.json
label: "test coverage"
label: coverage
message: ${{ env.COVERAGE }}
valColorRange: ${{ env.COVERAGE }}
maxColorRange: 100
Expand All @@ -54,19 +66,6 @@ jobs:
- name: Tool cover to html
run: go tool cover -html=coverage.out -o=cover.html

# - name: Obtain html coverage
# run: echo "HTML_COVERAGE='$(cat cover.html)'" >> $GITHUB_ENV

# - name: Upload html coverage
# uses: gorgbus/gist-actions@main
# env:
# GITHUB_TOKEN: ${{ secrets.GIST_SECRET }}
# with:
# action: "update"
# gist_id: "c77b22000b3e249510dfb4542847c708"
# file_name: "cover.html"
# content: ${{ }}

- name: Upload html coverage
uses: exuanbo/actions-deploy-gist@v1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
*.iml
*.prof
119 changes: 119 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package boot

import (
"fmt"
"testing"
"time"
)

/*
* Copyright (c) 2021-2023 boot-go
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

const (
events = 100000

receivers = 1000

referenceTime = 45
)

type IncrementEvent struct {
value int
}

type testReceiverComponent struct {
EventBus EventBus `boot:"wire"`
id int
count int
}

func (t *testReceiverComponent) Init() error {
t.count = 0
err := t.EventBus.Subscribe(func(e IncrementEvent) {
t.count = e.value
})
if err != nil {
return err
}
return nil
}

type testSenderComponent struct {
EventBus EventBus `boot:"wire"`
}

func (t *testSenderComponent) Init() error {
return nil
}

func (t *testSenderComponent) Start() error {
for i := 0; i < events; i++ {
err := t.EventBus.Publish(IncrementEvent{
value: i,
})
if err != nil {
return err
}
}
return nil
}

func (t *testSenderComponent) Stop() error {
return nil
}

func BenchmarkEventBus_Publish(b *testing.B) {
startTime := time.Now()
s := NewSession(UnitTestFlag)
for i := 0; i < receivers; i++ {
err := s.RegisterName(fmt.Sprintf("receiver-%d", i), func() Component {
return &testReceiverComponent{
id: i,
}
})
if err != nil {
b.Fatal(err)
}
}
err := s.Register(func() Component {
return &testSenderComponent{}
})
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
err = s.Go()
if err != nil {
b.Fatal(err)
}
endTime := time.Now()
duration := endTime.Sub(startTime)
relative := duration.Seconds() / referenceTime * 100
fmt.Printf("benchmark reference : %vs\n", referenceTime)
fmt.Printf("benchmark duration : %v\n", duration)
fmt.Printf("benchmark result : %.1f%%\n", relative)
if duration.Seconds() > referenceTime*1.1 { // 10% tolerance allowed
b.Fatal("benchmark test failed")
}
}
2 changes: 1 addition & 1 deletion eventbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func TestEventbusUnsubscribe(t *testing.T) {
wantErr: true,
},
{
name: "non Existing Subscription",
name: "int subscription",
event: testEventFunction,
eventHandler: func(event int) {},
wantErr: true,
Expand Down

0 comments on commit 73b5ebb

Please sign in to comment.