loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Reduce memory usage in testgit (#15306)

* reduce memory use in rawtest

* just use hashsum for diffs

Signed-off-by: Andrew Thornton <art27@cantab.net>

authored by

zeripath and committed by
GitHub
8be2cc4f b101fa83

+69 -25
+17 -25
integrations/git_test.go
··· 208 208 209 209 // Request raw paths 210 210 req := NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", little)) 211 - resp := session.MakeRequest(t, req, http.StatusOK) 212 - assert.Equal(t, littleSize, resp.Body.Len()) 211 + resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK) 212 + assert.Equal(t, littleSize, resp.Length) 213 213 214 214 setting.CheckLFSVersion() 215 215 if setting.LFS.StartServer { 216 216 req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", littleLFS)) 217 - resp = session.MakeRequest(t, req, http.StatusOK) 217 + resp := session.MakeRequest(t, req, http.StatusOK) 218 218 assert.NotEqual(t, littleSize, resp.Body.Len()) 219 219 assert.LessOrEqual(t, resp.Body.Len(), 1024) 220 220 if resp.Body.Len() != littleSize && resp.Body.Len() <= 1024 { ··· 224 224 225 225 if !testing.Short() { 226 226 req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", big)) 227 - resp = session.MakeRequest(t, req, http.StatusOK) 228 - assert.Equal(t, bigSize, resp.Body.Len()) 227 + resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK) 228 + assert.Equal(t, bigSize, resp.Length) 229 229 230 230 if setting.LFS.StartServer { 231 231 req = NewRequest(t, "GET", path.Join("/", username, reponame, "/raw/branch/master/", bigLFS)) 232 - resp = session.MakeRequest(t, req, http.StatusOK) 232 + resp := session.MakeRequest(t, req, http.StatusOK) 233 233 assert.NotEqual(t, bigSize, resp.Body.Len()) 234 234 if resp.Body.Len() != bigSize && resp.Body.Len() <= 1024 { 235 235 assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier) ··· 450 450 t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 451 451 452 452 // Then get the diff string 453 - var diffStr string 453 + var diffHash string 454 454 t.Run("GetDiff", func(t *testing.T) { 455 455 req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index)) 456 - resp := ctx.Session.MakeRequest(t, req, http.StatusOK) 457 - diffStr = resp.Body.String() 456 + resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK) 457 + diffHash = string(resp.Hash.Sum(nil)) 458 458 }) 459 459 460 460 // Now: Merge the PR & make sure that doesn't break the PR page or change its diff 461 461 t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index)) 462 462 t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 463 - t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr)) 463 + t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash)) 464 464 465 465 // Then: Delete the head branch & make sure that doesn't break the PR page or change its diff 466 466 t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch)) 467 467 t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 468 - t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr)) 468 + t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash)) 469 469 470 470 // Delete the head repository & make sure that doesn't break the PR page or change its diff 471 471 t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx)) 472 472 t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr)) 473 - t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffStr)) 473 + t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash)) 474 474 } 475 475 } 476 476 ··· 514 514 } 515 515 } 516 516 517 - func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffStr string) func(t *testing.T) { 517 + func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string) func(t *testing.T) { 518 518 return func(t *testing.T) { 519 519 req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index)) 520 - resp := ctx.Session.MakeRequest(t, req, http.StatusOK) 521 - expectedMaxLen := len(diffStr) 522 - if expectedMaxLen > 800 { 523 - expectedMaxLen = 800 524 - } 525 - actual := resp.Body.String() 526 - actualMaxLen := len(actual) 527 - if actualMaxLen > 800 { 528 - actualMaxLen = 800 529 - } 520 + resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK) 521 + actual := string(resp.Hash.Sum(nil)) 530 522 531 - equal := diffStr == actual 532 - assert.True(t, equal, "Unexpected change in the diff string: expected: %s but was actually: %s", diffStr[:expectedMaxLen], actual[:actualMaxLen]) 523 + equal := diffHash == actual 524 + assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s but was actually: %s", diffHash, actual) 533 525 } 534 526 } 535 527
+52
integrations/integration_test.go
··· 9 9 "context" 10 10 "database/sql" 11 11 "fmt" 12 + "hash" 13 + "hash/fnv" 12 14 "io" 13 15 "net/http" 14 16 "net/http/cookiejar" ··· 54 56 // NewRecorder returns an initialized ResponseRecorder. 55 57 func NewNilResponseRecorder() *NilResponseRecorder { 56 58 return &NilResponseRecorder{ 59 + ResponseRecorder: *httptest.NewRecorder(), 60 + } 61 + } 62 + 63 + type NilResponseHashSumRecorder struct { 64 + httptest.ResponseRecorder 65 + Hash hash.Hash 66 + Length int 67 + } 68 + 69 + func (n *NilResponseHashSumRecorder) Write(b []byte) (int, error) { 70 + _, _ = n.Hash.Write(b) 71 + n.Length += len(b) 72 + return len(b), nil 73 + } 74 + 75 + // NewRecorder returns an initialized ResponseRecorder. 76 + func NewNilResponseHashSumRecorder() *NilResponseHashSumRecorder { 77 + return &NilResponseHashSumRecorder{ 78 + Hash: fnv.New32(), 57 79 ResponseRecorder: *httptest.NewRecorder(), 58 80 } 59 81 } ··· 284 306 return resp 285 307 } 286 308 309 + func (s *TestSession) MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder { 310 + t.Helper() 311 + baseURL, err := url.Parse(setting.AppURL) 312 + assert.NoError(t, err) 313 + for _, c := range s.jar.Cookies(baseURL) { 314 + req.AddCookie(c) 315 + } 316 + resp := MakeRequestNilResponseHashSumRecorder(t, req, expectedStatus) 317 + 318 + ch := http.Header{} 319 + ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";")) 320 + cr := http.Request{Header: ch} 321 + s.jar.SetCookies(baseURL, cr.Cookies()) 322 + 323 + return resp 324 + } 325 + 287 326 const userPassword = "password" 288 327 289 328 var loginSessionCache = make(map[string]*TestSession, 10) ··· 419 458 func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder { 420 459 t.Helper() 421 460 recorder := NewNilResponseRecorder() 461 + c.ServeHTTP(recorder, req) 462 + if expectedStatus != NoExpectedStatus { 463 + if !assert.EqualValues(t, expectedStatus, recorder.Code, 464 + "Request: %s %s", req.Method, req.URL.String()) { 465 + logUnexpectedResponse(t, &recorder.ResponseRecorder) 466 + } 467 + } 468 + return recorder 469 + } 470 + 471 + func MakeRequestNilResponseHashSumRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseHashSumRecorder { 472 + t.Helper() 473 + recorder := NewNilResponseHashSumRecorder() 422 474 c.ServeHTTP(recorder, req) 423 475 if expectedStatus != NoExpectedStatus { 424 476 if !assert.EqualValues(t, expectedStatus, recorder.Code,