|
| 1 | +# CodSpeed Integration Setup for Go REST API |
| 2 | + |
| 3 | +This document describes the CodSpeed integration setup for comprehensive API benchmarking. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +CodSpeed integration has been successfully implemented with comprehensive benchmarks covering all API routes: |
| 8 | + |
| 9 | +- **Health Check**: `/healthcheck` |
| 10 | +- **Authentication**: `/v1/login` |
| 11 | +- **Album Management**: CRUD operations for `/v1/albums` |
| 12 | + |
| 13 | +## Files Created |
| 14 | + |
| 15 | +### Benchmark Files (`/benchmarks/`) |
| 16 | + |
| 17 | +1. **`setup_test.go`** - Shared benchmark utilities and mock implementations |
| 18 | +2. **`healthcheck_bench_test.go`** - Health check endpoint benchmarks |
| 19 | +3. **`auth_bench_test.go`** - Authentication endpoint benchmarks |
| 20 | +4. **`albums_bench_test.go`** - Album CRUD operation benchmarks |
| 21 | + |
| 22 | +### Workflow File |
| 23 | +- **`.github/workflows/codspeed.yml`** - CodSpeed GitHub Actions workflow |
| 24 | + |
| 25 | +## Benchmark Coverage |
| 26 | + |
| 27 | +### Health Check Benchmarks |
| 28 | +- `BenchmarkHealthcheck_GET` - Basic health check performance |
| 29 | + |
| 30 | +### Authentication Benchmarks |
| 31 | +- `BenchmarkAuth_Login_Success` - Successful login performance |
| 32 | +- `BenchmarkAuth_Login_InvalidCredentials` - Invalid credential handling |
| 33 | + |
| 34 | +### Album Management Benchmarks |
| 35 | +- `BenchmarkAlbums_GET_Single` - Single album retrieval |
| 36 | +- `BenchmarkAlbums_GET_List_Small` - Small paginated list (10 items) |
| 37 | +- `BenchmarkAlbums_GET_List_Large` - Large paginated list (50 items) |
| 38 | +- `BenchmarkAlbums_GET_List_Paginated` - Different pagination scenarios |
| 39 | +- `BenchmarkAlbums_POST_Create` - Album creation (authenticated) |
| 40 | +- `BenchmarkAlbums_PUT_Update` - Album updates (authenticated) |
| 41 | +- `BenchmarkAlbums_DELETE_Remove` - Album deletion (authenticated) |
| 42 | +- `BenchmarkAlbums_CRUD_Mixed` - Mixed CRUD operations |
| 43 | +- `BenchmarkAlbums_Concurrent_Read` - Concurrent read performance |
| 44 | + |
| 45 | +## Running Benchmarks Locally |
| 46 | + |
| 47 | +### Prerequisites |
| 48 | +- Go 1.21 or higher |
| 49 | +- All project dependencies installed (`go mod download`) |
| 50 | + |
| 51 | +### Commands |
| 52 | + |
| 53 | +```bash |
| 54 | +# Run all benchmarks |
| 55 | +go test -bench=. ./benchmarks/... |
| 56 | + |
| 57 | +# Run specific benchmark category |
| 58 | +go test -bench=BenchmarkAuth_ ./benchmarks/... |
| 59 | +go test -bench=BenchmarkAlbums_ ./benchmarks/... |
| 60 | +go test -bench=BenchmarkHealthcheck_ ./benchmarks/... |
| 61 | + |
| 62 | +# Run with memory profiling |
| 63 | +go test -bench=. -benchmem ./benchmarks/... |
| 64 | + |
| 65 | +# Verify benchmark compilation |
| 66 | +go test -c -o /dev/null ./benchmarks/... |
| 67 | +``` |
| 68 | + |
| 69 | +## CodSpeed GitHub Integration |
| 70 | + |
| 71 | +### Workflow Features |
| 72 | +- **Triggers**: Push to master, pull requests, manual dispatch |
| 73 | +- **Database**: PostgreSQL service for realistic testing |
| 74 | +- **Dependencies**: Automatic Go dependency installation |
| 75 | +- **Environment**: Database migrations and JWT key setup |
| 76 | +- **Verification**: Benchmark compilation check before running |
| 77 | +- **Execution**: CodSpeed action with time mode |
| 78 | + |
| 79 | +### Required Secrets |
| 80 | +Add `CODSPEED_TOKEN` to your GitHub repository secrets for CodSpeed integration. |
| 81 | + |
| 82 | +### Workflow Configuration |
| 83 | +The workflow inherits most configuration from the existing `build.yml` but adds: |
| 84 | +- CodSpeed-specific benchmark execution |
| 85 | +- Time mode for accurate performance measurement |
| 86 | +- Benchmark compilation verification step |
| 87 | + |
| 88 | +## Performance Baseline |
| 89 | + |
| 90 | +Initial benchmark results on Apple M1 Pro: |
| 91 | + |
| 92 | +``` |
| 93 | +BenchmarkHealthcheck_GET-10 117,393 ops @ 993 ns/op |
| 94 | +BenchmarkAuth_Login_Success-10 60,602 ops @ 1,983 ns/op |
| 95 | +BenchmarkAuth_Login_InvalidCredentials-10 56,492 ops @ 2,043 ns/op |
| 96 | +BenchmarkAlbums_GET_Single-10 67,444 ops @ 1,794 ns/op |
| 97 | +BenchmarkAlbums_GET_List_Small-10 13,710 ops @ 8,575 ns/op |
| 98 | +BenchmarkAlbums_GET_List_Large-10 3,981 ops @ 33,869 ns/op |
| 99 | +BenchmarkAlbums_POST_Create-10 10,000 ops @ 20,509 ns/op |
| 100 | +BenchmarkAlbums_PUT_Update-10 36,844 ops @ 3,282 ns/op |
| 101 | +BenchmarkAlbums_DELETE_Remove-10 64,342 ops @ 1,625 ns/op |
| 102 | +BenchmarkAlbums_CRUD_Mixed-10 20,562 ops @ 6,996 ns/op |
| 103 | +BenchmarkAlbums_Concurrent_Read-10 18,304 ops @ 6,547 ns/op |
| 104 | +``` |
| 105 | + |
| 106 | +## Architecture Decisions |
| 107 | + |
| 108 | +### Mock Infrastructure |
| 109 | +- **In-memory repositories** for consistent, fast benchmarks |
| 110 | +- **Mock authentication** using existing test utilities |
| 111 | +- **Pre-populated test data** (100 albums) for realistic scenarios |
| 112 | + |
| 113 | +### Benchmark Design |
| 114 | +- **Route coverage** - Every API endpoint has dedicated benchmarks |
| 115 | +- **Authentication testing** - Both success and failure scenarios |
| 116 | +- **Performance patterns** - Different data sizes and access patterns |
| 117 | +- **Concurrent testing** - Parallel execution benchmarks |
| 118 | +- **Error handling** - Comprehensive panic recovery and validation |
| 119 | + |
| 120 | +### CodSpeed Compliance |
| 121 | +- **Standard library usage** - Avoiding external test frameworks in benchmarks |
| 122 | +- **Clean benchmark functions** - Simple, focused performance measurements |
| 123 | +- **Consistent setup** - Shared utilities without affecting timing |
| 124 | +- **Proper validation** - Status code verification without assertion libraries |
| 125 | + |
| 126 | +## Next Steps |
| 127 | + |
| 128 | +1. **Add `CODSPEED_TOKEN` secret** to GitHub repository |
| 129 | +2. **Push changes** to trigger initial CodSpeed run |
| 130 | +3. **Monitor performance** through CodSpeed dashboard |
| 131 | +4. **Set performance thresholds** based on baseline results |
| 132 | +5. **Integrate alerts** for performance regressions |
| 133 | + |
| 134 | +The setup is complete and ready for production use! |
0 commit comments