From d7653dbe472105cd4f1273ee8de28ef9b8d6daf8 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:22:27 +0000 Subject: [PATCH 01/11] avoid non needed PRs for updating test durations with better logic --- .github/scripts/average_test_durations.py | 104 +++++++++++++++++----- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/.github/scripts/average_test_durations.py b/.github/scripts/average_test_durations.py index 0de5fb45d..7019db206 100644 --- a/.github/scripts/average_test_durations.py +++ b/.github/scripts/average_test_durations.py @@ -1,30 +1,92 @@ -# .github/scripts/average_test_durations.py +# Description: This script averages the test durations from all the artifacts import glob import json +import subprocess from collections import defaultdict import os +# Function to collect test names using pytest +def collect_tests_with_pytest(): + # Run pytest with --collect-only to get the list of test cases + result = subprocess.run( + ["pytest", "--collect-only"], + capture_output=True, + text=True + ) + + collected_tests = defaultdict(list) + + # Parse the output to organize tests under their respective modules + for line in result.stdout.splitlines(): + if line.startswith("tests/"): + collected_tests[line] = 0 + + return collected_tests + + +# Consolidate durations from existing artifacts +def consolidate_durations(): + durations = defaultdict(lambda: {'total_duration': 0, 'count': 0}) + + # Iterate over all downloaded duration artifacts + for folder in glob.glob("test-durations-*"): + # The path to the duration file in each directory + duration_file_path = os.path.join(folder, ".pytest-split-durations") + + if os.path.isfile(duration_file_path): + with open(duration_file_path, "r") as f: + data = json.load(f) + for test, duration in data.items(): + durations[test]['total_duration'] += duration + durations[test]['count'] += 1 + + # Calculate the average duration for each test + return {test: info['total_duration'] / info['count'] for test, info in durations.items()} + # Define the path to the consolidated durations file -consolidated_file = "tests/test_data/.pytest-split-durations" +CONSOLIDATED_FILE = "tests/test_data/.pytest-split-durations" -# Dictionary to store total duration and count for each test -durations = defaultdict(lambda: {'total_duration': 0, 'count': 0}) -# Iterate over all downloaded duration artifacts -for folder in glob.glob("test-durations-*"): - # The path to the duration file in each directory - duration_file_path = os.path.join(folder, ".pytest-split-durations") +# Main script logic +def main(): + # Collect tests grouped by modules using pytest + collected_tests = collect_tests_with_pytest() + + # Consolidate durations from artifacts + consolidated_durations = consolidate_durations() + + + # Merge and update with consolidated durations + updated_durations = {} + for test, duration in collected_tests.items(): + if test in consolidated_durations: + # Update average duration if test exists in consolidated data + updated_durations[test] = consolidated_durations[test] + else: + # Add new test with its collected duration + updated_durations[test] = duration + + # Load the existing durations file if it exists + existing_durations = {} + if os.path.isfile(CONSOLIDATED_FILE): + with open(CONSOLIDATED_FILE, "r") as f: + existing_durations = json.load(f) + - if os.path.isfile(duration_file_path): - with open(duration_file_path, "r") as f: - data = json.load(f) - for test, duration in data.items(): - durations[test]['total_duration'] += duration - durations[test]['count'] += 1 - -# Calculate the average duration for each test -averaged_durations = {test: info['total_duration'] / info['count'] for test, info in durations.items()} - -# Write the averaged durations to the consolidated file -with open(consolidated_file, "w") as f: - json.dump(averaged_durations, f, indent=4) + updated_durations_key = sorted(updated_durations.keys()) + existing_durations_key = sorted(existing_durations.keys()) + for i, j in zip(updated_durations_key, existing_durations_key): + print(i,j) + + + # Check if all keys in updated_durations are in existing_durations + if updated_durations_key == existing_durations_key: + print("No new tests detected; durations file remains unchanged.") + else: + # Write the updated durations to the consolidated file + with open(CONSOLIDATED_FILE, "w") as f: + json.dump(updated_durations, f, indent=4) + print("New tests detected; updated the durations file.") + +if __name__ == "__main__": + main() \ No newline at end of file From ec1bf7b7db274db09bcc40e8ee7764fecc27de4f Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:25:04 +0000 Subject: [PATCH 02/11] remove debugging print --- .github/scripts/average_test_durations.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/scripts/average_test_durations.py b/.github/scripts/average_test_durations.py index 7019db206..cc95588b4 100644 --- a/.github/scripts/average_test_durations.py +++ b/.github/scripts/average_test_durations.py @@ -73,10 +73,9 @@ def main(): existing_durations = json.load(f) + # Sort the keys to compare the tests in both dictionaries updated_durations_key = sorted(updated_durations.keys()) existing_durations_key = sorted(existing_durations.keys()) - for i, j in zip(updated_durations_key, existing_durations_key): - print(i,j) # Check if all keys in updated_durations are in existing_durations From baf8d8fabe0038b84d6e4ad5607f1b584da11951 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 15:45:23 +0000 Subject: [PATCH 03/11] temp disable restrictions --- .github/workflows/docker-publish.yml | 2 +- .github/workflows/python-package.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 1f3cdd9d9..bd9f0115b 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -53,7 +53,7 @@ jobs: docker push ghcr.io/${{ env.IMAGE_NAME }}/autoplex-python-${{ matrix.python-version }}:${{ env.VERSION }} update-devcontainer: - if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' + # if: github.event_name == 'release' && github.event.action == 'created' needs: build-image runs-on: ubuntu-latest steps: diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 9749f25e6..f08eff984 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -65,7 +65,7 @@ jobs: path: ./.coverage commit-durations: - if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' + # if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' needs: build runs-on: ubuntu-latest steps: From 19e5430d8bda0dc6dd223ebeff7ab17727c00223 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:07:04 +0000 Subject: [PATCH 04/11] install pytest --- .github/workflows/python-package.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index f08eff984..0590449d3 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -79,6 +79,7 @@ jobs: # Clear out any existing content in the consolidated file > tests/test_data/.pytest-split-durations # Run the Python script to average out test durations + pip install pytest python3 .github/scripts/average_test_durations.py rm -rf test-durations-* From dc5382c1a0a8ec40d9ebc3eb8eb138c205cdb409 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:27:44 +0000 Subject: [PATCH 05/11] remove clearing out step --- .github/workflows/python-package.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 0590449d3..232e2d41a 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -76,8 +76,6 @@ jobs: - name: Compute average of test durations run: | - # Clear out any existing content in the consolidated file - > tests/test_data/.pytest-split-durations # Run the Python script to average out test durations pip install pytest python3 .github/scripts/average_test_durations.py From 7533db3130c4ba4afce57f4fb4a9e7e859b56a40 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:22:23 +0000 Subject: [PATCH 06/11] add proper release restriction --- .github/workflows/docker-publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index bd9f0115b..f4b47853f 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -53,7 +53,7 @@ jobs: docker push ghcr.io/${{ env.IMAGE_NAME }}/autoplex-python-${{ matrix.python-version }}:${{ env.VERSION }} update-devcontainer: - # if: github.event_name == 'release' && github.event.action == 'created' + if: github.event_name == 'release' && github.event.action == 'created' needs: build-image runs-on: ubuntu-latest steps: From 3fa4ea816f74eba138c73dfa110f2905e73d34ed Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:22:45 +0000 Subject: [PATCH 07/11] demo run --- .github/workflows/python-package.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 232e2d41a..a08125517 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -84,7 +84,7 @@ jobs: - name: Create Pull Request to push consolidated test durations uses: peter-evans/create-pull-request@v7 with: - token: ${{ secrets.ACTION_SECRET }} + #token: ${{ secrets.ACTION_SECRET }} commit-message: update test durations title: Update test durations file body: Auto update test durations file From f91b864990c312f7bf56f802cd7b374f98ad0d42 Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:02:38 +0000 Subject: [PATCH 08/11] demo run --- .github/workflows/python-package.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index a08125517..eff4c0f88 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -74,6 +74,7 @@ jobs: - name: Download test duration artifacts uses: actions/download-artifact@v3 + - uses: actions/checkout@v3 - name: Compute average of test durations run: | # Run the Python script to average out test durations From 3eeb13e44dd9bc91a52caa56a79610aaedb5532f Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:26:12 +0000 Subject: [PATCH 09/11] demo run3 --- .github/workflows/python-package.yml | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index eff4c0f88..e00d7afcd 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -33,7 +33,7 @@ jobs: | jq -r 'sort_by(.created_at) | reverse | .[0].metadata.container.tags[0]') echo "VERSION=$TAG" >> $GITHUB_ENV - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Run tests using Docker image for Python ${{ matrix.python-version }} run: | docker pull ghcr.io/autoatml/autoplex/autoplex-python-${{ matrix.python-version }}:${{ env.VERSION }} @@ -68,17 +68,33 @@ jobs: # if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' needs: build runs-on: ubuntu-latest + defaults: + run: + shell: bash -l {0} # enables conda/mamba env activation by reading bash profile + steps: - - uses: actions/checkout@v3 + - name: Check out repo + uses: actions/checkout@v4 + - name: Set up micromamba + uses: mamba-org/setup-micromamba@main + - name: Create mamba environment + run: | + micromamba create -n autoplex_docs python=3.10 --yes + - name: Install uv + run: micromamba run -n autoplex_docs pip install uv + - name: Install autoplex and dependencies + run: | + micromamba activate autoplex_docs + uv pip install --upgrade pip + uv pip install --prerelease=allow .[tests,strict] + - name: Download test duration artifacts uses: actions/download-artifact@v3 - - uses: actions/checkout@v3 - name: Compute average of test durations run: | # Run the Python script to average out test durations - pip install pytest python3 .github/scripts/average_test_durations.py rm -rf test-durations-* From 9059fd07321132ef3a6c09c6a6ac2983f0dc436c Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 18:48:34 +0000 Subject: [PATCH 10/11] demo run4 --- .github/workflows/python-package.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index e00d7afcd..90046cb1e 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -80,12 +80,12 @@ jobs: uses: mamba-org/setup-micromamba@main - name: Create mamba environment run: | - micromamba create -n autoplex_docs python=3.10 --yes + micromamba create -n autoplex_tests python=3.10 --yes - name: Install uv - run: micromamba run -n autoplex_docs pip install uv + run: micromamba run -n autoplex_tests pip install uv - name: Install autoplex and dependencies run: | - micromamba activate autoplex_docs + micromamba activate autoplex_tests uv pip install --upgrade pip uv pip install --prerelease=allow .[tests,strict] @@ -94,7 +94,7 @@ jobs: - name: Compute average of test durations run: | - # Run the Python script to average out test durations + micromamba activate autoplex_tests python3 .github/scripts/average_test_durations.py rm -rf test-durations-* From 352c6b1b2d43ce55586fab1a751e3fd72c00fdcc Mon Sep 17 00:00:00 2001 From: Aakash Ashok Naik <91958822+naik-aakash@users.noreply.github.com> Date: Fri, 15 Nov 2024 19:08:19 +0000 Subject: [PATCH 11/11] update to checkoutv4, and final fix --- .github/workflows/docker-publish.yml | 2 +- .github/workflows/pylint.yml | 2 +- .github/workflows/python-package.yml | 4 ++-- .github/workflows/python-publish.yml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index f4b47853f..73bd4ce63 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -20,7 +20,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index c312f5f62..d55e55c33 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -12,7 +12,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-python@v4 with: diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 90046cb1e..77f67888f 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -65,7 +65,7 @@ jobs: path: ./.coverage commit-durations: - # if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' + if: github.repository_owner == 'autoatml' && github.ref == 'refs/heads/main' needs: build runs-on: ubuntu-latest defaults: @@ -101,7 +101,7 @@ jobs: - name: Create Pull Request to push consolidated test durations uses: peter-evans/create-pull-request@v7 with: - #token: ${{ secrets.ACTION_SECRET }} + token: ${{ secrets.ACTION_SECRET }} commit-message: update test durations title: Update test durations file body: Auto update test durations file diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 55821259b..4089882c8 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -16,7 +16,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v4 with: