Benchmark

Benchmark the bookmark store and handler with testing.B and pprof.

package main

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"
)

func BenchmarkBookmarkStore_List(b *testing.B) {
	db := setupTestDB(b)
	store := NewBookmarkStore(db)
	ctx := context.Background()

	for i := 0; i < 100; i++ {
		store.Create(ctx, fmt.Sprintf("https://example.com/%d", i), fmt.Sprintf("Bookmark %d", i))
	}

	b.ResetTimer()
	for b.Loop() {
		store.List(ctx)
	}
}

func BenchmarkHandleListBookmarks(b *testing.B) {
	db := setupTestDB(b)
	store := NewBookmarkStore(db)
	ctx := context.Background()

	for i := 0; i < 50; i++ {
		store.Create(ctx, fmt.Sprintf("https://example.com/%d", i), fmt.Sprintf("Bookmark %d", i))
	}

	mux := http.NewServeMux()
	registerRoutes(mux, store)

	req := httptest.NewRequest("GET", "/api/bookmarks", nil)
	b.ResetTimer()

	for b.Loop() {
		rec := httptest.NewRecorder()
		mux.ServeHTTP(rec, req)
	}
}

Run with go test -bench=. -benchmem ./api.

Enable pprof on a debug port in main.go:

import _ "net/http/pprof"

// inside main(), before starting the main server:
go func() {
	http.ListenAndServe(":6060", nil)
}()

Then collect a CPU profile: go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30.

⚠️ This test file references the bookmark store and handlers. Add it to the same package to run.

💻 Run locally

Copy the code above and run it on your machine

© 2026 ByteLearn.dev. Free courses for developers. · Privacy