name: Benchmark

on: pull_request

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, ubuntu-24.04-arm]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Clear repository
        run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE

      - uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: 1.22

      - uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Benchmark Target
        continue-on-error: true
        run: |
          export SONIC_NO_ASYNC_GC=1
          go test -run ^$ -count=20 -benchmem -bench 'BenchmarkDecoder_(Generic|Binding)_Sonic' ./decoder >> /var/tmp/sonic_bench_target_${{ github.run_id }}.out
          go test -run ^$ -count=20 -benchmem -bench 'BenchmarkEncoder_(Generic|Binding)_Sonic' ./encoder >> /var/tmp/sonic_bench_target_${{ github.run_id }}.out
          go test -run ^$ -count=20 -benchmem -bench 'Benchmark(Get|Set)One_Sonic|BenchmarkParseSeven_Sonic' ./ast >> /var/tmp/sonic_bench_target_${{ github.run_id }}.out

      - name: Clear repository
        run: sudo rm -fr $GITHUB_WORKSPACE && mkdir $GITHUB_WORKSPACE

      - name: Checkout main
        uses: actions/checkout@v2
        with:
          ref: main
      
      - name: Benchmark main
        continue-on-error: true
        run: |
          export SONIC_NO_ASYNC_GC=1
          go test -run ^$ -count=20 -benchmem -bench 'BenchmarkDecoder_(Generic|Binding)_Sonic' ./decoder >> /var/tmp/sonic_bench_main_${{ github.run_id }}.out
          go test -run ^$ -count=20 -benchmem -bench 'BenchmarkEncoder_(Generic|Binding)_Sonic' ./encoder >> /var/tmp/sonic_bench_main_${{ github.run_id }}.out
          go test -run ^$ -count=20 -benchmem -bench 'Benchmark(Get|Set)One_Sonic|BenchmarkParseSeven_Sonic' ./ast >> /var/tmp/sonic_bench_main_${{ github.run_id }}.out

      - name: Diff bench
        continue-on-error: true
        run: |
          ./scripts/bench.py -t 0.20 -d /var/tmp/sonic_bench_target_${{ github.run_id }}.out,/var/tmp/sonic_bench_main_${{ github.run_id }}.out x
