Skip to content
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

Add Benchmarks for VersionVector Performance Analysis #1150

Merged
merged 4 commits into from
Feb 13, 2025
Merged

Conversation

chacha912
Copy link
Contributor

@chacha912 chacha912 commented Feb 12, 2025

What this PR does / why we need it:

Overview

Added benchmark tests to analyze performance implications after deciding to keep detached client's lamport in Version Vector implementation. The tests can be compared by running them in each branch:

  • v052-bench (Lamport Clock implementation)
  • v0.5.6 (Version Vector - Removes detached clients)
  • v0.5.7 (Version Vector - Maintains detached clients)
go test -bench=BenchmarkVersionVector -benchmem -v ./test/bench -tags bench 

Test Scenario

  • Multiple clients (10, 100, 1000) participate in document editing
  • Measure performance metrics before and after client detachment
  • Test environment: Apple M1 Pro (darwin/arm64)

Key Findings

  • v0.5.2 (Lamport)
Metric 10 clients 100 clients 1000 clients
Total Operation Time (ns/op) 84,962,701 793,943,416 34,794,795,104
Memory Allocations (B/op) 35,107,503 219,921,926 5,028,582,136
Number of Allocations (allocs/op) 69,271 1,246,728 81,485,288
📦 ChangePack Size (bytes) 138.0 137.0 141.0
Snapshot Size (bytes) 379.0 3,079 30,081
Push-Pull Time (ms) 2.0 1.5 4.0
Attach Time (ms) 4.5 11.0 31.0
📦 ChangePack After Detach (bytes) 138.0 140.0 141.0
Snapshot After Detach (bytes) 136.0 137.0 139.0
Push-Pull After Detach (ms) 2.5 5.0 9.5
  • v0.5.6 (VV with Delete)
Metric 10 clients 100 clients 1000 clients
Total Operation Time (ns/op) 89,737,971 1,209,092,146 77,487,980,687
Memory Allocations (B/op) 36,230,524 291,491,508 53,293,939,780
Number of Allocations (allocs/op) 83,818 1,655,231 150,513,056
📦 ChangePack Size (bytes) 746.0 6,145 60,155
Snapshot Size (bytes) 379.0 3,079 30,081
Push-Pull Time (ms) 3.5 9.5 151.5
Attach Time (ms) 4.0 5.5 184.5
📦 ChangePack After Detach (bytes) 203.0 207.0 208.0
Snapshot After Detach (bytes) 136.0 137.0 139.0
Push-Pull After Detach (ms) 2.5 5.0 9.5
  • v0.5.7 (VV with Keep)
Metric 10 clients 100 clients 1000 clients
Total Operation Time (ns/op) 92,534,033 1,468,356,125 78,426,291,687
Memory Allocations (B/op) 35,992,066 303,121,232 53,079,921,324
Number of Allocations (allocs/op) 84,306 1,636,625 147,954,826
📦 ChangePack Size (bytes) 746.0 6,145 60,155
Snapshot Size (bytes) 379.0 3,079 30,081
Push-Pull Time (ms) 3.5 10.0 175.0
Attach Time (ms) 4.0 7.0 165.5
📦 ChangePack After Detach (bytes) 806.0 6,210 60,217
Snapshot After Detach (bytes) 136.0 137.0 139.0
Push-Pull After Detach (ms) 5.0 7.0 25.5

Performance Metrics

  1. Total Operation Time

    • The performance gap widens significantly with scale, showing Lamport is ~55% faster at 1000 clients
  2. Memory Usage

    • Lamport maintains significantly better memory efficiency
    • At 1000 clients: Lamport (5GB) vs VV methods (50GB)
  3. ChangePack Size

    • Lamport: Constant size (~140 bytes) regardless of client count
    • VV methods: Linear growth with client count
      • 10→1000 clients (100x) : 746 bytes → 60,155 bytes (80.6x increase)
    • VV methods: difference in ChangePack After Detach
      • v0.5.6: Maintains smaller size (~208 bytes)
      • v0.5.7: Grows with client history (~60,217 bytes)
  4. Snapshot Size

    • The snapshot size remains identical across Lamport and VV approaches since snapshots are composed of the same root and presences data structures.
  5. Push-Pull Performance

    • Lamport maintains consistent performance (2-4ms)
    • VV methods show non-linear growth
      • At 1000 clients: Lamport (4ms) vs VV Delete (151.5ms) vs VV Keep (175ms)

These results suggest that while VV implementation provides certain benefits, it comes with significant performance trade-offs, particularly in large-scale deployments. The Lamport approach shows better scalability characteristics across all measured metrics.

  • Note: Attach time results are without using snapshots. The attachment time could be faster when utilizing snapshots.

Future Works

We plan to analyze and optimize Version Vector performance using pprof and Go trace tools to:

  • Identify bottlenecks in VV operations (e.g., minVV calculation)
  • Investigate high memory usage patterns
  • Find optimization opportunities to improve execution time

These analyses will help us understand and reduce the performance gap between VV and Lamport implementations.

Which issue(s) this PR fixes:

Addresses #1144

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


Additional documentation:


Checklist:

  • Added relevant tests or not required
  • Addressed and resolved all CodeRabbit review comments
  • Didn't break anything

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a comprehensive performance benchmarking suite to evaluate version vector functionality across varied client scenarios.
  • Refactor

    • Streamlined client connection cleanup in testing routines to enhance stability and efficiency during performance evaluations.
  • Chores

    • Updated CI workflow to trigger benchmarking jobs for changes in the test/bench directory.

@chacha912 chacha912 requested a review from hackerwins February 12, 2025 07:35
Copy link

coderabbitai bot commented Feb 12, 2025

Walkthrough

This pull request refactors cleanup logic in benchmark tests by removing the custom cleanupClients function from the gRPC benchmarks and replacing it with a centralized helper call. It also introduces a new benchmark suite for Version Vector (VV) functionality, which includes setting up a test server, generating unique document keys, initializing clients and documents, and executing benchmark iterations. Additionally, a new helper function CleanupClients is added to the helper package to standardize the cleanup process across tests. Changes to the CI workflow allow for better recognition of benchmark-related modifications.

Changes

File(s) Change Summary
test/bench/grpc_bench_test.go Removed the custom cleanupClients function and replaced its usage with helper.CleanupClients. Updated benchmarkUpdateProject function signature to include project *types.Project.
test/bench/vv_bench_test.go Added a new benchmark suite for Version Vector functionality including:
- Test server setup (startTestServer)
- Unique document key creation (createDocKey)
- Client & document initialization functions
- Core benchmark functions (benchmarkVV and BenchmarkVersionVector)
test/helper/helper.go Introduced a new helper function CleanupClients that iterates over clients to deactivate and close them, ensuring proper cleanup during tests.
.github/workflows/ci.yml Added a filter for the bench job in the ci-target-check step to recognize changes in the test/bench/** directory.

Sequence Diagram(s)

sequenceDiagram
    participant BV as BenchmarkVersionVector
    participant TS as TestServer
    participant IC as Clients/Docs Initialization
    participant VV as Benchmark Loop

    BV->>TS: startTestServer(snapshotInterval, snapshotThreshold)
    BV->>IC: initializeClientsAndDocs(n, docKey)
    IC->>IC: [Loop] For each client: initializeClientAndDoc (activate & attach document)
    BV->>VV: benchmarkVV(clientCnt)
    VV->>Clients: Execute operations (activate, text updates, sync)
    BV-->>TS: Shutdown test server (deferred)
Loading

Possibly related PRs

Suggested labels

enhancement 🌟

Suggested reviewers

  • hackerwins
  • JOOHOJANG
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/bench/vv_bench_test.go (1)

96-198: Consider adding memory usage metrics.

The benchmark effectively measures operation times and data sizes. Based on the PR objectives, which mention significant memory usage differences between implementations (5GB vs 50GB), consider adding memory profiling metrics.

Add memory profiling using the runtime package:

+import "runtime"

 func benchmarkVV(clientCnt int, b *testing.B) {
+    var m runtime.MemStats
     for i := 0; i < b.N; i++ {
+        runtime.ReadMemStats(&m)
+        initialAlloc := m.Alloc

         // ... existing code ...

+        runtime.ReadMemStats(&m)
+        b.ReportMetric(float64(m.Alloc-initialAlloc), "memory_allocated_bytes")
     }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1b2718b and 331a07a.

📒 Files selected for processing (3)
  • test/bench/grpc_bench_test.go (2 hunks)
  • test/bench/vv_bench_test.go (1 hunks)
  • test/helper/helper.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (8)
test/bench/vv_bench_test.go (5)

43-56: LGTM!

The server initialization logic is well-structured with appropriate error handling for test setup.


58-60: LGTM!

The key generation logic ensures uniqueness by combining index and timestamp.


62-78: LGTM!

The initialization logic is well-structured with appropriate error handling using test assertions.


80-94: LGTM!

The function efficiently handles bulk initialization by reusing the single initialization logic.


200-225: LGTM!

The benchmark setup is comprehensive, covering various client counts as specified in the PR objectives. The server lifecycle is properly managed.

test/bench/grpc_bench_test.go (2)

192-192: LGTM!

Good refactoring to use the centralized cleanup helper.


258-258: LGTM!

Consistent use of the centralized cleanup helper.

test/helper/helper.go (1)

557-563: LGTM!

The helper function effectively centralizes the client cleanup logic with appropriate error handling.

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Project coverage is 38.46%. Comparing base (45c3259) to head (57967d5).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
test/helper/helper.go 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1150      +/-   ##
==========================================
- Coverage   38.59%   38.46%   -0.13%     
==========================================
  Files         165      169       +4     
  Lines       25293    25433     +140     
==========================================
+ Hits         9761     9783      +22     
- Misses      14712    14829     +117     
- Partials      820      821       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/bench/grpc_bench_test.go (1)

299-304: LGTM! Consider cleanup for created project.

Good changes to use a real project for benchmarking. However, consider cleaning up the created project after the benchmark completes to prevent resource leaks.

Add project cleanup in the defer block:

 adminCli := helper.CreateAdminCli(b, defaultServer.RPCAddr())
-defer func() { adminCli.Close() }()
+defer func() {
+    if err := adminCli.DeleteProject(ctx, project.ID.String()); err != nil {
+        b.Logf("Failed to delete project: %v", err)
+    }
+    adminCli.Close()
+}()
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8564e6d and 57967d5.

📒 Files selected for processing (1)
  • test/bench/grpc_bench_test.go (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build
  • GitHub Check: bench
🔇 Additional comments (4)
test/bench/grpc_bench_test.go (4)

28-28: LGTM!

Good practice using an alias for the time package to avoid potential naming conflicts.


94-107: LGTM! Improved function reusability and name uniqueness.

Good improvements:

  1. Function now accepts a project parameter instead of using hardcoded values
  2. Using timestamps for name generation reduces the chance of conflicts

192-192: LGTM! Centralized cleanup logic.

Good refactoring to use the centralized helper.CleanupClients function, promoting code reuse and consistency.


258-258: LGTM! Consistent cleanup approach.

Good use of the centralized cleanup function, maintaining consistency with other tests.

Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

@hackerwins hackerwins changed the title Add Version Vector Benchmark Tests Add Benchmarks for VersionVector Performance Analysis Feb 13, 2025
@hackerwins hackerwins merged commit 2fa676d into main Feb 13, 2025
5 checks passed
@hackerwins hackerwins deleted the vv-bench branch February 13, 2025 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants