forked from autoatml/autoplex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request autoatml#255 from naik-aakash/combine_workflows
Fix recursive autoupdate durations
- Loading branch information
Showing
5 changed files
with
107 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,30 +1,91 @@ | ||
# .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) | ||
# Sort the keys to compare the tests in both dictionaries | ||
updated_durations_key = sorted(updated_durations.keys()) | ||
existing_durations_key = sorted(existing_durations.keys()) | ||
|
||
|
||
# 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() |
This file contains 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 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 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 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