Skip to content

Commit

Permalink
cli: add 'gobenchdata action' to use gobenchdata action with custom G…
Browse files Browse the repository at this point in the history
…o versions (#62)
  • Loading branch information
bobheadxi authored Jun 11, 2022
1 parent ea0e521 commit 5a2f2cd
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 125 deletions.
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ RUN go build -ldflags "-X main.Version=$(git describe --tags)" -o /bin/gobenchda
RUN rm -rf /tmp/build

# init entrypoint
WORKDIR /tmp/entrypoint
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
ENTRYPOINT ["/bin/gobenchdata", "action"]
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ JSON to `gh-pages` and visualizing it with a generated web app or your own web a

### Setup

The default action, `uses: bobheadxi/gobenchdata@v1`, is distributed as a [Docker container action](https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action).
This means that the Go version benchmarks are run with are tied to the version of Go that ships with the `gobenchdata` action.
For greater flexibility, such as to use custom Go versions, see [custom setup](#custom-setup).

Learn more about GitHub Actions in the [official documentation](https://github.com/features/actions).

#### Docker container action

For example, in `.github/workflows/push.yml`, using [the new YAML syntax for workflows](https://help.github.com/en/articles/workflow-syntax-for-github-actions), a simple benchmark [run and publish workflow](#publishing) would look like:

```yml
Expand All @@ -55,7 +63,36 @@ jobs:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
```
Learn more about GitHub Actions in the [official documentation](https://github.com/features/actions).
#### Custom setup
For greater flexibility and the ability to target custom Go versions, you can either:
- directly use the [`entrypoint.sh`](entrypoint.sh) script, or some variant of it
- run `gobenchdata action`, which replicates the behaviour of the GitHub Action

Using `gobenchdata action` to replicate the [Docker container action](#docker-container-action) would look like:

```yml
name: gobenchdata publish
on: push
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- uses: actions/setup-go@v2
with: { go-version: "1.18" }
- name: gobenchdata publish
run: go run go.bobheadxi.dev/gobenchdata@v1 action
with:
PRUNE_COUNT: 30
GO_TEST_FLAGS: -cpu 1,2
PUBLISH: true
PUBLISH_BRANCH: gh-pages
env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
```

### Configuration

Expand Down
29 changes: 29 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"context"
_ "embed"
"os"
"strings"

"github.com/sourcegraph/run"
)

//go:embed entrypoint.sh
var entrypointScript string

// runEmbeddedAction executes an embedded version of entrypoint.sh
func runEmbeddedAction(ctx context.Context) error {
cmd := run.Cmd(ctx, "bash").
Input(strings.NewReader(entrypointScript)).
StdOut()

if executable, err := os.Executable(); err == nil {
cmd = cmd.Env(map[string]string{
// point to self
"GOBENCHDATA": executable,
})
}

return cmd.Run().Stream(os.Stdout)
}
118 changes: 62 additions & 56 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
set -e

# core configuration
INPUT_SUBDIRECTORY="${INPUT_SUBDIRECTORY:-"."}"
INPUT_PRUNE_COUNT="${INPUT_PRUNE_COUNT:-"0"}"
INPUT_BENCHMARKS_OUT="${INPUT_BENCHMARKS_OUT:-"benchmarks.json"}"
INPUT_GO_TEST_PKGS="${INPUT_GO_TEST_PKGS:-"./..."}"
INPUT_GO_BENCHMARKS="${INPUT_GO_BENCHMARKS:-"."}"
INPUT_GIT_COMMIT_MESSAGE="${INPUT_GIT_COMMIT_MESSAGE:-"add benchmark run for ${GITHUB_SHA}"}"
export GOBENCHDATA="${GOBENCHDATA:-"gobenchdata"}"
export INPUT_SUBDIRECTORY="${INPUT_SUBDIRECTORY:-"."}"
export INPUT_PRUNE_COUNT="${INPUT_PRUNE_COUNT:-"0"}"
export INPUT_BENCHMARKS_OUT="${INPUT_BENCHMARKS_OUT:-"benchmarks.json"}"
export INPUT_GO_TEST_PKGS="${INPUT_GO_TEST_PKGS:-"./..."}"
export INPUT_GO_BENCHMARKS="${INPUT_GO_BENCHMARKS:-"."}"
export INPUT_GIT_COMMIT_MESSAGE="${INPUT_GIT_COMMIT_MESSAGE:-"add benchmark run for ${GITHUB_SHA}"}"

# publishing configuration
INPUT_PUBLISH_REPO="${INPUT_PUBLISH_REPO:-${GITHUB_REPOSITORY}}"
INPUT_PUBLISH_BRANCH="${INPUT_PUBLISH_BRANCH:-"gh-pages"}"
export INPUT_PUBLISH_REPO="${INPUT_PUBLISH_REPO:-${GITHUB_REPOSITORY}}"
export INPUT_PUBLISH_BRANCH="${INPUT_PUBLISH_BRANCH:-"gh-pages"}"

# pull request checks
INPUT_CHECKS="${INPUT_CHECKS:-"false"}"
INPUT_CHECKS_CONFIG="${INPUT_CHECKS_CONFIG:-"gobenchdata-checks.yml"}"
export INPUT_CHECKS="${INPUT_CHECKS:-"false"}"
export INPUT_CHECKS_CONFIG="${INPUT_CHECKS_CONFIG:-"gobenchdata-checks.yml"}"

# output build data
echo '========================'
command -v gobenchdata
gobenchdata version
command -v ${GOBENCHDATA}
${GOBENCHDATA} version
echo "👨‍⚕️ Checking configuration..."
env | grep 'INPUT_'
echo "GITHUB_ACTOR=${GITHUB_ACTOR}"
echo "GITHUB_WORKSPACE=${GITHUB_WORKSPACE}"
Expand All @@ -31,8 +33,10 @@ echo '========================'

# setup
mkdir -p /tmp/{gobenchdata,build}
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config --global user.name "${GITHUB_ACTOR}"
if [[ "${SET_GIT_USER}" != "false" ]]; then
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config user.name "${GITHUB_ACTOR}"
fi

# run benchmarks from configured directory
echo
Expand All @@ -44,58 +48,60 @@ go test \
-bench "${INPUT_GO_BENCHMARKS}" \
-benchmem \
${INPUT_GO_TEST_FLAGS} \
${INPUT_GO_TEST_PKGS} \
| gobenchdata --json "${RUN_OUTPUT}" -v "${GITHUB_SHA}" -t "ref=${GITHUB_REF}"
${INPUT_GO_TEST_PKGS} |
${GOBENCHDATA} --json "${RUN_OUTPUT}" -v "${GITHUB_SHA}" -t "ref=${GITHUB_REF}"
cd "${GITHUB_WORKSPACE}"

# fetch published data
echo
echo "📚 Checking out ${INPUT_PUBLISH_REPO}@${INPUT_PUBLISH_BRANCH}..."
cd /tmp/build
git clone https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${INPUT_PUBLISH_REPO}.git .
git checkout ${INPUT_PUBLISH_BRANCH}
echo

if [[ "${INPUT_CHECKS}" == "true" ]]; then

# check results against published
echo '🔎 Evaluating results against base runs...'
CHECKS_OUTPUT="/tmp/gobenchdata/checks-results.json"
gobenchdata checks eval "${INPUT_BENCHMARKS_OUT}" "${RUN_OUTPUT}" \
--checks.config "${GITHUB_WORKSPACE}/${INPUT_CHECKS_CONFIG}" \
--json ${CHECKS_OUTPUT} \
--flat
RESULTS=$(cat ${CHECKS_OUTPUT})
echo "::set-output name=checks-results::$RESULTS"

# output results
if [[ "${INPUT_PUBLISH}" == "true" || "${INPUT_CHECKS}" == "true" ]]; then
echo
echo "📚 Checking out ${INPUT_PUBLISH_REPO}@${INPUT_PUBLISH_BRANCH}..."
cd /tmp/build
git clone https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${INPUT_PUBLISH_REPO}.git .
git checkout ${INPUT_PUBLISH_BRANCH}
echo
echo '📝 Generating checks report...'
gobenchdata checks report ${CHECKS_OUTPUT}

fi

if [[ "${INPUT_PUBLISH}" == "true" ]]; then
if [[ "${INPUT_CHECKS}" == "true" ]]; then

# merge results with published
echo '☝️ Updating results...'
if [[ -f "${INPUT_BENCHMARKS_OUT}" ]]; then
echo '📈 Existing report found - merging...'
gobenchdata merge "${RUN_OUTPUT}" "${INPUT_BENCHMARKS_OUT}" \
--prune "${INPUT_PRUNE_COUNT}" \
--json "${INPUT_BENCHMARKS_OUT}" \
# check results against published
echo '🔎 Evaluating results against base runs...'
CHECKS_OUTPUT="/tmp/gobenchdata/checks-results.json"
${GOBENCHDATA} checks eval "${INPUT_BENCHMARKS_OUT}" "${RUN_OUTPUT}" \
--checks.config "${GITHUB_WORKSPACE}/${INPUT_CHECKS_CONFIG}" \
--json ${CHECKS_OUTPUT} \
--flat
else
cp "${RUN_OUTPUT}" "${INPUT_BENCHMARKS_OUT}"
RESULTS=$(cat ${CHECKS_OUTPUT})
echo "::set-output name=checks-results::$RESULTS"

# output results
echo
echo '📝 Generating checks report...'
${GOBENCHDATA} checks report ${CHECKS_OUTPUT}

fi

# publish results
echo
echo '📷 Committing and pushing new benchmark data...'
git add .
git commit -m "${INPUT_GIT_COMMIT_MESSAGE}"
git push -f origin ${INPUT_PUBLISH_BRANCH}
if [[ "${INPUT_PUBLISH}" == "true" ]]; then

# merge results with published
echo '☝️ Updating results...'
if [[ -f "${INPUT_BENCHMARKS_OUT}" ]]; then
echo '📈 Existing report found - merging...'
${GOBENCHDATA} merge "${RUN_OUTPUT}" "${INPUT_BENCHMARKS_OUT}" \
--prune "${INPUT_PRUNE_COUNT}" \
--json "${INPUT_BENCHMARKS_OUT}" \
--flat
else
cp "${RUN_OUTPUT}" "${INPUT_BENCHMARKS_OUT}"
fi

# publish results
echo
echo '📷 Committing and pushing new benchmark data...'
git add .
git commit -m "${INPUT_GIT_COMMIT_MESSAGE}"
git push -f origin ${INPUT_PUBLISH_BRANCH}

fi
fi

echo
Expand Down
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ require (
github.com/antonmedv/expr v1.9.0
github.com/google/go-cmp v0.5.8
github.com/olekukonko/tablewriter v0.0.5
github.com/sourcegraph/run v0.9.0
github.com/spf13/pflag v1.0.5
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/net v0.0.0-20220607020251-c690dde0001d
gopkg.in/yaml.v2 v2.4.0
)

require (
bitbucket.org/creachadair/shell v0.0.7 // indirect
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/djherbis/buffer v1.2.0 // indirect
github.com/djherbis/nio/v3 v3.0.1 // indirect
github.com/gizak/termui/v3 v3.1.0 // indirect
github.com/itchyny/gojq v0.12.7 // indirect
github.com/itchyny/timefmt-go v0.1.3 // indirect
github.com/karrick/godirwalk v1.17.0 // indirect
github.com/labstack/echo v3.3.10+incompatible // indirect
github.com/labstack/gommon v0.3.1 // indirect
Expand All @@ -28,11 +34,11 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.11 // indirect
Expand Down
Loading

1 comment on commit 5a2f2cd

@bobheadxi
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently broken, needs #63

Please sign in to comment.