name: Benchmark Main

on:
  push:
    branches: [ main ]
  workflow_dispatch:

permissions:
  contents: read

jobs:
  sdk-benchmark:
    permissions:
      contents: write # for git push to benchmarks branch
    name: Benchmark SDK
    runs-on: self-hosted
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - id: setup-java
        name: Set up Java for build
        uses: actions/setup-java@3a4f6e1af504cf6a31855fa899c6aa5355ba6c12 # v4.7.0
        with:
          distribution: temurin
          java-version: 17

      - name: Set up gradle
        uses: gradle/actions/setup-gradle@94baf225fe0a508e581a564467443d0e2379123b # v4.3.0
      - name: Run jmh
        run: ./gradlew jmhJar

      - name: Run Benchmark
        run: |
          cd sdk/trace/build
          java -jar libs/opentelemetry-sdk-trace-*-jmh.jar -rf json SpanBenchmark SpanPipelineBenchmark ExporterBenchmark

      - name: Store benchmark results
        uses: benchmark-action/github-action-benchmark@d48d326b4ca9ba73ca0cd0d59f108f9e02a381c7 # v1.20.4
        with:
          tool: 'jmh'
          output-file-path: sdk/trace/build/jmh-result.json
          gh-pages-branch: benchmarks
          github-token: ${{ secrets.GITHUB_TOKEN }}
          benchmark-data-dir-path: "benchmarks"
          auto-push: true
