-
Notifications
You must be signed in to change notification settings - Fork 0
test(bench): enable TUS upload performance testing for files up to 1TB #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf40ede
test(bench): enable TUS upload performance testing for files up to 1TB
pcfreak30 2ce6412
ci(test): separate benchmark execution from regular tests
pcfreak30 69bee62
fix(test): resolve import cycle by moving GetStandardTestOptions to t…
pcfreak30 650bfff
ci(bench): remove unnecessary setup steps from benchmark job
pcfreak30 04ef89d
fix(test): replace hardcoded UserID with dynamic user ID in TUS uploa…
pcfreak30 dfe109e
fix(bench): add option to disable pin timeout for large file uploads
pcfreak30 ccf91f2
fix(bench): prevent infinite loop when workflow fails in pin wait
pcfreak30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
internal/protocol/bench_tests/op_tus_upload_bench_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| package bench_tests | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/rand" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "runtime/pprof" | ||
|
|
||
| "github.com/docker/go-units" | ||
|
|
||
| pluginCore "go.lumeweb.com/portal-plugin-ipfs/core" | ||
| "go.lumeweb.com/portal-plugin-ipfs/internal" | ||
| "go.lumeweb.com/portal-plugin-ipfs/internal/db" | ||
| tusTestUtils "go.lumeweb.com/portal-plugin-ipfs/internal/testing/tus" | ||
| "go.lumeweb.com/portal-plugin-ipfs/internal/testing/util" | ||
| "go.lumeweb.com/portal/core" | ||
| coreTesting "go.lumeweb.com/portal/core/testing" | ||
| "go.lumeweb.com/queryutil" | ||
| "go.lumeweb.com/queryutil/filter" | ||
| ) | ||
|
|
||
| func createLargeTestFile(tb coreTesting.TB, size int64) *os.File { | ||
| tb.Helper() | ||
|
|
||
| tempDir := tb.TempDir() | ||
| filePath := filepath.Join(tempDir, fmt.Sprintf("test-file-%d.dat", size)) | ||
|
|
||
| file, err := os.Create(filePath) | ||
| if err != nil { | ||
| tb.Fatalf("Failed to create test file: %v", err) | ||
| } | ||
|
|
||
| chunkSize := 1024 * 1024 | ||
| chunk := make([]byte, chunkSize) | ||
| bytesWritten := int64(0) | ||
|
|
||
| for bytesWritten < size { | ||
| remaining := size - bytesWritten | ||
| if remaining < int64(chunkSize) { | ||
| chunk = chunk[:remaining] | ||
| } | ||
|
|
||
| _, err := rand.Read(chunk) | ||
| if err != nil { | ||
| file.Close() | ||
| tb.Fatalf("Failed to generate random data: %v", err) | ||
| } | ||
|
|
||
| _, err = file.Write(chunk) | ||
| if err != nil { | ||
| file.Close() | ||
| tb.Fatalf("Failed to write to test file: %v", err) | ||
| } | ||
|
|
||
| bytesWritten += int64(len(chunk)) | ||
| } | ||
|
|
||
| _, err = file.Seek(0, 0) | ||
| if err != nil { | ||
| file.Close() | ||
| tb.Fatalf("Failed to seek to beginning of test file: %v", err) | ||
| } | ||
|
|
||
| return file | ||
| } | ||
|
|
||
| func benchmarkLargeUpload(tb coreTesting.TB, ctx coreTesting.TestContext, size int64) { | ||
| testFile := createLargeTestFile(tb, size) | ||
| defer testFile.Close() | ||
|
|
||
| _, requestID := tusTestUtils.SetupTUSUpload(tb, ctx, testFile, nil) | ||
|
|
||
| wfTest := coreTesting.NewWorkflowTest(ctx) | ||
| wf := wfTest.NewOperationWorkflow(core.TUSUploadOperationName(internal.ProtocolName)) | ||
|
|
||
| workflowOptions := []core.WorkflowOption{ | ||
| core.WithWorkflowUserID(1), | ||
| core.WithWorkflowSourceIP("127.0.0.1"), | ||
| } | ||
|
|
||
| req := wfTest.GetRequest(requestID) | ||
| uploadStart := time.Now() | ||
|
|
||
| wfTest.MustConvertRequestToWorkflow(requestID, wf, 0, workflowOptions...) | ||
| wfTest.ExecuteWorkflowStep(req) | ||
| wfTest.CompleteWorkflowStep(req) | ||
|
|
||
| uploadDuration := time.Since(uploadStart) | ||
| tb.Logf("Upload: %v, Size: %.2f GB", uploadDuration, float64(size)/(1024*1024*1024)) | ||
|
|
||
| tusTestUtils.AssertTUSWorkflowSuccess(wfTest, req) | ||
|
|
||
| pinSvc := core.GetService[pluginCore.IPFSPinService](ctx, pluginCore.PIN_SERVICE) | ||
| if pinSvc == nil { | ||
| tb.Skip("Pin service not available") | ||
| } | ||
|
|
||
| startTime := time.Now() | ||
| maxPinTimeout := 30 * time.Minute | ||
| if timeoutStr := os.Getenv("TUS_BENCH_PIN_TIMEOUT"); timeoutStr != "" { | ||
| if duration, err := time.ParseDuration(timeoutStr); err == nil { | ||
| maxPinTimeout = duration | ||
| } | ||
| } | ||
|
|
||
| for time.Since(startTime) < maxPinTimeout { | ||
| sort := []filter.Sort{{Field: "created_at", Order: filter.OrderDesc}} | ||
| pins, _, _ := pinSvc.ListPins(ctx, nil, sort, queryutil.DefaultPagination) | ||
|
|
||
| for _, pin := range pins { | ||
| if pin.UserID == 1 && pin.Status == db.PinningStatusPinned { | ||
| pinDuration := time.Since(startTime) | ||
| throughputMBps := float64(size) / (1024 * 1024) / uploadDuration.Seconds() | ||
| tb.Logf("Pin wait: %v, Throughput: %.2f MB/s", pinDuration, throughputMBps) | ||
| return | ||
| } | ||
| } | ||
| time.Sleep(5 * time.Second) | ||
| } | ||
| tb.Fatalf("Timeout waiting for pin status") | ||
| } | ||
|
|
||
| type benchmarkTestCase struct { | ||
| name string | ||
| size int64 | ||
| } | ||
|
|
||
| var benchmarkCases = []benchmarkTestCase{ | ||
| {"1GB", 1 * units.GB}, | ||
| {"10GB", 10 * units.GB}, | ||
| {"100GB", 100 * units.GB}, | ||
| {"1TB", units.TB}, | ||
| } | ||
|
|
||
| func BenchmarkTUSUpload(b *testing.B) { | ||
| for _, tc := range benchmarkCases { | ||
| b.Run(tc.name, func(b *testing.B) { | ||
| if testing.Short() && tc.name == "1TB" { | ||
| b.Skip("Skipping 1TB benchmark in short mode") | ||
| } | ||
| b.ResetTimer() | ||
| for b.Loop() { | ||
| coreTesting.RunTestCaseWithDB(b, func(tb coreTesting.TB, ctx coreTesting.TestContext) { | ||
| benchmarkLargeUpload(tb, ctx, tc.size) | ||
| }, util.GetStandardTestOptions()...) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkTUSUpload_Profile(b *testing.B) { | ||
| if testing.Short() { | ||
| b.Skip("Skipping performance profiling in short mode") | ||
| } | ||
| if os.Getenv("TUS_BENCH_ENABLE_PROFILING") != "true" { | ||
| b.Skip("Set TUS_BENCH_ENABLE_PROFILING=true to enable profiling") | ||
| } | ||
|
|
||
| profilesDir := os.Getenv("TUS_BENCH_PROFILES_DIR") | ||
| if profilesDir == "" { | ||
| profilesDir = b.TempDir() | ||
| } | ||
|
|
||
| cpuProfileFile := filepath.Join(profilesDir, "cpu.prof") | ||
| memProfileFile := filepath.Join(profilesDir, "mem.prof") | ||
|
|
||
| f, err := os.Create(cpuProfileFile) | ||
| if err != nil { | ||
| b.Fatalf("Could not create CPU profile: %v", err) | ||
| } | ||
| defer f.Close() | ||
| if err := pprof.StartCPUProfile(f); err != nil { | ||
| b.Fatalf("Could not start CPU profile: %v", err) | ||
| } | ||
| defer pprof.StopCPUProfile() | ||
|
|
||
| for _, tc := range benchmarkCases { | ||
| b.Run(tc.name+"-Profile", func(b *testing.B) { | ||
| b.ResetTimer() | ||
| for b.Loop() { | ||
| coreTesting.RunTestCaseWithDB(b, func(tb coreTesting.TB, ctx coreTesting.TestContext) { | ||
| benchmarkLargeUpload(tb, ctx, tc.size) | ||
| }, util.GetStandardTestOptions()...) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| f2, err := os.Create(memProfileFile) | ||
| if err != nil { | ||
| b.Fatalf("Could not create memory profile: %v", err) | ||
| } | ||
| defer f2.Close() | ||
| runtime.GC() | ||
| if err := pprof.WriteHeapProfile(f2); err != nil { | ||
| b.Fatalf("Could not write memory profile: %v", err) | ||
| } | ||
|
|
||
| b.Logf("Profiles saved to %s", profilesDir) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.