-
Notifications
You must be signed in to change notification settings - Fork 55
CM-25249 - Optimize the local files collection and improve the progress bar stage #141
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
Merged
MarshalX
merged 1 commit into
main
from
CM-25249-optimize-local-files-collection-and-improve-the-prepare-local-files-stage-of-the-progress-bar
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| TELEGRAM_BOT_TOKEN=923445010:AAGWKwWTNx_6RAuRdcp2kWax5_JltwkF2Lw |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import glob | ||
| import logging | ||
| import os | ||
| import timeit | ||
| from pathlib import Path | ||
| from typing import Dict, List, Tuple, Union | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
|
|
||
| def filter_files(paths: List[Union[Path, str]]) -> List[str]: | ||
| return [str(path) for path in paths if os.path.isfile(path)] | ||
|
|
||
|
|
||
| def get_all_files_glob(path: Union[Path, str]) -> List[str]: | ||
| # DOESN'T RETURN HIDDEN FILES. CAN'T BE USED | ||
| # and doesn't show the best performance | ||
| if not str(path).endswith(os.sep): | ||
| path = f'{path}{os.sep}' | ||
|
|
||
| return filter_files(glob.glob(f'{path}**', recursive=True)) | ||
|
|
||
|
|
||
| def get_all_files_walk(path: str) -> List[str]: | ||
| files = [] | ||
|
|
||
| for root, _, filenames in os.walk(path): | ||
| for filename in filenames: | ||
| files.append(os.path.join(root, filename)) | ||
|
|
||
| return files | ||
|
|
||
|
|
||
| def get_all_files_listdir(path: str) -> List[str]: | ||
| files = [] | ||
|
|
||
| def _(sub_path: str) -> None: | ||
| items = os.listdir(sub_path) | ||
|
|
||
| for item in items: | ||
| item_path = os.path.join(sub_path, item) | ||
|
|
||
| if os.path.isfile(item_path): | ||
| files.append(item_path) | ||
| elif os.path.isdir(item_path): | ||
| _(item_path) | ||
|
|
||
| _(path) | ||
| return files | ||
|
|
||
|
|
||
| def get_all_files_rglob(path: str) -> List[str]: | ||
| return filter_files(list(Path(path).rglob(r'*'))) | ||
|
|
||
|
|
||
| def test_get_all_files_performance(test_files_path: str) -> None: | ||
| results: Dict[str, Tuple[int, float]] = {} | ||
| for func in { | ||
| get_all_files_rglob, | ||
| get_all_files_listdir, | ||
| get_all_files_walk, | ||
| }: | ||
| name = func.__name__ | ||
| start_time = timeit.default_timer() | ||
|
|
||
| files_count = len(func(test_files_path)) | ||
|
|
||
| executed_time = timeit.default_timer() - start_time | ||
| results[name] = (files_count, executed_time) | ||
|
|
||
| logger.info(f'Time result {name}: {executed_time}') | ||
| logger.info(f'Files count {name}: {files_count}') | ||
|
|
||
| files_counts = [result[0] for result in results.values()] | ||
| assert len(set(files_counts)) == 1 # all should be equal | ||
|
|
||
| logger.info(f'Benchmark TOP with ({files_counts[0]}) files:') | ||
| for func_name, result in sorted(results.items(), key=lambda x: x[1][1]): | ||
| logger.info(f'- {func_name}: {result[1]}') | ||
|
|
||
| # according to my (MarshalX) local tests, the fastest is get_all_files_walk | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| # provide a path with thousands of files | ||
| huge_dir_path = '/Users/ilyasiamionau/projects/cycode/' | ||
| test_get_all_files_performance(huge_dir_path) | ||
|
|
||
| # Output: | ||
| # INFO:__main__:Benchmark TOP with (94882) files: | ||
| # INFO:__main__:- get_all_files_walk: 0.717258458 | ||
| # INFO:__main__:- get_all_files_listdir: 1.4648628330000002 | ||
| # INFO:__main__:- get_all_files_rglob: 2.368291458 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.