package hotpath
import "testing"
func BenchmarkParse(b *testing.B) {
b.ReportAllocs()
payload := []byte("id=123&status=active&limit=50")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = parse(payload)
}
}
func parse(_ []byte) map[string]string {
// placeholder for the thing you actually benchmark
return map[string]string{"ok": "true"}
}
When latency matters, I benchmark the smallest interesting unit and watch allocations. In Go, a surprising number of regressions come from accidental heap churn: converting []byte to string, building lots of temporary maps, or using fmt.Sprintf in loops. A benchmark with b.ReportAllocs() and b.ResetTimer() gives you fast feedback. The other key is realism: seed representative payload sizes so the benchmark reflects production. Once a baseline exists, it’s easy to see whether a refactor made things worse. I also like to run benchmarks with -benchmem and keep an eye on allocs/op; it’s often a better leading indicator than raw ns/op. Benchmarks aren’t for micro-optimizing everything—just the paths you call millions of times.
Related snips
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
Rails.application.configure do
config.after_initialize do
Bullet.enable = true
Bullet.alert = false
Bullet.bullet_logger = true
Bullet.console = true
N+1 query detection with Bullet gem
json.array! @posts do |post|
json.cache! ['v1', post], expires_in: 1.hour do
json.id post.id
json.title post.title
json.excerpt post.excerpt
json.published_at post.published_at
Fragment caching for expensive JSON serialization
package files
import (
"context"
"time"
Presigned S3 upload URLs (AWS SDK v2)
Share this code
Here's the card — post it anywhere.