github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/.github/workflows/bechmark.yml (about)

     1  name: Benchmark
     2  
     3  on:
     4    pull_request:
     5  
     6  permissions:
     7    contents: read
     8    pull-requests: write
     9  
    10  concurrency:
    11    group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
    12    cancel-in-progress: true
    13  
    14  jobs:
    15    packages:
    16      name: Changed packages
    17      runs-on: ubuntu-latest
    18      outputs:
    19        matrix: ${{ steps.set-matrix.outputs.matrix }}
    20      steps:
    21        - name: Find packages with changes
    22          id: packages
    23          uses: tj-actions/changed-files@v44
    24          with:
    25            files: '**/*.go'
    26            dir_names: true
    27            json: true
    28  
    29        - name: Set output
    30          id: set-matrix
    31          run: |
    32            echo "matrix={\"package\":${{ steps.packages.outputs.all_changed_and_modified_files }}}" >> $GITHUB_OUTPUT
    33  
    34    benchmark:
    35      name: Benchmark ${{ matrix.package }}
    36      needs: packages
    37      runs-on: ubuntu-latest
    38      strategy:
    39        matrix: ${{ fromJSON(needs.packages.outputs.matrix) }}
    40      steps:
    41        - name: Checkout repository
    42          uses: actions/checkout@v4
    43  
    44        - name: Setup Go
    45          uses: actions/setup-go@v5
    46          with:
    47            go-version-file: go.mod
    48  
    49        - name: Run benchmark
    50          run: go test -bench=. -count=10 -benchmem ./${{ matrix.package }} | tee after
    51  
    52        - name: Run benchmark for base code
    53          run: |
    54            git fetch --quiet origin master ${{ github.event.pull_request.base.sha }}
    55            git reset --quiet --hard ${{ github.event.pull_request.base.sha }}
    56            go test -bench=. -count=10 -benchmem ./${{ matrix.package }} | tee before
    57  
    58        - name: Compare benchmarks
    59          id: bench
    60          run: |
    61            GO111MODULE=off go get golang.org/x/perf/cmd/benchstat
    62            OUTPUT=$(benchstat before after)
    63            echo "${OUTPUT}"
    64            echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
    65  
    66        - name: Save benchmark results
    67          uses: cloudposse/github-action-matrix-outputs-write@1.0.0
    68          if: steps.bench.outputs.diff != ''
    69          with:
    70            matrix-step-name: ${{ github.job }}
    71            matrix-key: ${{ matrix.package }}
    72            outputs: ${{ toJSON(steps.bench.outputs) }}
    73  
    74    comment:
    75      name: Comment
    76      needs: benchmark
    77      runs-on: ubuntu-latest
    78      steps:
    79        - name: Load benchmark results
    80          uses: cloudposse/github-action-matrix-outputs-read@1.0.0
    81          id: read
    82          with:
    83            matrix-step-name: benchmark
    84  
    85        - name: Generate comment text
    86          uses: actions/github-script@v7
    87          if: steps.read.outputs.result != '{}'
    88          id: parse
    89          with:
    90            result-encoding: string
    91            script: |
    92              const result = ${{ steps.read.outputs.result }}
    93              return Object.keys(result.diff).sort().map((key) => `
    94              <details><summary><code>${key}</code></summary>
    95  
    96              ` + "```" + `
    97              ${result.diff[key]}
    98              ` + "```" + `
    99              </details>
   100              `).join('')
   101  
   102        - name: Create comment
   103          if: steps.parse.outputs.result != ''
   104          uses: marocchino/sticky-pull-request-comment@v2
   105          with:
   106            GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
   107            header: benchmarks
   108            message: |
   109              ### Benchmark Results
   110              ${{ steps.parse.outputs.result }}