| 1 |
PKG := github.com/goccy/go-json |
| 2 |
|
| 3 |
BIN_DIR := $(CURDIR)/bin |
| 4 |
PKGS := $(shell go list ./... | grep -v internal/cmd|grep -v test) |
| 5 |
COVER_PKGS := $(foreach pkg,$(PKGS),$(subst $(PKG),.,$(pkg))) |
| 6 |
|
| 7 |
COMMA := , |
| 8 |
EMPTY := |
| 9 |
SPACE := $(EMPTY) $(EMPTY) |
| 10 |
COVERPKG_OPT := $(subst $(SPACE),$(COMMA),$(COVER_PKGS)) |
| 11 |
|
| 12 |
$(BIN_DIR): |
| 13 |
@mkdir -p $(BIN_DIR) |
| 14 |
|
| 15 |
.PHONY: cover |
| 16 |
cover: |
| 17 |
go test -coverpkg=$(COVERPKG_OPT) -coverprofile=cover.out ./... |
| 18 |
|
| 19 |
.PHONY: cover-html |
| 20 |
cover-html: cover |
| 21 |
go tool cover -html=cover.out |
| 22 |
|
| 23 |
.PHONY: lint |
| 24 |
lint: golangci-lint |
| 25 |
$(BIN_DIR)/golangci-lint run |
| 26 |
|
| 27 |
golangci-lint: | $(BIN_DIR) |
| 28 |
@{ \ |
| 29 |
set -e; \ |
| 30 |
GOLANGCI_LINT_TMP_DIR=$$(mktemp -d); \ |
| 31 |
cd $$GOLANGCI_LINT_TMP_DIR; \ |
| 32 |
go mod init tmp; \ |
| 33 |
GOBIN=$(BIN_DIR) go install github.com/golangci/golangci-lint/v2/cmd/[email protected]; \ |
| 34 |
rm -rf $$GOLANGCI_LINT_TMP_DIR; \ |
| 35 |
} |
| 36 |
|
| 37 |
.PHONY: generate |
| 38 |
generate: |
| 39 |
go generate ./internal/... |
| 40 |
|
| 41 |
# Fails if the benchmarks of go-json under ./benchmarks are degraded compared with the master branch: |
| 42 |
# their mean is slower beyond -tolerance, or one of them is slower beyond -single-tolerance. |
| 43 |
# The benchmarks of the other libraries are never measured. |
| 44 |
# Options of internal/cmd/benchcheck can be passed by BENCH_CHECK_FLAGS. |
| 45 |
# e.g.) make bench-check BENCH_CHECK_FLAGS="-bench GoJson -no-cache" |
| 46 |
.PHONY: bench-check |
| 47 |
bench-check: |
| 48 |
go run ./internal/cmd/benchcheck $(BENCH_CHECK_FLAGS) |
| 49 |
|
| 50 |
# bench-calibrate measures the benchmarks of HEAD against themselves, linked with other function layouts, |
| 51 |
# and prints how much the results of identical code differ: the noise which bench-check has to see beyond. |
| 52 |
# e.g.) make bench-calibrate BENCH_CHECK_FLAGS="-group decode" |
| 53 |
.PHONY: bench-calibrate |
| 54 |
bench-calibrate: |
| 55 |
go run ./internal/cmd/benchcheck -calibrate -attempts 1 $(BENCH_CHECK_FLAGS) |
| 56 |
|
| 57 |
# bench-compare-encode prints the encode benchmarks of go-json and of bytedance/sonic side by side. |
| 58 |
# SonicStd is sonic configured to do what encoding/json, and go-json, do ( escape HTML, sort the keys of a map ), |
| 59 |
# and GoJsonLikeSonic is go-json configured to do what sonic does by default. |
| 60 |
# SonicFastest is sonic.ConfigFastest. SONIC_MAX_INLINE_DEPTH sets how deep sonic inlines the nested structs. |
| 61 |
# The Twitter benchmarks are the ones of sonic itself, with go-json beside it ( benchmarks/sonic_bench_test.go, under the license of sonic ). |
| 62 |
.PHONY: bench-compare-encode |
| 63 |
bench-compare-encode: |
| 64 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_(Encode|Marshal|EncodeBigData|MarshalBigData).*_(GoJson|GoJsonLikeSonic|Sonic|SonicFastest|SonicStd)$$' -benchtime 300ms -count 3 . |
| 65 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Twitter' -benchtime 300ms -count 3 . |
| 66 |
|
| 67 |
# bench-compare-decode prints the decode benchmarks of go-json and of bytedance/sonic side by side. |
| 68 |
# Sonic is sonic.ConfigDefault and SonicStd is sonic.ConfigStd, which validates the strings as encoding/json does. |
| 69 |
# SonicFastest is sonic at its fastest, and GoJsonUnmarshalOfNoCopyString go-json at its ( benchmarks/sonic_decode_test.go ). |
| 70 |
# SonicFastestValidating is SonicFastest with the checks which go-json does: the values which are skipped and the |
| 71 |
# strings are validated. |
| 72 |
# The Twitter benchmarks decode the payload of sonic's own benchmarks ( benchmarks/sonic_bench_test.go ), and the |
| 73 |
# Keys ones structs whose keys differ only, in length or in script ( benchmarks/decode_key_length_test.go ), and the |
| 74 |
# GitHub ones the responses of the REST and the GraphQL APIs of GitHub ( benchmarks/decode_github_test.go ), and |
| 75 |
# the OpenAI and Anthropic ones the responses and the streams of the APIs of LLMs ( benchmarks/llm_api_test.go ). |
| 76 |
# BENCH_LIVE_HEAP_MB gives the benchmarks a live heap of that size, as a real program has, which sets the goal of the |
| 77 |
# GC for every library alike ( benchmarks/live_heap_test.go ). |
| 78 |
.PHONY: bench-compare-decode |
| 79 |
bench-compare-decode: |
| 80 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Decode_(Small|Medium|Large)Struct_Unmarshal_(GoJson|GoJsonUnmarshalOfNoCopyString|Sonic|SonicStd|SonicFastest|SonicFastestValidating)$$' -benchtime 300ms -count 3 . |
| 81 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Decode_Twitter' -benchtime 300ms -count 3 . |
| 82 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Decode_(Short|Medium|Long|NonASCII|UnknownNonASCII)Keys_Unmarshal_' -benchtime 300ms -count 3 . |
| 83 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Decode_GitHub(REST|GraphQL)_Unmarshal_' -benchtime 300ms -count 3 . |
| 84 |
cd benchmarks && go test -run '^$$' -bench '^Benchmark_Decode_(OpenAIChatCompletion|OpenAIResponse|AnthropicMessage)(Stream)?_Unmarshal_' -benchtime 300ms -count 3 . |
| 85 |
|
| 86 |
# bench-report measures the JSON libraries doing the same work, as the benchmark report does on the CI, and |
| 87 |
# renders the page and the summary into benchmarks/site ( see benchmarks/report_test.go ). |
| 88 |
.PHONY: bench-report |
| 89 |
bench-report: |
| 90 |
mkdir -p benchmarks/site |
| 91 |
cd benchmarks && BENCH_REPORT_OUT=$(CURDIR)/benchmarks/site/results-$$(go env GOARCH).json go test -run '^TestReport$$' -count=1 -timeout 60m -test.benchtime=100ms . |
| 92 |
cd benchmarks && go run ./cmd/benchreport -out site site/results-$$(go env GOARCH).json |
| 93 |
|
| 94 |
# bench-profile-decode prints where the CPU time of the decode benchmarks of go-json goes. |
| 95 |
.PHONY: bench-profile-decode |
| 96 |
bench-profile-decode: |
| 97 |
cd benchmarks && for b in Decode_SmallStruct_Unmarshal_GoJson Decode_MediumStruct_Unmarshal_GoJson Decode_LargeStruct_Unmarshal_GoJson Decode_TwitterBinding_Unmarshal_GoJson Decode_TwitterGeneric_Unmarshal_GoJson Decode_LargeStruct_Stream_GoJson; do \ |
| 98 |
go test -run '^$$' -bench "^Benchmark_$$b$$" -benchtime 3s -cpuprofile /tmp/$$b.prof -o /tmp/bench.test . > /dev/null && \ |
| 99 |
echo "=== $$b" && go tool pprof -top -nodecount=22 /tmp/bench.test /tmp/$$b.prof 2>/dev/null | tail -n +5 && \ |
| 100 |
echo "--- by line" && go tool pprof -top -lines -nodecount=30 /tmp/bench.test /tmp/$$b.prof 2>/dev/null | tail -n +6; \ |
| 101 |
done |
| 102 |
|
| 103 |
# bench-profile-encode prints where the CPU time of the encode benchmarks of go-json goes. |
| 104 |
.PHONY: bench-profile-encode |
| 105 |
bench-profile-encode: |
| 106 |
cd benchmarks && for b in Encode_SmallStructCached_GoJson Encode_MediumStructCached_GoJson Encode_LargeStructCached_GoJson Encode_MapInterface_GoJson TwitterGeneric_GoJson TwitterGeneric_GoJsonLikeSonicFast TwitterParallelBinding_GoJson; do \ |
| 107 |
go test -run '^$$' -bench "^Benchmark_$${b}$$" -benchtime 3s -cpuprofile /tmp/$$b.prof -o /tmp/bench.test . > /dev/null && \ |
| 108 |
echo "=== $$b" && go tool pprof -top -nodecount=22 /tmp/bench.test /tmp/$$b.prof 2>/dev/null | tail -n +5 && \ |
| 109 |
echo "--- by line" && go tool pprof -top -lines -nodecount=30 /tmp/bench.test /tmp/$$b.prof 2>/dev/null | tail -n +6; \ |
| 110 |
done |
| 111 |
|
| 112 |
# bench-variants measures the candidates of the optimizations against what is used now. |
| 113 |
.PHONY: bench-variants |
| 114 |
bench-variants: |
| 115 |
go test -run '^$$' -bench 'BenchmarkVariant|BenchmarkAppendString|BenchmarkScanString' -benchtime 300ms -count 2 ./internal/encoder/ |