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

Perpartition with global - Regression Tests #45415

Draft
wants to merge 44 commits into
base: master
Choose a base branch
from

Conversation

tolik0
Copy link
Contributor

@tolik0 tolik0 commented Sep 12, 2024

What

This PR is created to run the regression tests with the new cursor in low code - Per Partition with

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
airbyte-docs ⬜️ Ignored (Inspect) Visit Preview Oct 23, 2024 1:27pm

@octavia-squidington-iii octavia-squidington-iii added area/connectors Connector related issues area/documentation Improvements or additions to documentation CDK Connector Development Kit connectors/source/jira labels Sep 12, 2024
@tolik0 tolik0 changed the title erpartition with global regression tests Perpartition with global - Regression Tests Sep 12, 2024
codeflash-ai bot added a commit that referenced this pull request Sep 12, 2024
…n PR #45415 (`tolik0/airbyte-cdk/perpartition-with-global-regression-tests`)

To optimize the provided code for better performance, the main areas of focus include reducing redundant computations and minimizing potential thread contention. The logic and structure remain the same. Here is the refactored and optimized code.



### Summary of Optimizations
1. **Consolidated Conditional Checks**: Simplified boolean checks to make them more efficient.
2. **Lazy Evaluations/Short-circuiting**: Used more efficient conditional statements to avoid redundant operations when not necessary.
3. **Intermediate Variables**: Directly accessed dictionary elements where possible to avoid redundant intermediate variables.
4. **Reduced Lock Contention**: No change here as the current implementation handles threading with minimal contention; the threading lock and semaphore usage seem appropriate for their purpose. 

Note that no memory or extensive runtime profiling was done, but these changes streamline the control flow and simplify some operations, which should yield runtime improvements.
Copy link

codeflash-ai bot commented Sep 12, 2024

⚡️ Codeflash found optimizations for this PR

📄 GlobalSubstreamCursor.set_initial_state() in airbyte-cdk/python/airbyte_cdk/sources/declarative/incremental/global_substream_cursor.py

📈 Performance improved by 65% (0.65x faster)

⏱️ Runtime went down from 3.27 milliseconds to 1.98 millisecond

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch tolik0/airbyte-cdk/perpartition-with-global-regression-tests).

Copy link

codeflash-ai bot commented Sep 17, 2024

⚡️ Codeflash found optimizations for this PR

📄 Timer.finish() in airbyte-cdk/python/airbyte_cdk/sources/declarative/incremental/global_substream_cursor.py

📈 Performance improved by 40% (0.40x faster)

⏱️ Runtime went down from 3.89 milliseconds to 2.77 milliseconds

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch tolik0/airbyte-cdk/perpartition-with-global-regression-tests).

…ck' into tolik0/airbyte-cdk/perpartition-with-global-regression-tests
…ck' into tolik0/airbyte-cdk/perpartition-with-global-regression-tests
Copy link

codeflash-ai bot commented Sep 17, 2024

⚡️ Codeflash found optimizations for this PR

📄 Timer.finish() in airbyte-cdk/python/airbyte_cdk/sources/declarative/incremental/global_substream_cursor.py

📈 Performance improved by 37% (0.37x faster)

⏱️ Runtime went down from 80.4 microseconds to 58.7 microseconds

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch tolik0/airbyte-cdk/perpartition-with-global-regression-tests).

@@ -25,7 +55,7 @@ def start(self) -> None:

