mik-laj commented on a change in pull request #7650: [AIRFLOW-7008][WIP] Add perf kit with common used decorators/contexts URL: https://github.com/apache/airflow/pull/7650#discussion_r389755483
########## File path: scripts/perf/perf_kit/imports.py ########## @@ -0,0 +1,68 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import builtins +import sys +from contextlib import contextmanager + + +class TraceImportResult: + def __init__(self): + self.new_modules = None + + def print_new_modules(self): + for i, new_module in enumerate(sorted(trace_resuslt.new_modules)): + print(f"{i}. {new_module}") + + +@contextmanager +def trace_imports(): + real_import = builtins.__import__ + before_modules = set(sys.modules.keys()) + result = TraceImportResult() + + def debug_import(*args, **kwargs): + frame = sys._getframe(1) + source = frame.f_back.f_code.co_filename if frame.f_back else "<unknown>" + imported_name = args[0] + + if "airflow" in source: + print(f"{source} >>> {imported_name}") + return real_import(*args, **kwargs) + + builtins.__import__ = debug_import + try: + yield result + finally: + builtins.__import__ = real_import + after_modules = set(sys.modules.keys()) + before_count = len(before_modules) + after_count = len(after_modules) + diff_count = after_count - before_count + result.new_modules = after_modules - before_modules + print(f"Modules loaded - before: {before_count}") + print(f"Modules loaded - after: {after_count}") + print(f"Modules loaded - diff: {diff_count}") + + +if __name__ == "__main__": + # Example: + + with trace_imports() as trace_resuslt: + import airflow # noqa # pylint: disable=unused-import Review comment: There are many very complex tools designed for this, but it is a simple tool that is easy to use. I would also like to track only imports that are from Airflow, because I have no influence on those that are in other libraries. This tool was very useful, when worked on https://github.com/apache/airflow/pull/7327 This is not a tool that is now actively used, so I can add it later. For now, I will delete it from this PR. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
