Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ on:
branches:
- main
pull_request:
workflow_dispatch:
inputs:
arch:
description: Runner architecture for benchmarks
required: true
default: all
type: choice
options:
- all
- amd64
- arm64

jobs:
test:
if: ${{ github.event_name != 'workflow_dispatch' }}
runs-on: ubuntu-latest
env:
GOTOOLCHAIN: local
Expand All @@ -29,3 +41,123 @@ jobs:

- name: Run workspace tests
run: ./scripts/test-all.sh

benchmark-amd64:
name: Benchmarks (linux/amd64)
if: ${{ github.event_name == 'workflow_dispatch' && (inputs.arch == 'all' || inputs.arch == 'amd64') }}
runs-on: ubuntu-latest
env:
ARCH: amd64
GOTOOLCHAIN: local
steps:
- name: Check out repository
uses: actions/checkout@v7

- name: Set up latest stable Go
uses: actions/setup-go@v6
with:
go-version: stable
check-latest: true

- name: Run string builder benchmarks
shell: bash
run: |
set -euo pipefail
mkdir -p benchmark-results
{
printf '# stringbuilderlint benchmarks (%s/%s)\n' "$(go env GOOS)" "$(go env GOARCH)"
go version
printf '\n'
cd stringbuilderlint
go test -run '^$' -bench 'Benchmark(Concat|BuilderConcat|Sprintf|BuilderSprintf)' -benchmem -count 5
} | tee "benchmark-results/stringbuilderlint-${ARCH}.txt"

- name: Run lookup table benchmarks
shell: bash
run: |
set -euo pipefail
mkdir -p benchmark-results
{
printf '# lookuptablelint benchmarks (%s/%s)\n' "$(go env GOOS)" "$(go env GOARCH)"
go version
printf '\n'
cd lookuptablelint
go test -run '^$' -bench 'Benchmark(MapLookup|SwitchLookup)' -benchmem -count 5
} | tee "benchmark-results/lookuptablelint-${ARCH}.txt"

- name: Write benchmark summary
shell: bash
run: |
set -euo pipefail
{
printf '## Benchmarks (linux/%s)\n\n' "${ARCH}"
printf '```text\n'
cat benchmark-results/*.txt
printf '```\n'
} >> "${GITHUB_STEP_SUMMARY}"

- name: Upload benchmark results
uses: actions/upload-artifact@v7
with:
name: benchmark-results-amd64
path: benchmark-results/*.txt

benchmark-arm64:
name: Benchmarks (linux/arm64)
if: ${{ github.event_name == 'workflow_dispatch' && (inputs.arch == 'all' || inputs.arch == 'arm64') }}
runs-on: ubuntu-24.04-arm
env:
ARCH: arm64
GOTOOLCHAIN: local
steps:
- name: Check out repository
uses: actions/checkout@v7

- name: Set up latest stable Go
uses: actions/setup-go@v6
with:
go-version: stable
check-latest: true

- name: Run string builder benchmarks
shell: bash
run: |
set -euo pipefail
mkdir -p benchmark-results
{
printf '# stringbuilderlint benchmarks (%s/%s)\n' "$(go env GOOS)" "$(go env GOARCH)"
go version
printf '\n'
cd stringbuilderlint
go test -run '^$' -bench 'Benchmark(Concat|BuilderConcat|Sprintf|BuilderSprintf)' -benchmem -count 5
} | tee "benchmark-results/stringbuilderlint-${ARCH}.txt"

- name: Run lookup table benchmarks
shell: bash
run: |
set -euo pipefail
mkdir -p benchmark-results
{
printf '# lookuptablelint benchmarks (%s/%s)\n' "$(go env GOOS)" "$(go env GOARCH)"
go version
printf '\n'
cd lookuptablelint
go test -run '^$' -bench 'Benchmark(MapLookup|SwitchLookup)' -benchmem -count 5
} | tee "benchmark-results/lookuptablelint-${ARCH}.txt"

- name: Write benchmark summary
shell: bash
run: |
set -euo pipefail
{
printf '## Benchmarks (linux/%s)\n\n' "${ARCH}"
printf '```text\n'
cat benchmark-results/*.txt
printf '```\n'
} >> "${GITHUB_STEP_SUMMARY}"

- name: Upload benchmark results
uses: actions/upload-artifact@v7
with:
name: benchmark-results-arm64
path: benchmark-results/*.txt
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use (
./shutdown
./slicer
./sqlconv
./stringbuilderlint
./structify
./strutils
./tasker
Expand Down
136 changes: 136 additions & 0 deletions lookuptablelint/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package lookuptablelint

import "testing"

var benchmarkBoolSink bool

var lookupInputs = [...]string{
"a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p",
}

var lookupMap2 = map[string]struct{}{
"a": {},
"b": {},
}

var lookupMap4 = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"d": {},
}

var lookupMap8 = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"d": {},
"e": {},
"f": {},
"g": {},
"h": {},
}

var lookupMap16 = map[string]struct{}{
"a": {},
"b": {},
"c": {},
"d": {},
"e": {},
"f": {},
"g": {},
"h": {},
"i": {},
"j": {},
"k": {},
"l": {},
"m": {},
"n": {},
"o": {},
"p": {},
}

func lookupSwitch2(value string) bool {
switch value {
case "a", "b":
return true
default:
return false
}
}

func lookupSwitch4(value string) bool {
switch value {
case "a", "b", "c", "d":
return true
default:
return false
}
}

func lookupSwitch8(value string) bool {
switch value {
case "a", "b", "c", "d", "e", "f", "g", "h":
return true
default:
return false
}
}

func lookupSwitch16(value string) bool {
switch value {
case "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p":
return true
default:
return false
}
}

func BenchmarkMapLookup2(b *testing.B) {
for i := 0; b.Loop(); i++ {
_, benchmarkBoolSink = lookupMap2[lookupInputs[i&15]]
}
}

func BenchmarkSwitchLookup2(b *testing.B) {
for i := 0; b.Loop(); i++ {
benchmarkBoolSink = lookupSwitch2(lookupInputs[i&15])
}
}

func BenchmarkMapLookup4(b *testing.B) {
for i := 0; b.Loop(); i++ {
_, benchmarkBoolSink = lookupMap4[lookupInputs[i&15]]
}
}

func BenchmarkSwitchLookup4(b *testing.B) {
for i := 0; b.Loop(); i++ {
benchmarkBoolSink = lookupSwitch4(lookupInputs[i&15])
}
}

func BenchmarkMapLookup8(b *testing.B) {
for i := 0; b.Loop(); i++ {
_, benchmarkBoolSink = lookupMap8[lookupInputs[i&15]]
}
}

func BenchmarkSwitchLookup8(b *testing.B) {
for i := 0; b.Loop(); i++ {
benchmarkBoolSink = lookupSwitch8(lookupInputs[i&15])
}
}

func BenchmarkMapLookup16(b *testing.B) {
for i := 0; b.Loop(); i++ {
_, benchmarkBoolSink = lookupMap16[lookupInputs[i&15]]
}
}

func BenchmarkSwitchLookup16(b *testing.B) {
for i := 0; b.Loop(); i++ {
benchmarkBoolSink = lookupSwitch16(lookupInputs[i&15])
}
}
Loading
Loading