def finish(self) -> int:
if self._start:
return int((time.perf_counter_ns() - self._start) // 1e9)
return ((time.perf_counter_ns() - self._start) / 1e9).__ceil__()
Copy link

Choose a reason for hiding this comment

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

Suggested change
return ((time.perf_counter_ns() - self._start) / 1e9).__ceil__()
return (time.perf_counter_ns() - self._start + 999_999_999) // 1_000_000_000

Copy link

codeflash-ai bot commented Sep 18, 2024

⚡️ Codeflash found optimizations for this PR

📄 Timer.finish() in airbyte-cdk/python/airbyte_cdk/sources/declarative/incremental/global_substream_cursor.py

📈 Performance improved by 37% (0.37x faster)

⏱️ Runtime went down from 2.28 milliseconds to 1.66 millisecond

Explanation and details

Here's an optimized version of the code.

This should provide a performance improvement by reducing the overhead of function calls and improving computational efficiency.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

🔘 (none found) − ⚙️ Existing Unit Tests

✅ 1009 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import time
# function to test
from typing import Optional

import pytest  # used for our unit tests
from airbyte_cdk.sources.declarative.incremental.global_substream_cursor import \
    Timer

# unit tests

def test_timer_not_started():
    # Test that finish raises RuntimeError when called without starting the timer
    timer = Timer()
    with pytest.raises(RuntimeError, match="Global substream cursor timer not started"):
        timer.finish()
    # Outputs were verified to be equal to the original implementation

def test_timer_started_and_stopped_immediately():
    # Test that finish returns 1 when the timer is started and stopped immediately
    timer = Timer()
    timer._start = time.perf_counter_ns()
    codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation

def test_timer_started_and_stopped_after_short_duration():
    # Test that finish returns approximately 1 when waited for 1 second
    timer = Timer()
    timer._start = time.perf_counter_ns()
    time.sleep(1)
    codeflash_output = timer.finish()

    # Test that finish returns approximately 1 when waited for 0.5 seconds
    timer._start = time.perf_counter_ns()
    time.sleep(0.5)
    codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation


def test_multiple_calls_to_finish_without_restarting_timer():
    # Test that finish returns consistent values when called multiple times
    timer = Timer()
    timer._start = time.perf_counter_ns()
    time.sleep(1)
    codeflash_output = timer.finish()
    time.sleep(1)
    codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation

def test_timer_restarted_and_stopped():
    # Test that finish returns correct value after restarting the timer
    timer = Timer()
    timer._start = time.perf_counter_ns()
    time.sleep(1)
    codeflash_output = timer.finish()
    timer._start = time.perf_counter_ns()
    time.sleep(2)
    codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation

def test_edge_cases_with_very_small_time_intervals():
    # Test that finish handles very small time intervals correctly
    timer = Timer()
    timer._start = time.perf_counter_ns()
    time.sleep(0.000001)  # 1 microsecond
    codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation


def test_performance_and_scalability():
    # Test the performance and scalability by running the timer in a loop
    timer = Timer()
    for _ in range(1000):
        timer._start = time.perf_counter_ns()
        time.sleep(0.01)  # 10 milliseconds
        codeflash_output = timer.finish()
    # Outputs were verified to be equal to the original implementation

🔘 (none found) − ⏪ Replay Tests

codeflash-ai bot added a commit that referenced this pull request Sep 18, 2024
…45415 (`tolik0/airbyte-cdk/perpartition-with-global-regression-tests`)

To optimize this Python program for better runtime performance, we can take several steps. The main points for optimization involve reducing redundant operations and leveraging efficient data structures and algorithms. With that in mind, here's the optimized code.



### Explanation.

1. **Removed Unnecessary Attributes**: The attribute `_over_limit` was removed because it's not used elsewhere in the code.
2. **Optimized Dictionary Update**: For updating `self._cursor_per_partition`, used dictionary comprehensions for efficiency.
3. **Streamlined Setup**: Simplified the logic inside `set_initial_state` to reduce lookups and make the code more efficient. Specifically, checked for 'states' directly within the dictionary.
4. **General Cleanup**: Eliminated unnecessary comments and clarified existing ones for better readability and maintenance.

These changes should help improve the runtime performance of the function, especially in scenarios involving many partitions and states.
Copy link

codeflash-ai bot commented Sep 18, 2024

⚡️ Codeflash found optimizations for this PR

📄 PerPartitionCursor.set_initial_state() in airbyte-cdk/python/airbyte_cdk/sources/declarative/incremental/per_partition_cursor.py

📈 Performance improved by 6% (0.06x faster)

⏱️ Runtime went down from 292 milliseconds to 275 milliseconds

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch tolik0/airbyte-cdk/perpartition-with-global-regression-tests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/connectors Connector related issues area/documentation Improvements or additions to documentation CDK Connector Development Kit connectors/source/jira
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants