Path: blob/main/vendor/github.com/pelletier/go-toml/v2/ci.sh
2880 views
#!/usr/bin/env bash123stderr() {4echo "$@" 1>&25}67usage() {8b=$(basename "$0")9echo $b: ERROR: "$@" 1>&21011cat 1>&2 <<EOF1213DESCRIPTION1415$(basename "$0") is the script to run continuous integration commands for16go-toml on unix.1718Requires Go and Git to be available in the PATH. Expects to be ran from the19root of go-toml's Git repository.2021USAGE2223$b COMMAND [OPTIONS...]2425COMMANDS2627benchmark [OPTIONS...] [BRANCH]2829Run benchmarks.3031ARGUMENTS3233BRANCH Optional. Defines which Git branch to use when running34benchmarks.3536OPTIONS3738-d Compare benchmarks of HEAD with BRANCH using benchstats. In39this form the BRANCH argument is required.4041-a Compare benchmarks of HEAD against go-toml v1 and42BurntSushi/toml.4344-html When used with -a, emits the output as HTML, ready to be45embedded in the README.4647coverage [OPTIONS...] [BRANCH]4849Generates code coverage.5051ARGUMENTS5253BRANCH Optional. Defines which Git branch to use when reporting54coverage. Defaults to HEAD.5556OPTIONS5758-d Compare coverage of HEAD with the one of BRANCH. In this form,59the BRANCH argument is required. Exit code is non-zero when60coverage percentage decreased.61EOF62exit 163}6465cover() {66branch="${1}"67dir="$(mktemp -d)"6869stderr "Executing coverage for ${branch} at ${dir}"7071if [ "${branch}" = "HEAD" ]; then72cp -r . "${dir}/"73else74git worktree add "$dir" "$branch"75fi7677pushd "$dir"78go test -covermode=atomic -coverpkg=./... -coverprofile=coverage.out.tmp ./...79grep -Ev '(fuzz|testsuite|tomltestgen|gotoml-test-decoder|gotoml-test-encoder)' coverage.out.tmp > coverage.out80go tool cover -func=coverage.out81echo "Coverage profile for ${branch}: ${dir}/coverage.out" >&282popd8384if [ "${branch}" != "HEAD" ]; then85git worktree remove --force "$dir"86fi87}8889coverage() {90case "$1" in91-d)92shift93target="${1?Need to provide a target branch argument}"9495output_dir="$(mktemp -d)"96target_out="${output_dir}/target.txt"97head_out="${output_dir}/head.txt"9899cover "${target}" > "${target_out}"100cover "HEAD" > "${head_out}"101102cat "${target_out}"103cat "${head_out}"104105echo ""106107target_pct="$(tail -n2 ${target_out} | head -n1 | sed -E 's/.*total.*\t([0-9.]+)%.*/\1/')"108head_pct="$(tail -n2 ${head_out} | head -n1 | sed -E 's/.*total.*\t([0-9.]+)%/\1/')"109echo "Results: ${target} ${target_pct}% HEAD ${head_pct}%"110111delta_pct=$(echo "$head_pct - $target_pct" | bc -l)112echo "Delta: ${delta_pct}"113114if [[ $delta_pct = \-* ]]; then115echo "Regression!";116117target_diff="${output_dir}/target.diff.txt"118head_diff="${output_dir}/head.diff.txt"119cat "${target_out}" | grep -E '^github.com/pelletier/go-toml' | tr -s "\t " | cut -f 2,3 | sort > "${target_diff}"120cat "${head_out}" | grep -E '^github.com/pelletier/go-toml' | tr -s "\t " | cut -f 2,3 | sort > "${head_diff}"121122diff --side-by-side --suppress-common-lines "${target_diff}" "${head_diff}"123return 1124fi125return 0126;;127esac128129cover "${1-HEAD}"130}131132bench() {133branch="${1}"134out="${2}"135replace="${3}"136dir="$(mktemp -d)"137138stderr "Executing benchmark for ${branch} at ${dir}"139140if [ "${branch}" = "HEAD" ]; then141cp -r . "${dir}/"142else143git worktree add "$dir" "$branch"144fi145146pushd "$dir"147148if [ "${replace}" != "" ]; then149find ./benchmark/ -iname '*.go' -exec sed -i -E "s|github.com/pelletier/go-toml/v2|${replace}|g" {} \;150go get "${replace}"151fi152153export GOMAXPROCS=2154go test '-bench=^Benchmark(Un)?[mM]arshal' -count=10 -run=Nothing ./... | tee "${out}"155popd156157if [ "${branch}" != "HEAD" ]; then158git worktree remove --force "$dir"159fi160}161162fmktemp() {163if mktemp --version &> /dev/null; then164# GNU165mktemp --suffix=-$1166else167# BSD168mktemp -t $1169fi170}171172benchstathtml() {173python3 - $1 <<'EOF'174import sys175176lines = []177stop = False178179with open(sys.argv[1]) as f:180for line in f.readlines():181line = line.strip()182if line == "":183stop = True184if not stop:185lines.append(line.split(','))186187results = []188for line in reversed(lines[2:]):189if len(line) < 8 or line[0] == "":190continue191v2 = float(line[1])192results.append([193line[0].replace("-32", ""),194"%.1fx" % (float(line[3])/v2), # v1195"%.1fx" % (float(line[7])/v2), # bs196])197# move geomean to the end198results.append(results[0])199del results[0]200201202def printtable(data):203print("""204<table>205<thead>206<tr><th>Benchmark</th><th>go-toml v1</th><th>BurntSushi/toml</th></tr>207</thead>208<tbody>""")209210for r in data:211print(" <tr><td>{}</td><td>{}</td><td>{}</td></tr>".format(*r))212213print(""" </tbody>214</table>""")215216217def match(x):218return "ReferenceFile" in x[0] or "HugoFrontMatter" in x[0]219220above = [x for x in results if match(x)]221below = [x for x in results if not match(x)]222223printtable(above)224print("<details><summary>See more</summary>")225print("""<p>The table above has the results of the most common use-cases. The table below226contains the results of all benchmarks, including unrealistic ones. It is227provided for completeness.</p>""")228printtable(below)229print('<p>This table can be generated with <code>./ci.sh benchmark -a -html</code>.</p>')230print("</details>")231232EOF233}234235benchmark() {236case "$1" in237-d)238shift239target="${1?Need to provide a target branch argument}"240241old=`fmktemp ${target}`242bench "${target}" "${old}"243244new=`fmktemp HEAD`245bench HEAD "${new}"246247benchstat "${old}" "${new}"248return 0249;;250-a)251shift252253v2stats=`fmktemp go-toml-v2`254bench HEAD "${v2stats}" "github.com/pelletier/go-toml/v2"255v1stats=`fmktemp go-toml-v1`256bench HEAD "${v1stats}" "github.com/pelletier/go-toml"257bsstats=`fmktemp bs-toml`258bench HEAD "${bsstats}" "github.com/BurntSushi/toml"259260cp "${v2stats}" go-toml-v2.txt261cp "${v1stats}" go-toml-v1.txt262cp "${bsstats}" bs-toml.txt263264if [ "$1" = "-html" ]; then265tmpcsv=`fmktemp csv`266benchstat -format csv go-toml-v2.txt go-toml-v1.txt bs-toml.txt > $tmpcsv267benchstathtml $tmpcsv268else269benchstat go-toml-v2.txt go-toml-v1.txt bs-toml.txt270fi271272rm -f go-toml-v2.txt go-toml-v1.txt bs-toml.txt273return $?274esac275276bench "${1-HEAD}" `mktemp`277}278279case "$1" in280coverage) shift; coverage $@;;281benchmark) shift; benchmark $@;;282*) usage "bad argument $1";;283esac284285286