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 timeTaken to query response #1270

Merged
merged 2 commits into from
Mar 25, 2025
Merged

Conversation

parmesant
Copy link
Contributor

@parmesant parmesant commented Mar 25, 2025

Fixes #XXXX.

Description


This PR has:

  • been tested to ensure log ingestion and log query works.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added documentation for new or modified features or behaviors.

Summary by CodeRabbit

  • New Features
    • Query responses now include execution time details, providing users with insight into the duration of query operations without altering current behavior.
    • HTTP response headers now feature a new timing header for enhanced performance tracking.

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link

coderabbitai bot commented Mar 25, 2025

Walkthrough

The changes enhance the query function in the HTTP handler by introducing timing measurements for query execution. A new variable total_time is created to store the formatted elapsed time after executing the query. This variable is included in the QueryResponse struct returned in the HTTP response, along with a new header for the elapsed time defined by the constant TIME_ELAPSED_HEADER. The existing logic of the function remains unchanged.

Changes

Files Change Summary
src/handlers/.../query.rs, src/response.rs Added a new field total_time (type String) to the QueryResponse struct. Introduced the constant TIME_ELAPSED_HEADER in both files. Updated the HTTP response to include total_time in the headers.

Sequence Diagram(s)

Loading
sequenceDiagram
    participant C as Client
    participant QH as Query Handler
    participant QE as Query Execution

    C->>QH: Send query request
    QH->>QH: Start timer (Instant::now())
    QH->>QE: Execute query
    QE-->>QH: Return query results
    QH->>QH: Compute elapsed time (total_time)
    QH->>C: Send response with total_time included

Possibly related PRs

  • add time field names to the list #1244: Introduces a constant TIME_ELAPSED_HEADER and a field total_time in the QueryResponse struct, enhancing timing information in HTTP responses.
  • log: track conversion times #1241: Similar to the main PR, this one also introduces a constant TIME_ELAPSED_HEADER and a field total_time in the QueryResponse struct for improved timing information in responses.

Suggested reviewers

  • nikhilsinhaparseable
  • de-sh

Poem

I hopped through lines of code so bright,
Adding time to track our query's flight.
With every tick and every byte,
A new field shines with timing light.
Enjoy the change, from dusk till night! 🥕
Coding in rhythm, pure delight!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 947e049 and ccc540b.

📒 Files selected for processing (2)
  • src/handlers/http/query.rs (2 hunks)
  • src/response.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/handlers/http/query.rs
  • src/response.rs
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: Quest Smoke and Load Tests for Distributed deployments
  • GitHub Check: Quest Smoke and Load Tests for Standalone deployments
  • GitHub Check: coverage
  • GitHub Check: Build Default x86_64-unknown-linux-gnu
  • GitHub Check: Build Default aarch64-unknown-linux-gnu
  • GitHub Check: Build Default x86_64-apple-darwin
  • GitHub Check: Build Default aarch64-apple-darwin
  • GitHub Check: Build Kafka aarch64-apple-darwin
  • GitHub Check: Build Default x86_64-pc-windows-msvc
  • GitHub Check: Build Kafka x86_64-unknown-linux-gnu

🪧 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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

🔭 Outside diff range comments (1)
src/handlers/http/query.rs (1)

69-153: 💡 Verification agent

🧩 Analysis chain

Verify timing function with automated tests.

The PR objectives mention that testing has not been completed. Adding unit tests to verify that the timing information is correctly included in the response would help ensure the functionality works as expected.

Consider adding a test that verifies:

  1. The response JSON contains the totalTime field
  2. The value is properly formatted
  3. The time reported is reasonable for the executed query

🌐 Web query:

What's the best way to test timing functionality in Rust?

💡 Result:

Testing timing functionality in Rust requires approaches that ensure determinism and avoid real-time delays. Here's a concise guide to best practices:


1. Mocking Time in Unit Tests

For synchronous code, use conditional compilation and thread-local storage to override time functions:

#[cfg(test)]
mod mock_time {
    use std::cell::RefCell;
    thread_local! { static MOCK_TIME: RefCell<Option<SystemTime>> = RefCell::new(None); }
    
    pub fn now() -> SystemTime { /* ... */ }
    pub fn set_mock_time(time: SystemTime) { /* ... */ }
}
  • Non-test code uses SystemTime::now()
  • Tests control mocked time via set_mock_time()
    Gotcha: #[cfg(test)] doesn’t propagate across crates—use RUSTFLAGS or dedicated crates like faketime [1].

2. Async Runtime Time Control

For async code (e.g., Tokio timers), use runtime instrumentation:

