Skip to main content

go: create a new folder for Go-related scripts (bench, cover, stress)

ID
1cdc6db
date
2026-11-01 16:30:00+00:00
author
Alex Chan <alexc@tailscale.com>
parent
d46d186
message
go: create a new folder for Go-related scripts (bench, cover, stress)
changed files
6 files, 186 additions, 14 deletions

Changed files

config.fish (6786 → 6821)

diff --git a/config.fish b/config.fish
index e1e8401..cc6ceef 100644
--- a/config.fish
+++ b/config.fish
@@ -52,6 +52,7 @@ prepend_to_path ~/repos/scripts/ci
 prepend_to_path ~/repos/scripts/debug
 prepend_to_path ~/repos/scripts/fs
 prepend_to_path ~/repos/scripts/git
+prepend_to_path ~/repos/scripts/go
 prepend_to_path ~/repos/scripts/images
 prepend_to_path ~/repos/scripts/installers
 prepend_to_path ~/repos/scripts/macos

go/README.md (0 → 1489)

diff --git a/go/README.md b/go/README.md
new file mode 100644
index 0000000..ea4ed5f
--- /dev/null
+++ b/go/README.md
@@ -0,0 +1,65 @@
+# go
+
+These scripts are for working with Go.
+
+## The individual scripts
+
+<!-- [[[cog
+
+# This adds the root of the repo to the PATH, which has cog_helpers.py
+from os.path import abspath, dirname
+import sys
+
+sys.path.append(abspath(dirname(dirname("."))))
+
+import cog_helpers
+
+folder_name = "go"
+
+scripts = [
+    {
+        "usage": "gobench PACKAGE -bench=$BENCHMARK_NAME",
+        "description": "compare benchmark results between a local branch and main",
+    },
+    {
+        "usage": "gocover PACKAGE",
+        "description": "run tests and report test coverage for the package"
+    },
+    {
+        "usage": "gostress PACKAGE TEST_NAME",
+        "description": "run a test with Go’s stress utility",
+    },
+]
+
+cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
+
+]]]-->
+<dl>
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/go/gobench">
+      <code>gobench PACKAGE -bench=$BENCHMARK_NAME</code>
+    </a>
+  </dt>
+  <dd>
+    compare benchmark results between a local branch and main
+  </dd>
+
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/go/gocover">
+      <code>gocover PACKAGE</code>
+    </a>
+  </dt>
+  <dd>
+    run tests and report test coverage for the package
+  </dd>
+
+  <dt>
+    <a href="https://github.com/alexwlchan/scripts/blob/main/go/gostress">
+      <code>gostress PACKAGE TEST_NAME</code>
+    </a>
+  </dt>
+  <dd>
+    run a test with Go’s stress utility
+  </dd>
+</dl>
+<!-- [[[end]]] (sum: eCNO30UwC4) -->
\ No newline at end of file

go/gobench (0 → 3015)

diff --git a/go/gobench b/go/gobench
new file mode 100755
index 0000000..7213948
--- /dev/null
+++ b/go/gobench
@@ -0,0 +1,107 @@
+#!/usr/bin/env bash
+
+# gobench helps me compare benchmarks between a local branch and main,
+# so I can see the difference (and hopefully the improvement!).
+#
+# It runs samples in the local branch and main, then compares the
+# results using benchstat.
+#
+# == Usage ==
+#
+# Call `gobench` with the arguments you'd pass to `go test` to run
+# the benchmark. For example:
+#
+#     $ gobench ./control -bench=BenchmarkServerEncode
+#
+# By default, it runs 6 samples. If you want to run more samples, set
+# the SAMPLE_COUNT environment variable. For example:
+#
+#     $ SAMPLE_COUNT=12 gobench ./control -bench=BenchmarkServerEncode
+#
+# == Installation ==
+#
+# You need to install benchstat to use this script.
+#
+#     $ go install golang.org/x/perf/cmd/benchstat@latest
+#
+
+set -o errexit
+set -o nounset
+
+# 1. Get the current branch and commits to test
+CURRENT_BRANCH=$(git symbolic-ref --short -q HEAD || git rev-parse --short HEAD)
+MAIN_COMMIT=$(git rev-parse --short main)
+HEAD_COMMIT=$(git rev-parse --short HEAD)
+
+if [[ "$CURRENT_BRANCH" = "main" ]]; then
+  echo "gobench can only be run from non-main branches" >&2
+  exit 1
+fi
+
+if [[ "$MAIN_COMMIT" = "$HEAD_COMMIT" ]]; then
+  echo "Your branch is the same as main; gobench can only be run from distinct branches" >&2
+  exit 1
+fi
+
+# 2. Create a temporary log for output. Clean it up on exit or error,
+# and restore the starting branch/commit.
+TMP_LOG=$(mktemp)
+trap 'git checkout -q "$CURRENT_BRANCH"; rm -f "$TMP_LOG"' EXIT INT TERM
+
+# 3. Create a temporary directory to store output
+TMP_DIR=$(mktemp -d)
+echo "Output directory: $TMP_DIR"
+
+# 4. Determine the Go binary to use.
+#
+# Prefer `./tool/go` if present (which is common in Tailscale repos);
+# fallback to `go` if not.
+GO_BIN="go"
+if [[ -x "./tool/go" ]]; then
+  GO_BIN="./tool/go"
+fi
+
+# 5. Run alternating benchmarks
+SAMPLE_COUNT="${SAMPLE_COUNT:-6}"
+
+MAIN_OUTPUT="$TMP_DIR/main (${MAIN_COMMIT:0:7})"
+HEAD_OUTPUT="$TMP_DIR/$CURRENT_BRANCH (${HEAD_COMMIT:0:7})"
+
+mkdir -p "$(dirname "$MAIN_OUTPUT")"
+mkdir -p "$(dirname "$HEAD_OUTPUT")"
+
+for ((N=1; N<=SAMPLE_COUNT; N++)); do
+  # Benchmark main
+  echo "[$N/$SAMPLE_COUNT] Running on main ($MAIN_COMMIT)"
+  git checkout -q "$MAIN_COMMIT"
+  if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then
+    echo "ERROR: Benchmark failed:" >&2
+    cat "$TMP_LOG" >&2
+    exit 1
+  fi
+  cat "$TMP_LOG" >> "$MAIN_OUTPUT"
+
+  # Benchmark head
+  echo "[$N/$SAMPLE_COUNT] Running on $CURRENT_BRANCH ($HEAD_COMMIT)"
+  git checkout -q "$HEAD_COMMIT"
+  if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then
+    echo "ERROR: Benchmark failed:" >&2
+    cat "$TMP_LOG" >&2
+    exit 1
+  fi
+  cat "$TMP_LOG" >> "$HEAD_OUTPUT"
+done
+
+rm -f "$TMP_LOG"
+
+# 6. Compare results
+if ! command -v benchstat &>/dev/null; then
+  echo "ERROR: benchstat is required. Install via: go install golang.org/x/perf/cmd/benchstat@latest" >&2
+  exit 1
+fi
+
+echo ""
+echo "== Benchmark results =="
+pushd "$TMP_DIR" >/dev/null
+  benchstat "${MAIN_OUTPUT#$TMP_DIR/}" "${HEAD_OUTPUT#$TMP_DIR/}"
+popd >/dev/null

go/gocover (0 → 298)

diff --git a/go/gocover b/go/gocover
new file mode 100755
index 0000000..86e0445
--- /dev/null
+++ b/go/gocover
@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+set -o errexit
+set -o nounset
+
+print_info '-> ./tool/go test -coverpkg="$1" -coverprofile=coverage.out "$@"'
+./tool/go test -coverpkg="$1" -coverprofile=coverage.out "$@"
+
+echo ""
+
+print_info "-> ./tool/go tool cover -html=coverage.out"
+./tool/go tool cover -html=coverage.out

tailscale/gostress (444) → go/gostress (444)

diff --git a/tailscale/gostress b/go/gostress
similarity index 100%
rename from tailscale/gostress
rename to go/gostress

tailscale/README.md (3364 → 3030)

diff --git a/tailscale/README.md b/tailscale/README.md
index b3a34d2..ccbe021 100644
--- a/tailscale/README.md
+++ b/tailscale/README.md
@@ -26,10 +26,6 @@ scripts = [
         "description": "ensure I'm connected to a particular tailnet on my work computer"
     },
     {
-        "usage": "gostress PACKAGE TEST_NAME",
-        "description": "run a test with Go’s stress utility",
-    },
-    {
         "usage": "start_local_tailscaled.sh NAME",
         "description": "start a local instance of tailscaled",
     },
@@ -74,15 +70,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
-    <a href="https://github.com/alexwlchan/scripts/blob/main/tailscale/gostress">
-      <code>gostress PACKAGE TEST_NAME</code>
-    </a>
-  </dt>
-  <dd>
-    run a test with Go’s stress utility
-  </dd>
-
-  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/tailscale/start_local_tailscaled.sh">
       <code>start_local_tailscaled.sh NAME</code>
     </a>
@@ -127,4 +114,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     run the Yarn formatting/linting commands for the admin console
   </dd>
 </dl>
-<!-- [[[end]]] (sum: 8W27QzHggD) -->
\ No newline at end of file
+<!-- [[[end]]] (sum: 1KXMVCWaLN) -->
\ No newline at end of file