Debian Codegraph @

benchmarks/decode_github_test.go

raw
1 package benchmark
2
3 import (
4 stdjson "encoding/json"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/bytedance/sonic"
10 gojson "github.com/goccy/go-json"
11 )
12
13 // The decode benchmarks of the responses of the GitHub API, which is one of the most used JSON APIs: the list of
14 // the issues of a repository by the REST API ( GET /repos/{owner}/{repo}/issues ) and by the GraphQL API.
15 //
16 // The payloads are generated ( see github_payload_test.go ): the keys, their order, the types and the nulls are
17 // the ones of the responses, and the titles and the descriptions are markdown with lists, code and escapes.
18 //
19 // The REST types are the ones of the most used client, github.com/google/go-github: fields of pointers, times
20 // by a type with UnmarshalJSON, and keys of the response which no field has, as the API adds keys. The GraphQL
21 // types have no tags, as the ones of github.com/shurcooL/githubv4: their fields match the camel case keys by
22 // case folding.
23
24 // GitHubTimestamp is a time of the REST API, as go-github decodes it: an RFC 3339 string or Unix seconds.
25 type GitHubTimestamp struct {
26 time.Time
27 }
28
29 func (t *GitHubTimestamp) UnmarshalJSON(data []byte) error {
30 if len(data) > 0 && data[0] != '"' {
31 var sec int64
32 if err := stdjson.Unmarshal(data, &sec); err != nil {
33 return err
34 }
35 t.Time = time.Unix(sec, 0)
36 return nil
37 }
38 var err error
39 t.Time, err = time.Parse(`"`+time.RFC3339+`"`, string(data))
40 return err
41 }
42
43 type GitHubUser struct {
44 Login *string `json:"login,omitempty"`
45 ID *int64 `json:"id,omitempty"`
46 NodeID *string `json:"node_id,omitempty"`
47 AvatarURL *string `json:"avatar_url,omitempty"`
48 HTMLURL *string `json:"html_url,omitempty"`
49 GravatarID *string `json:"gravatar_id,omitempty"`
50 Type *string `json:"type,omitempty"`
51 SiteAdmin *bool `json:"site_admin,omitempty"`
52 URL *string `json:"url,omitempty"`
53 EventsURL *string `json:"events_url,omitempty"`
54 FollowingURL *string `json:"following_url,omitempty"`
55 FollowersURL *string `json:"followers_url,omitempty"`
56 GistsURL *string `json:"gists_url,omitempty"`
57 OrganizationsURL *string `json:"organizations_url,omitempty"`
58 ReceivedEventsURL *string `json:"received_events_url,omitempty"`
59 ReposURL *string `json:"repos_url,omitempty"`
60 StarredURL *string `json:"starred_url,omitempty"`
61 SubscriptionsURL *string `json:"subscriptions_url,omitempty"`
62 }
63
64 type GitHubLabel struct {
65 ID *int64 `json:"id,omitempty"`
66 URL *string `json:"url,omitempty"`
67 Name *string `json:"name,omitempty"`
68 Color *string `json:"color,omitempty"`
69 Description *string `json:"description,omitempty"`
70 Default *bool `json:"default,omitempty"`
71 NodeID *string `json:"node_id,omitempty"`
72 }
73
74 type GitHubMilestone struct {
75 URL *string `json:"url,omitempty"`
76 HTMLURL *string `json:"html_url,omitempty"`
77 LabelsURL *string `json:"labels_url,omitempty"`
78 ID *int64 `json:"id,omitempty"`
79 Number *int `json:"number,omitempty"`
80 State *string `json:"state,omitempty"`
81 Title *string `json:"title,omitempty"`
82 Description *string `json:"description,omitempty"`
83 Creator *GitHubUser `json:"creator,omitempty"`
84 OpenIssues *int `json:"open_issues,omitempty"`
85 ClosedIssues *int `json:"closed_issues,omitempty"`
86 CreatedAt *GitHubTimestamp `json:"created_at,omitempty"`
87 UpdatedAt *GitHubTimestamp `json:"updated_at,omitempty"`
88 ClosedAt *GitHubTimestamp `json:"closed_at,omitempty"`
89 DueOn *GitHubTimestamp `json:"due_on,omitempty"`
90 NodeID *string `json:"node_id,omitempty"`
91 }
92
93 type GitHubPullRequestLinks struct {
94 URL *string `json:"url,omitempty"`
95 HTMLURL *string `json:"html_url,omitempty"`
96 DiffURL *string `json:"diff_url,omitempty"`
97 PatchURL *string `json:"patch_url,omitempty"`
98 MergedAt *GitHubTimestamp `json:"merged_at,omitempty"`
99 }
100
101 type GitHubReactions struct {
102 TotalCount *int `json:"total_count,omitempty"`
103 PlusOne *int `json:"+1,omitempty"`
104 MinusOne *int `json:"-1,omitempty"`
105 Laugh *int `json:"laugh,omitempty"`
106 Confused *int `json:"confused,omitempty"`
107 Heart *int `json:"heart,omitempty"`
108 Hooray *int `json:"hooray,omitempty"`
109 Rocket *int `json:"rocket,omitempty"`
110 Eyes *int `json:"eyes,omitempty"`
111 URL *string `json:"url,omitempty"`
112 }
113
114 type GitHubIssue struct {
115 ID *int64 `json:"id,omitempty"`
116 Number *int `json:"number,omitempty"`
117 State *string `json:"state,omitempty"`
118 StateReason *string `json:"state_reason,omitempty"`
119 Locked *bool `json:"locked,omitempty"`
120 Title *string `json:"title,omitempty"`
121 Body *string `json:"body,omitempty"`
122 AuthorAssociation *string `json:"author_association,omitempty"`
123 User *GitHubUser `json:"user,omitempty"`
124 Labels []*GitHubLabel `json:"labels,omitempty"`
125 Assignee *GitHubUser `json:"assignee,omitempty"`
126 Comments *int `json:"comments,omitempty"`
127 ClosedAt *GitHubTimestamp `json:"closed_at,omitempty"`
128 CreatedAt *GitHubTimestamp `json:"created_at,omitempty"`
129 UpdatedAt *GitHubTimestamp `json:"updated_at,omitempty"`
130 ClosedBy *GitHubUser `json:"closed_by,omitempty"`
131 URL *string `json:"url,omitempty"`
132 HTMLURL *string `json:"html_url,omitempty"`
133 CommentsURL *string `json:"comments_url,omitempty"`
134 EventsURL *string `json:"events_url,omitempty"`
135 LabelsURL *string `json:"labels_url,omitempty"`
136 RepositoryURL *string `json:"repository_url,omitempty"`
137 Milestone *GitHubMilestone `json:"milestone,omitempty"`
138 PullRequestLinks *GitHubPullRequestLinks `json:"pull_request,omitempty"`
139 Reactions *GitHubReactions `json:"reactions,omitempty"`
140 Assignees []*GitHubUser `json:"assignees,omitempty"`
141 NodeID *string `json:"node_id,omitempty"`
142 Draft *bool `json:"draft,omitempty"`
143 ActiveLockReason *string `json:"active_lock_reason,omitempty"`
144 }
145
146 type GitHubGraphQLIssues struct {
147 Data struct {
148 Repository struct {
149 Name string
150 NameWithOwner string
151 Description string
152 URL string
153 StargazerCount int
154 ForkCount int
155 IsArchived bool
156 CreatedAt time.Time
157 UpdatedAt time.Time
158 PrimaryLanguage *struct {
159 Name string
160 Color string
161 }
162 Issues struct {
163 TotalCount int
164 PageInfo struct {
165 HasNextPage bool
166 EndCursor string
167 }
168 Nodes []GitHubGraphQLIssue
169 }
170 }
171 }
172 }
173
174 type GitHubGraphQLIssue struct {
175 ID string
176 Number int
177 Title string
178 Body string
179 State string
180 StateReason *string
181 URL string
182 CreatedAt time.Time
183 UpdatedAt time.Time
184 ClosedAt *time.Time
185 Locked bool
186 Author struct {
187 Login string
188 AvatarURL string
189 URL string
190 }
191 AuthorAssociation string
192 Labels struct {
193 Nodes []struct {
194 ID string
195 Name string
196 Color string
197 Description string
198 }
199 }
200 Assignees struct {
201 Nodes []struct {
202 Login string
203 AvatarURL string
204 }
205 }
206 Milestone *struct {
207 Title string
208 Number int
209 State string
210 DueOn *time.Time
211 }
212 Comments struct {
213 TotalCount int
214 }
215 ReactionGroups []struct {
216 Content string
217 Reactors struct {
218 TotalCount int
219 }
220 }
221 }
222
223 var (
224 githubRESTIssues = githubRESTIssuesPayload()
225 githubGraphQLIssues = githubGraphQLIssuesPayload()
226 )
227
228 func init() {
229 sonicTypes = append(sonicTypes, reflect.TypeOf([]*GitHubIssue{}), reflect.TypeOf(GitHubGraphQLIssues{}))
230 }
231
232 func TestGitHubPayloads(t *testing.T) {
233 // go-json decodes the payloads as encoding/json does.
234 var restWant, restGot []*GitHubIssue
235 if err := stdjson.Unmarshal(githubRESTIssues, &restWant); err != nil {
236 t.Fatal(err)
237 }
238 if err := gojson.Unmarshal(githubRESTIssues, &restGot); err != nil {
239 t.Fatal(err)
240 }
241 if !reflect.DeepEqual(restGot, restWant) {
242 t.Fatal("the REST payload decodes differently")
243 }
244 var graphqlWant, graphqlGot GitHubGraphQLIssues
245 if err := stdjson.Unmarshal(githubGraphQLIssues, &graphqlWant); err != nil {
246 t.Fatal(err)
247 }
248 if err := gojson.Unmarshal(githubGraphQLIssues, &graphqlGot); err != nil {
249 t.Fatal(err)
250 }
251 if !reflect.DeepEqual(graphqlGot, graphqlWant) {
252 t.Fatal("the GraphQL payload decodes differently")
253 }
254 if len(graphqlGot.Data.Repository.Issues.Nodes) == 0 || graphqlGot.Data.Repository.NameWithOwner == "" {
255 t.Fatal("the GraphQL fields are not matched by case folding")
256 }
257 }
258
259 func Benchmark_Decode_GitHubREST_Unmarshal_EncodingJson(b *testing.B) {
260 benchDecode[[]*GitHubIssue](b, githubRESTIssues, stdjson.Unmarshal)
261 }
262
263 func Benchmark_Decode_GitHubREST_Unmarshal_GoJson(b *testing.B) {
264 benchDecode[[]*GitHubIssue](b, githubRESTIssues, gojson.Unmarshal)
265 }
266
267 func Benchmark_Decode_GitHubREST_Unmarshal_GoJsonUnmarshalOf(b *testing.B) {
268 benchDecodeOf[[]*GitHubIssue](b, githubRESTIssues)
269 }
270
271 func Benchmark_Decode_GitHubREST_Unmarshal_Sonic(b *testing.B) {
272 pretouchSonic()
273 benchDecode[[]*GitHubIssue](b, githubRESTIssues, sonic.ConfigDefault.Unmarshal)
274 }
275
276 func Benchmark_Decode_GitHubREST_Unmarshal_SonicStd(b *testing.B) {
277 pretouchSonic()
278 benchDecode[[]*GitHubIssue](b, githubRESTIssues, sonic.ConfigStd.Unmarshal)
279 }
280
281 func Benchmark_Decode_GitHubGraphQL_Unmarshal_EncodingJson(b *testing.B) {
282 benchDecode[GitHubGraphQLIssues](b, githubGraphQLIssues, stdjson.Unmarshal)
283 }
284
285 func Benchmark_Decode_GitHubGraphQL_Unmarshal_GoJson(b *testing.B) {
286 benchDecode[GitHubGraphQLIssues](b, githubGraphQLIssues, gojson.Unmarshal)
287 }
288
289 func Benchmark_Decode_GitHubGraphQL_Unmarshal_GoJsonUnmarshalOf(b *testing.B) {
290 benchDecodeOf[GitHubGraphQLIssues](b, githubGraphQLIssues)
291 }
292
293 func Benchmark_Decode_GitHubGraphQL_Unmarshal_Sonic(b *testing.B) {
294 pretouchSonic()
295 benchDecode[GitHubGraphQLIssues](b, githubGraphQLIssues, sonic.ConfigDefault.Unmarshal)
296 }
297
298 func Benchmark_Decode_GitHubGraphQL_Unmarshal_SonicStd(b *testing.B) {
299 pretouchSonic()
300 benchDecode[GitHubGraphQLIssues](b, githubGraphQLIssues, sonic.ConfigStd.Unmarshal)
301 }
302
303 func Benchmark_Decode_GitHubREST_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) {
304 benchDecodeNoCopy[[]*GitHubIssue](b, githubRESTIssues)
305 }
306
307 func Benchmark_Decode_GitHubREST_Unmarshal_SonicFastest(b *testing.B) {
308 pretouchSonic()
309 benchDecodeSonicFastest[[]*GitHubIssue](b, githubRESTIssues)
310 }
311
312 func Benchmark_Decode_GitHubREST_Unmarshal_SonicFastestValidating(b *testing.B) {
313 pretouchSonic()
314 benchDecodeSonicFastestValidating[[]*GitHubIssue](b, githubRESTIssues)
315 }
316
317 func Benchmark_Decode_GitHubGraphQL_Unmarshal_GoJsonUnmarshalOfNoCopyString(b *testing.B) {
318 benchDecodeNoCopy[GitHubGraphQLIssues](b, githubGraphQLIssues)
319 }
320
321 func Benchmark_Decode_GitHubGraphQL_Unmarshal_SonicFastest(b *testing.B) {
322 pretouchSonic()
323 benchDecodeSonicFastest[GitHubGraphQLIssues](b, githubGraphQLIssues)
324 }
325
326 func Benchmark_Decode_GitHubGraphQL_Unmarshal_SonicFastestValidating(b *testing.B) {
327 pretouchSonic()
328 benchDecodeSonicFastestValidating[GitHubGraphQLIssues](b, githubGraphQLIssues)
329 }