#[tokio::test(start_paused = true)]
async fn test_timer() {
    let start = Instant::now();
    sleep(Duration::from_secs(10)).await;
    assert_eq!(start.elapsed().as_secs(), 10); // Instant result
}
  • start_paused = true freezes time until manually advanced [7]
  • Use time::advance(Duration).await to trigger timers deterministically [4]

3. Preventing Optimizations

For benchmarking loops, use std::hint::black_box to prevent compiler optimizations:

let start = Instant::now();
for _ in 0..100_000 {
    std::hint::black_box(test_function());
}
let elapsed = start.elapsed();

Avoid SystemTime for microbenchmarks—use Instant for monotonic timing [6][12].


Key Libraries

Use Case Crate/Feature Notes
Async time mocking tokio::test-util Built-in with Tokio runtime
Deterministic test runs ditto_time Custom time control for async workflows
Performance benchmarks criterion Statistical analysis of timings

Common Pitfalls

  • Flaky Tests: Avoid real-time sleeps; use mockable time sources [4]
  • Cross-Crate Mocking: Use RUSTFLAGS='--cfg feature_flag' for shared time logic [1]
  • Doc Tests: RUSTDOCFLAGS may not honor RUSTFLAGS for mocked time [1]

For complex async systems, combine thread-local time controls with runtime instrumentation to fully isolate timing behavior in tests [4][7].

Citations:


Action: Add Automated Timing Tests Using Deterministic Time Control

Please add unit tests to verify the timing logic in the query handler. In these tests, ensure that:

  • The response JSON includes the total_time (or equivalent) field.
  • The timing value is properly formatted (e.g., matches the expected duration string format).
  • The measured elapsed time is within a logically reasonable range.

For asynchronous code, consider using deterministic time control techniques (like Tokio’s #[tokio::test(start_paused = true)] or appropriate time-mocking libraries) to avoid real delays and ensure reproducible results.

🧹 Nitpick comments (2)
src/handlers/http/query.rs (2)

136-136: Consider using a more consistent time format for API consumers.

The debug formatting ({:?}) produces human-readable output but might be difficult to parse programmatically. Consider a more structured approach like providing milliseconds as a number or using a standardized time format string.

-let total_time = format!("{:?}", start.elapsed());
+let total_time = format!("{:.3}s", start.elapsed().as_secs_f64());

Alternatively, if you prefer a more machine-parseable format:

-let total_time = format!("{:?}", start.elapsed());
+let total_time = start.elapsed().as_millis().to_string();

69-143: Consider adding documentation for the new timing functionality.

Adding a brief comment explaining the purpose of the timing measurement would improve code maintainability for future developers.

 pub async fn query(req: HttpRequest, query_request: Query) -> Result<HttpResponse, QueryError> {
+    // Start timing the query execution to track performance
     let start = Instant::now();
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d9da876 and 947e049.

📒 Files selected for processing (2)
  • src/handlers/http/query.rs (2 hunks)
  • src/response.rs (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: Build Default x86_64-pc-windows-msvc
  • GitHub Check: Build Default x86_64-unknown-linux-gnu
  • GitHub Check: Build Default aarch64-apple-darwin
  • GitHub Check: Build Default aarch64-unknown-linux-gnu
  • GitHub Check: Build Kafka x86_64-unknown-linux-gnu
  • GitHub Check: coverage
  • GitHub Check: Quest Smoke and Load Tests for Standalone deployments
  • GitHub Check: Quest Smoke and Load Tests for Distributed deployments
🔇 Additional comments (5)
src/response.rs (3)

31-31: Field addition looks good.

The addition of the total_time field to the QueryResponse struct is appropriate for storing the query execution time.


50-55: Consistent field inclusion with proper JSON structure.

The totalTime field is correctly added to the JSON response when with_fields is true, maintaining consistent camelCase naming convention.


57-60: Proper handling of the alternate response format.

The totalTime field is also correctly included when with_fields is false, ensuring the timing information is always present in the response regardless of the format requested.

src/handlers/http/query.rs (2)

70-70: Timer initialization at function start is correct.

Using Instant::now() at the beginning of the function ensures the timing captures the entire query execution process.


137-143: The QueryResponse struct is correctly populated with timing information.

The total_time field is properly included in the QueryResponse struct, ensuring that the timing information will be available in the HTTP response.

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 25, 2025
@nikhilsinhaparseable nikhilsinhaparseable self-requested a review March 25, 2025 08:49
Copy link
Contributor

@nikhilsinhaparseable nikhilsinhaparseable left a comment

Choose a reason for hiding this comment

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

looks good

@nitisht nitisht merged commit 40b3195 into parseablehq:main Mar 25, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants