-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
236 lines (209 loc) · 7.93 KB
/
Makefile
File metadata and controls
236 lines (209 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
.PHONY: help test bench bench-all bench-compare clean build
# Default target
help:
@echo "Available commands:"
@echo " test - Run all tests"
@echo " bench - Run all benchmarks"
@echo " bench-all - Run all benchmarks with detailed output"
@echo " bench-compare- Run benchmarks with comparison between approaches"
@echo " bench-summary- Show clean performance comparison table"
@echo " bench-table - Show all benchmarks in table format"
@echo " mod-tidy - Run go mod tidy while preserving Go 1.20"
@echo " mod-verify - Verify Go version is correctly set to 1.20"
@echo " mod-reset - Reset go.mod to proper Go 1.20 state"
@echo " fmt - Format code with go fmt and gofmt -s"
@echo " fmt-check - Check if code is properly formatted"
@echo " vet - Run go vet"
@echo " check - Run all quality checks (fmt, vet, test)"
@echo " quality-check- Run comprehensive quality checks for release"
@echo " report-check - Check for Go Report Card issues"
@echo " install-tools- Install quality check tools"
@echo " build - Build the project"
@echo " clean - Clean build artifacts"
# Run tests
test:
go test -v ./...
# Run tests with coverage
test-coverage:
go test -coverprofile=coverage.out ./...
@echo "Coverage report generated: coverage.out"
@echo "To view the coverage report, run:"
@echo "make view-coverage"
# View coverage report in browser
view-coverage:
@echo "Opening coverage report in browser..."
@go tool cover -html=coverage.out -o coverage.html
@open coverage.html || xdg-open coverage.html || start coverage.html
# Run benchmarks
bench:
go test -bench=. -benchmem
# Run benchmarks with more detailed output
bench-all:
@echo "=== Running All Benchmarks ==="
@echo ""
@echo "Legend:"
@echo "- Generated: Current optimized approach with compile-time constants"
@echo "- Legacy: Runtime map-based error registry"
@echo "- StaticCode: uint64 constants with message maps"
@echo "- VarCode: var declarations with message maps"
@echo ""
go test -bench=. -benchmem -count=3
# Run benchmarks with comparison focus
bench-compare:
@echo "=== Benchmark Comparison: Different Approaches ==="
@echo ""
@echo "Comparing 4 different error handling approaches:"
@echo "1. Generated (current): Compile-time optimized"
@echo "2. Legacy: Runtime map lookup"
@echo "3. StaticCode: Static constants + maps"
@echo "4. VarCode: Variable declarations + maps"
@echo ""
@echo "Performance Metrics:"
@echo "- ns/op: nanoseconds per operation (lower is better)"
@echo "- B/op: bytes allocated per operation (lower is better)"
@echo "- allocs/op: allocations per operation (lower is better)"
@echo ""
go test -bench="PolicyNotFound$$|MultipleErrors$$|ErrorMessage$$|JSON$$" -benchmem -count=5
# Build the project
build:
go build ./...
# Build command line tool
build-tool:
go build -o bin/rescodegen ./cmd/rescodegen
# Clean build artifacts
clean:
go clean ./...
rm -rf bin/
# Run examples
run-example-basic:
cd examples/basic && go run main.go
run-example-microservice:
cd examples/microservice && go run service.go
# Generate code for examples
generate-basic:
cd examples/basic && go run ../../cmd/rescodegen/main.go -input errors.yaml -output errors_gen.go
generate-microservice:
cd examples/microservice && go run ../../cmd/rescodegen/main.go -input errors.json -output service_errors.go
# Development helpers
mod-tidy:
@echo "Running go mod tidy while preserving Go 1.20..."
@if grep -q "go 1\.20" go.mod; then \
go mod tidy; \
if ! grep -q "go 1\.20" go.mod; then \
echo "WARNING: Go version was changed! Restoring Go 1.20..."; \
sed -i '' 's/go [0-9]\+\.[0-9]\+.*/go 1.20/' go.mod; \
fi; \
else \
echo "ERROR: go.mod doesn't contain 'go 1.20'. Please check manually."; \
exit 1; \
fi
@echo "✓ Go version preserved at 1.20"
mod-verify:
@echo "Verifying Go version in go.mod..."
@if grep -q "go 1\.20" go.mod; then \
echo "✓ Go version is correctly set to 1.20"; \
else \
echo "✗ Go version is not 1.20. Current version:"; \
grep "^go " go.mod; \
exit 1; \
fi
# Reset go.mod to proper state if it gets corrupted
mod-reset:
@echo "Resetting go.mod to Go 1.20 with compatible dependencies..."
@rm -f go.mod go.sum
@go mod init github.com/restayway/rescode
@sed -i '' 's/go [0-9]\+\.[0-9]\+.*/go 1.20/' go.mod
@go get google.golang.org/grpc@v1.56.3
@go get gopkg.in/yaml.v3@v3.0.1
@go mod edit -require=google.golang.org/grpc@v1.56.3 -require=gopkg.in/yaml.v3@v3.0.1
@go mod tidy
@make mod-verify
@echo "✓ go.mod reset successfully"
# Format code
fmt:
go fmt ./...
gofmt -s -w .
fmt-check:
@echo "Checking code formatting..."
@if [ "$$(gofmt -s -d . | wc -l)" -eq 0 ]; then \
echo "✓ All files are properly formatted"; \
else \
echo "✗ Some files need formatting:"; \
gofmt -s -d .; \
echo "Run 'make fmt' to fix formatting issues"; \
exit 1; \
fi
# Run go vet
vet:
go vet ./...
# Check for Go Report Card issues
report-check:
@echo "=== Go Report Card Quality Check ==="
@echo ""
@echo "1. Checking gofmt..."
@make fmt-check
@echo ""
@echo "2. Checking go vet..."
@go vet ./...
@echo "✓ go vet passed"
@echo ""
@echo "3. Checking gocyclo (if available)..."
@if command -v gocyclo >/dev/null 2>&1; then \
gocyclo -over 15 .; \
if [ $$? -eq 0 ]; then echo "✓ gocyclo passed"; fi; \
else \
echo "⚠ gocyclo not installed (optional)"; \
fi
@echo ""
@echo "4. Checking ineffassign (if available)..."
@if command -v ineffassign >/dev/null 2>&1; then \
ineffassign .; \
if [ $$? -eq 0 ]; then echo "✓ ineffassign passed"; fi; \
else \
echo "⚠ ineffassign not installed (optional)"; \
fi
@echo ""
@echo "5. Checking misspell (if available)..."
@if command -v misspell >/dev/null 2>&1; then \
misspell -error .; \
if [ $$? -eq 0 ]; then echo "✓ misspell passed"; fi; \
else \
echo "⚠ misspell not installed (optional)"; \
fi
@echo ""
@echo "✓ Go Report Card check completed!"
# Install quality check tools
install-tools:
@echo "Installing Go quality check tools..."
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
go install github.com/gordonklaus/ineffassign@latest
go install github.com/client9/misspell/cmd/misspell@latest
@echo "✓ Quality tools installed"
# Full development check
check: fmt-check vet test
@echo "✓ All quality checks passed!"
# Code quality check for CI/release
quality-check: mod-verify fmt-check vet test
@echo "✓ All quality checks passed - ready for release!"
# Show benchmark results in a table format
bench-table:
@echo "Running benchmarks and formatting results..."
@go test -bench=. -benchmem | grep -E "(Benchmark|PASS)" | grep -v "PASS" | \
awk 'BEGIN {printf "%-40s %12s %8s %12s\n", "Benchmark", "ns/op", "B/op", "allocs/op"; print "========================================================================="} \
{printf "%-40s %12s %8s %12s\n", $$1, $$3, $$5, $$7}'
# Summary comparison of all approaches
bench-summary:
@echo "=== Performance Comparison Summary ==="
@echo ""
@echo "Running benchmarks for PolicyNotFound scenario..."
@go test -bench="PolicyNotFound$$" -benchmem -count=1 | grep "Benchmark" | \
awk 'BEGIN {printf "%-35s %12s %10s %12s\n", "Approach", "ns/op", "B/op", "allocs/op"; print "==============================================================================="} \
/BenchmarkGenerated_PolicyNotFound/ {printf "%-35s %12s %10s %12s\n", "Generated (Optimized)", $$3, $$5, $$7} \
/BenchmarkLegacy_PolicyNotFound/ {printf "%-35s %12s %10s %12s\n", "Legacy (Map Lookup)", $$3, $$5, $$7} \
/BenchmarkStaticCode_PolicyNotFound/ {printf "%-35s %12s %10s %12s\n", "Static Constants + Maps", $$3, $$5, $$7} \
/BenchmarkVarCode_PolicyNotFound/ {printf "%-35s %12s %10s %12s\n", "Var Declarations + Maps", $$3, $$5, $$7}'
@echo ""
@echo "Key Insights:"
@echo "- Generated approach: ~80x faster, zero allocations"
@echo "- All map-based approaches: Similar performance, 1 allocation per call"
@echo "- Lower ns/op = faster, Lower B/op = less memory, Lower allocs/op = better"