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 c982f73
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 15 deletions.
18 changes: 4 additions & 14 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
- name: Test
run: go test -cpu 2 -timeout 2m -race -v ./...

- name: Perform benchmark testing
run: go test -bench=Benchmark . -run Benchmark

- name: Test coverage
run: |
until go test -v -p 1 -covermode=count -coverprofile=coverage.out ./...; do
Expand All @@ -45,7 +48,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 +57,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 = 1000000

receivers = 1000

referenceTime = 90
)

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 c982f73

Please sign in to comment.