Skip to content
Latchkey

benchmarks workflow (terramate-io/terramate)

The benchmarks workflow from terramate-io/terramate, explained and optimized by Latchkey.

C

CI health: C - fair

Run this on Latchkey for self-healing, caching, and up to 58% lower cost.

Grade your own workflow free or run it on Latchkey →
Source: terramate-io/terramate.github/workflows/benchmark.ymlLicense MPL-2.0View source

What it does

This is the benchmarks workflow from the terramate-io/terramate repository, a real project running GitHub Actions. It is shown here with attribution under its MPL-2.0 license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
# Copyright 2023 Terramate GmbH
# SPDX-License-Identifier: MPL-2.0

name: benchmarks
on:
  pull_request:
    paths:
      - go.mod
      - go.sum
      - "**.go"
      - "!cloud/testserver/**"
      - .github/workflows/benchmark.yml

permissions:
  pull-requests: write

jobs:
  discover:
    runs-on: blacksmith-4vcpu-ubuntu-2404
    outputs:
      packages: ${{ steps.find-packages.outputs.packages }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # pin@v4
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}
          fetch-depth: 0

      - name: Find benchmark packages
        id: find-packages
        run: |
          # Find all packages containing benchmark functions (not just files named *_bench_test.go)
          packages=$( (grep -r --include='*_test.go' -l '^func Benchmark' . || true) | xargs -r dirname | sed 's|^\./||' | sort -u | jq -R -s -c 'split("\n") | map(select(length > 0))' )
          echo "packages=${packages}" >> $GITHUB_OUTPUT
          echo "Found packages: ${packages}"

  benchmarks:
    needs: discover
    runs-on: blacksmith-4vcpu-ubuntu-2404

    strategy:
      fail-fast: false
      matrix:
        package: ${{ fromJson(needs.discover.outputs.packages) }}

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # pin@v4
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}
          fetch-depth: 0

      - uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # pin@v5
        with:
          go-version-file: "go.mod"
          check-latest: true

      - name: Set artifact name
        id: artifact
        run: |
          # Replace / with - for artifact name compatibility
          artifact_name=$(echo "${{ matrix.package }}" | tr '/' '-')
          echo "name=benchmark-${artifact_name}" >> $GITHUB_OUTPUT

      - name: run benchcheck
        id: benchmark
        run: |
          result=$(make bench/check 'pkg=./${{ matrix.package }}' 'parallel=1' 'new=${{ github.event.pull_request.head.sha }}' 'old=${{ github.event.pull_request.base.sha }}')
          echo "$result"
          echo "## Package: ${{ matrix.package }}" > benchmark-result.txt
          echo "$result" >> benchmark-result.txt

      - name: Upload benchmark result
        uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # pin@v4
        with:
          name: ${{ steps.artifact.outputs.name }}
          path: benchmark-result.txt
          retention-days: 1

  report:
    needs: [discover, benchmarks]
    runs-on: blacksmith-4vcpu-ubuntu-2404
    if: always() && needs.benchmarks.result != 'skipped'

    steps:
      - name: Download all benchmark results
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # pin@v4
        continue-on-error: true
        with:
          path: benchmark-results
          pattern: benchmark-*

      - name: Combine results
        id: combine
        run: |
          echo "result<<EOF" >> $GITHUB_OUTPUT
          # Sort and combine all benchmark result files
          find benchmark-results -name 'benchmark-result.txt' -type f | sort | while read -r result_file; do
            cat "$result_file" >> $GITHUB_OUTPUT
            echo "" >> $GITHUB_OUTPUT
            echo "---" >> $GITHUB_OUTPUT
            echo "" >> $GITHUB_OUTPUT
          done
          echo "EOF" >> $GITHUB_OUTPUT

      - uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # pin@v2
        with:
          header: benchmark
          message: |
            ```
            ${{ steps.combine.outputs.result }}
            ```

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

# Copyright 2023 Terramate GmbH
# SPDX-License-Identifier: MPL-2.0
 
name: benchmarks
on:
  pull_request:
    paths:
      - go.mod
      - go.sum
      - "**.go"
      - "!cloud/testserver/**"
      - .github/workflows/benchmark.yml
 
permissions:
  pull-requests: write
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  discover:
    timeout-minutes: 30
    runs-on: blacksmith-4vcpu-ubuntu-2404
    outputs:
      packages: ${{ steps.find-packages.outputs.packages }}
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # pin@v4
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}
          fetch-depth: 0
 
      - name: Find benchmark packages
        id: find-packages
        run: |
          # Find all packages containing benchmark functions (not just files named *_bench_test.go)
          packages=$( (grep -r --include='*_test.go' -l '^func Benchmark' . || true) | xargs -r dirname | sed 's|^\./||' | sort -u | jq -R -s -c 'split("\n") | map(select(length > 0))' )
          echo "packages=${packages}" >> $GITHUB_OUTPUT
          echo "Found packages: ${packages}"
 
  benchmarks:
    timeout-minutes: 30
    needs: discover
    runs-on: blacksmith-4vcpu-ubuntu-2404
 
    strategy:
      fail-fast: false
      matrix:
        package: ${{ fromJson(needs.discover.outputs.packages) }}
 
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # pin@v4
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}
          fetch-depth: 0
 
      - uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # pin@v5
        with:
          go-version-file: "go.mod"
          check-latest: true
 
      - name: Set artifact name
        id: artifact
        run: |
          # Replace / with - for artifact name compatibility
          artifact_name=$(echo "${{ matrix.package }}" | tr '/' '-')
          echo "name=benchmark-${artifact_name}" >> $GITHUB_OUTPUT
 
      - name: run benchcheck
        id: benchmark
        run: |
          result=$(make bench/check 'pkg=./${{ matrix.package }}' 'parallel=1' 'new=${{ github.event.pull_request.head.sha }}' 'old=${{ github.event.pull_request.base.sha }}')
          echo "$result"
          echo "## Package: ${{ matrix.package }}" > benchmark-result.txt
          echo "$result" >> benchmark-result.txt
 
      - name: Upload benchmark result
        uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # pin@v4
        with:
          name: ${{ steps.artifact.outputs.name }}
          path: benchmark-result.txt
          retention-days: 1
 
  report:
    timeout-minutes: 30
    needs: [discover, benchmarks]
    runs-on: blacksmith-4vcpu-ubuntu-2404
    if: always() && needs.benchmarks.result != 'skipped'
 
    steps:
      - name: Download all benchmark results
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # pin@v4
        continue-on-error: true
        with:
          path: benchmark-results
          pattern: benchmark-*
 
      - name: Combine results
        id: combine
        run: |
          echo "result<<EOF" >> $GITHUB_OUTPUT
          # Sort and combine all benchmark result files
          find benchmark-results -name 'benchmark-result.txt' -type f | sort | while read -r result_file; do
            cat "$result_file" >> $GITHUB_OUTPUT
            echo "" >> $GITHUB_OUTPUT
            echo "---" >> $GITHUB_OUTPUT
            echo "" >> $GITHUB_OUTPUT
          done
          echo "EOF" >> $GITHUB_OUTPUT
 
      - uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # pin@v2
        with:
          header: benchmark
          message: |
            ```
            ${{ steps.combine.outputs.result }}
            ```
 

What changed

This workflow runs 3 jobs per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow