Debian Codegraph @

benchmarks/github_payload_test.go

raw
1 package benchmark
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 // The payloads of the GitHub API ( see decode_github_test.go ): a page of 30 issues of a repository, by the REST
9 // API and by the GraphQL API. The keys and their order are the ones of the responses; the issues are mostly pull
10 // requests with markdown descriptions, some with labels, a milestone or reactions.
11
12 const (
13 githubOwner = "example"
14 githubRepo = "json-codec"
15 githubAPI = "https://api.github.com"
16 githubWeb = "https://github.com"
17 )
18
19 var githubNow = time.Date(2026, 9, 24, 12, 0, 0, 0, time.UTC)
20
21 type githubUserInfo struct {
22 login string
23 id int64
24 }
25
26 var githubUsers = []githubUserInfo{{"maintainer", 209884}, {"contributor-one", 28623}, {"gopher42", 982358}, {"dependabot[bot]", 49699333}}
27
28 func githubRESTUser(u githubUserInfo) object {
29 api := githubAPI + "/users/" + u.login
30 typ := "User"
31 if u.id == 49699333 {
32 typ = "Bot"
33 }
34 return object{
35 {"login", u.login},
36 {"id", u.id},
37 {"node_id", fmt.Sprintf("MDQ6VXNlcjE%d", u.id)},
38 {"avatar_url", fmt.Sprintf("https://avatars.githubusercontent.com/u/%d?v=4", u.id)},
39 {"gravatar_id", ""},
40 {"url", api},
41 {"html_url", githubWeb + "/" + u.login},
42 {"followers_url", api + "/followers"},
43 {"following_url", api + "/following{/other_user}"},
44 {"gists_url", api + "/gists{/gist_id}"},
45 {"starred_url", api + "/starred{/owner}{/repo}"},
46 {"subscriptions_url", api + "/subscriptions"},
47 {"organizations_url", api + "/orgs"},
48 {"repos_url", api + "/repos"},
49 {"events_url", api + "/events{/privacy}"},
50 {"received_events_url", api + "/received_events"},
51 {"type", typ},
52 {"user_view_type", "public"},
53 {"site_admin", false},
54 }
55 }
56
57 var githubLabels = []struct{ name, color, description string }{
58 {"bug", "d73a4a", "Something isn't working"},
59 {"enhancement", "a2eeef", "New feature or request"},
60 {"performance", "fbca04", "Faster encoding or decoding"},
61 {"good first issue", "7057ff", "Good for newcomers"},
62 }
63
64 func githubRESTLabel(r *payloadRand, i int) object {
65 l := githubLabels[i]
66 return object{
67 {"id", 1000000000 + int64(i)*7919},
68 {"node_id", "LA_kwDOD" + r.token(base64URL, 14)},
69 {"url", githubAPI + "/repos/" + githubOwner + "/" + githubRepo + "/labels/" + l.name},
70 {"name", l.name},
71 {"color", l.color},
72 {"default", i < 2},
73 {"description", l.description},
74 }
75 }
76
77 // githubIssue is what the REST and the GraphQL payloads of an issue are made of.
78 type githubIssue struct {
79 number int
80 title, body string
81 author, closer githubUserInfo
82 pullRequest, closed bool
83 merged bool
84 labels []int
85 milestone bool
86 comments int
87 reactions [8]int
88 created, updated, closedAt time.Time
89 }
90
91 func newGitHubIssues(r *payloadRand, bodyBytes, codeOneIn int) []githubIssue {
92 issues := make([]githubIssue, 30)
93 for i := range issues {
94 is := &issues[i]
95 is.number = 659 - i
96 is.title = sentence(r)
97 is.title = is.title[:len(is.title)-1]
98 is.body = markdownWithCode(r, r.between(bodyBytes/10, bodyBytes*2), codeOneIn)
99 is.author = githubUsers[r.intn(len(githubUsers))]
100 is.closer = githubUsers[0]
101 is.pullRequest = !r.chance(8)
102 is.closed = !r.chance(6)
103 is.merged = is.pullRequest && is.closed && !r.chance(5)
104 for l := range githubLabels {
105 if r.chance(4) {
106 is.labels = append(is.labels, l)
107 }
108 }
109 is.milestone = r.chance(5)
110 is.comments = r.intn(4)
111 for k := range is.reactions {
112 if r.chance(6) {
113 is.reactions[k] = r.between(1, 21)
114 }
115 }
116 is.created = githubNow.Add(-time.Duration(r.between(3600, 90*86400)) * time.Second)
117 is.updated = is.created.Add(time.Duration(r.between(60, 86400)) * time.Second)
118 is.closedAt = is.updated
119 }
120 return issues
121 }
122
123 func githubRESTIssuesPayload() []byte {
124 r := newPayloadRand(1)
125 repo := githubAPI + "/repos/" + githubOwner + "/" + githubRepo
126 var issues []any
127 for _, is := range newGitHubIssues(r, 1600, 100) {
128 url := fmt.Sprintf("%s/issues/%d", repo, is.number)
129 kind := "issues"
130 if is.pullRequest {
131 kind = "pull"
132 }
133 labels := []any{}
134 for _, l := range is.labels {
135 labels = append(labels, githubRESTLabel(r, l))
136 }
137 var milestone any
138 if is.milestone {
139 milestone = object{
140 {"url", repo + "/milestones/3"},
141 {"html_url", githubWeb + "/" + githubOwner + "/" + githubRepo + "/milestone/3"},
142 {"labels_url", repo + "/milestones/3/labels"},
143 {"id", 11873412},
144 {"node_id", "MI_kwDOD" + r.token(base64URL, 14)},
145 {"number", 3},
146 {"title", "v0.11.0"},
147 {"description", sentence(r)},
148 {"creator", githubRESTUser(githubUsers[0])},
149 {"open_issues", 12},
150 {"closed_issues", 41},
151 {"state", "open"},
152 {"created_at", timestamp(githubNow.AddDate(0, -4, 0))},
153 {"updated_at", timestamp(githubNow.AddDate(0, 0, -2))},
154 {"due_on", timestamp(githubNow.AddDate(0, 1, 0))},
155 {"closed_at", nil},
156 }
157 }
158 state, closedAt, closedBy, stateReason := "open", any(nil), any(nil), any(nil)
159 if is.closed {
160 state, closedAt, closedBy = "closed", timestamp(is.closedAt), githubRESTUser(is.closer)
161 if !is.pullRequest {
162 stateReason = "completed"
163 }
164 }
165 o := object{
166 {"url", url},
167 {"repository_url", repo},
168 {"labels_url", url + "/labels{/name}"},
169 {"comments_url", url + "/comments"},
170 {"events_url", url + "/events"},
171 {"html_url", fmt.Sprintf("%s/%s/%s/%s/%d", githubWeb, githubOwner, githubRepo, kind, is.number)},
172 {"id", 5523508452 + int64(is.number)*1537},
173 {"node_id", "PR_kwDOD" + r.token(base64URL, 17)},
174 {"number", is.number},
175 {"title", is.title},
176 {"user", githubRESTUser(is.author)},
177 {"labels", labels},
178 {"state", state},
179 {"locked", false},
180 {"assignees", []any{}},
181 {"milestone", milestone},
182 {"comments", is.comments},
183 {"created_at", timestamp(is.created)},
184 {"updated_at", timestamp(is.updated)},
185 {"closed_at", closedAt},
186 {"assignee", nil},
187 {"author_association", "CONTRIBUTOR"},
188 {"active_lock_reason", nil},
189 }
190 if is.pullRequest {
191 var mergedAt any
192 if is.merged {
193 mergedAt = timestamp(is.closedAt)
194 }
195 pr := fmt.Sprintf("%s/%s/%s/pull/%d", githubWeb, githubOwner, githubRepo, is.number)
196 o = append(o,
197 field{"draft", false},
198 field{"pull_request", object{
199 {"url", fmt.Sprintf("%s/pulls/%d", repo, is.number)},
200 {"html_url", pr},
201 {"diff_url", pr + ".diff"},
202 {"patch_url", pr + ".patch"},
203 {"merged_at", mergedAt},
204 }},
205 )
206 }
207 total := 0
208 for _, n := range is.reactions {
209 total += n
210 }
211 o = append(o,
212 field{"body", is.body},
213 field{"closed_by", closedBy},
214 field{"reactions", object{
215 {"url", url + "/reactions"},
216 {"total_count", total},
217 {"+1", is.reactions[0]},
218 {"-1", is.reactions[1]},
219 {"laugh", is.reactions[2]},
220 {"hooray", is.reactions[3]},
221 {"confused", is.reactions[4]},
222 {"heart", is.reactions[5]},
223 {"rocket", is.reactions[6]},
224 {"eyes", is.reactions[7]},
225 }},
226 field{"timeline_url", url + "/timeline"},
227 field{"performed_via_github_app", nil},
228 field{"state_reason", stateReason},
229 )
230 if !is.pullRequest {
231 o = append(o,
232 field{"sub_issues_summary", object{{"total", 0}, {"completed", 0}, {"percent_completed", 0}}},
233 field{"issue_dependencies_summary", object{{"blocked_by", 0}, {"total_blocked_by", 0}, {"blocking", 0}, {"total_blocking", 0}}},
234 field{"pinned_comment", nil},
235 )
236 }
237 issues = append(issues, o)
238 }
239 return encodePayload(issues)
240 }
241
242 var githubReactionContents = []string{"THUMBS_UP", "THUMBS_DOWN", "LAUGH", "HOORAY", "CONFUSED", "HEART", "ROCKET", "EYES"}
243
244 func githubGraphQLIssuesPayload() []byte {
245 r := newPayloadRand(2)
246 var nodes []any
247 for _, is := range newGitHubIssues(r, 1800, 3) {
248 state, stateReason, closedAt := "OPEN", any(nil), any(nil)
249 if is.closed {
250 state, stateReason, closedAt = "CLOSED", "COMPLETED", timestamp(is.closedAt)
251 }
252 labels := []any{}
253 for _, l := range is.labels {
254 labels = append(labels, object{
255 {"id", "LA_kwDOD" + r.token(base64URL, 14)},
256 {"name", githubLabels[l].name},
257 {"color", githubLabels[l].color},
258 {"description", githubLabels[l].description},
259 })
260 }
261 var milestone any
262 if is.milestone {
263 milestone = object{{"title", "v0.11.0"}, {"number", 3}, {"state", "OPEN"}, {"dueOn", timestamp(githubNow.AddDate(0, 1, 0))}}
264 }
265 reactions := make([]any, len(githubReactionContents))
266 for k, content := range githubReactionContents {
267 reactions[k] = object{{"content", content}, {"reactors", object{{"totalCount", is.reactions[k]}}}}
268 }
269 nodes = append(nodes, object{
270 {"id", "I_kwDOD" + r.token(base64URL, 11)},
271 {"number", is.number},
272 {"title", is.title},
273 {"body", is.body},
274 {"state", state},
275 {"stateReason", stateReason},
276 {"url", fmt.Sprintf("%s/%s/%s/issues/%d", githubWeb, githubOwner, githubRepo, is.number)},
277 {"createdAt", timestamp(is.created)},
278 {"updatedAt", timestamp(is.updated)},
279 {"closedAt", closedAt},
280 {"locked", false},
281 {"author", object{
282 {"login", is.author.login},
283 {"avatarUrl", fmt.Sprintf("https://avatars.githubusercontent.com/u/%d?u=%s&v=4", is.author.id, r.token(hexDigits, 40))},
284 {"url", githubWeb + "/" + is.author.login},
285 }},
286 {"authorAssociation", "CONTRIBUTOR"},
287 {"labels", object{{"nodes", labels}}},
288 {"assignees", object{{"nodes", []any{}}}},
289 {"milestone", milestone},
290 {"comments", object{{"totalCount", is.comments}}},
291 {"reactionGroups", reactions},
292 })
293 }
294 return encodePayload(object{{"data", object{{"repository", object{
295 {"name", githubRepo},
296 {"nameWithOwner", githubOwner + "/" + githubRepo},
297 {"description", "Fast JSON encoder and decoder compatible with encoding/json"},
298 {"url", githubWeb + "/" + githubOwner + "/" + githubRepo},
299 {"stargazerCount", 3708},
300 {"forkCount", 220},
301 {"isArchived", false},
302 {"createdAt", "2020-04-19T09:32:36Z"},
303 {"updatedAt", timestamp(githubNow)},
304 {"primaryLanguage", object{{"name", "Go"}, {"color", "#00ADD8"}}},
305 {"issues", object{
306 {"totalCount", 269},
307 {"pageInfo", object{{"hasNextPage", true}, {"endCursor", "Y3Vyc29yOnYyOpK5MjAyNi0wOS0yNFQx" + r.token(base64URL, 20)}}},
308 {"nodes", nodes},
309 }},
310 }}}}})
311 }