Skip to content

Commit

Permalink
implement benchmarks in CI
Browse files Browse the repository at this point in the history
Masorubka1 authored and geseq committed Oct 16, 2024
1 parent cd247a1 commit a0ac20d
Showing 6 changed files with 48 additions and 95 deletions.
118 changes: 43 additions & 75 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -6,98 +6,66 @@ on:
pull_request:
branches: [main]

permissions:
contents: read
pull-requests: write
statuses: write

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
- uses: actions/setup-go@v4
with:
go-version: 1.19
go-version: "stable"

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...

cache-main-benchmark:
runs-on: ubuntu-latest
needs: build-and-test

steps:
- uses: actions/checkout@v3

- name: Cache Main Branch Benchmarks
id: cache-benchmark
uses: actions/cache@v3
with:
path: main_benchmarks.txt
key: main-benchmarks-${{ runner.os }}-${{ github.sha }}
restore-keys: |
main-benchmarks-${{ runner.os }}-
- name: Run Benchmarks on Main Branch (if no cache)
if: steps.cache-benchmark.outputs.cache-hit != 'true'
run: |
git fetch origin main
git checkout origin/main
go test -bench . -benchmem -run=^$ ./... > main_benchmarks.txt
continue-on-error: true

- name: Save Main Benchmark Results
uses: actions/upload-artifact@v3
with:
name: main_benchmarks
path: main_benchmarks.txt

run-pr-benchmark:
runs-on: ubuntu-latest
needs: cache-main-benchmark

steps:
- uses: actions/checkout@v3

- name: Run Benchmarks on PR Branch
- name: Run Tests and Generate Test Report
run: |
go test -bench . -benchmem -run=^$ ./... > pr_benchmarks.txt
continue-on-error: true
mkdir -p reports
go test -v -json ./... > reports/report.json
- name: Save PR Benchmark Results
uses: actions/upload-artifact@v3
- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
with:
name: pr_benchmarks
path: pr_benchmarks.txt

compare-benchmarks:
files: 'reports/report.json'
check_name: 'Go Tests'
comment_title: 'Go Test Results'
commit: ${{ github.event.pull_request.head.sha }}
github_token: ${{ secrets.GITHUB_TOKEN }}
hide_comments: 'all but latest'
deduplicate_classes_by_file_name: true

benchmark:
name: Run Go benchmarks
runs-on: ubuntu-latest
needs: run-pr-benchmark

permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v3

- name: Download Main and PR Benchmark Results
uses: actions/download-artifact@v3
with:
name: main_benchmarks
path: main_benchmarks.txt
- uses: actions/checkout@v4

- uses: actions/download-artifact@v3
- uses: actions/setup-go@v4
with:
name: pr_benchmarks
path: pr_benchmarks.txt
go-version: '1.x'

- name: Compare Benchmarks
- name: Run benchmarks
run: |
echo "Comparing benchmarks..."
diff main_benchmarks.txt pr_benchmarks.txt || true
mkdir -p benchmarks
go test -bench . -benchmem -run=^$ ./... > benchmarks/benchmark.txt
- name: Display Benchmark Results
run: |
echo "Benchmark results for PR branch:"
cat pr_benchmarks.txt
echo ""
echo "Benchmark results for Main branch:"
cat main_benchmarks.txt
- name: Post Benchmark Results
uses: rhysd/github-action-benchmark@v1
with:
tool: 'go'
output-file-path: 'benchmarks/benchmark.txt'
github-token: ${{ secrets.GITHUB_TOKEN }}
ref: 'main'
comment-on-alert: true
alert-threshold: '15%'
fail-on-alert: true
summary-always: true
9 changes: 4 additions & 5 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package orderbook

import (
"fmt"
"math/rand"
"runtime"
"strings"
@@ -31,14 +30,13 @@ func BenchmarkLatency(b *testing.B) {
printResultsWithPercentiles(b, "Cancel Order", totalCancelHist)
}

// Функция для бенчмарка Latency, который выполняется в течение указанного времени (duration)
func runBenchmarkLatency(b *testing.B) ([]float64, []float64) {
seed := time.Now().UnixNano()
duration := 30 * time.Second
lowerBound := decimal.MustParse("50.0")
upperBound := decimal.MustParse("100.0")
minSpread := decimal.MustParse("0.25")
sched := true
sched := false

ob := getOrderBook()
bid, ask, bidQty, askQty := getInitialVars(lowerBound, upperBound, minSpread)
@@ -193,11 +191,12 @@ func runBenchmarkThroughput(b *testing.B) []float64 {
func printResultsWithPercentiles(b *testing.B, operationName string, data []float64) {
percentiles := []float64{50, 75, 90, 95, 99, 99.9}

b.Logf("Operation: %s", operationName)
for _, p := range percentiles {
value := calculatePercentile(data, p)
metricName := fmt.Sprintf("%s_%v_percentile_ms", strings.ReplaceAll(operationName, " ", "_"), p)
b.ReportMetric(value, metricName)
b.Logf("%v: %f ms", p, value)
}
b.ReportMetric(calculatePercentile(data, percentiles[3]), strings.ReplaceAll(operationName, " ", "_")+"_95_ns/po")
}

func calculatePercentile(data []float64, percentile float64) float64 {
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/geseq/orderbook

go 1.22.0

toolchain go1.23.0
go 1.23

require (
fortio.org/fortio v1.67.1
5 changes: 0 additions & 5 deletions pkg/pool/pool_generic.go
Original file line number Diff line number Diff line change
@@ -7,11 +7,6 @@ import (
"golang.org/x/sys/cpu"
)

// var oPool = newOrderPool(1)
// var ntPool = newNodeTreePool(1)
// var notPool = newOrderTreeNodePool(1)
// var oqPool = newOrderQueuePool(1)

// ItemPoolV2 is a generic item pool with the V2 suffix
type ItemPoolV2[T any] struct {
ch *ItemChanV2[T]
5 changes: 0 additions & 5 deletions pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
package orderbook

//go:generate gotemplate "github.com/geseq/orderbook/pool" orderPool(Order)
//go:generate gotemplate "github.com/geseq/orderbook/pool" orderQueuePool(orderQueue)
//go:generate gotemplate "github.com/geseq/orderbook/pool" nodeTreePool(nodeTree)
//go:generate gotemplate "github.com/geseq/orderbook/pool" orderTreeNodePool(nodeOrderTree)
2 changes: 0 additions & 2 deletions pricelevel.go
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@ import (
decimal "github.com/geseq/udecimal"
)

//go:generate gotemplate "github.com/geseq/redblacktree" tree(udecimal.Decimal,*orderQueue)

// priceLevel implements facade to operations with order queue
type priceLevel struct {
priceTree *local_tree.Tree[decimal.Decimal, *orderQueue]

0 comments on commit a0ac20d

Please sign in to comment.