You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/03/09 10:35:01 UTC

[GitHub] [airflow] ashb commented on a change in pull request #7650: [AIRFLOW-7008][WIP] Add perf kit with common used decorators/contexts

ashb 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_r389578111
 
 

 ##########
 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:
   Python has most of this built in already - https://docs.python.org/3/using/cmdline.html#id5 
   
   > `-X importtime` to show how long each import takes. It shows module name, cumulative time (including nested imports) and self time (excluding nested imports). Note that its output may be broken in multi-threaded application. Typical usage is `python3 -X importtime -c 'import asyncio'`. See also [`PYTHONPROFILEIMPORTTIME`](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPROFILEIMPORTTIME).
   
   And can be visualized easily with https://github.com/nschloe/tuna
   
   ![](https://camo.githubusercontent.com/ed7993003ee9e2ce952875f2b793d5129098aa30/68747470733a2f2f6e7363686c6f652e6769746875622e696f2f74756e612f73637265656e636173742e676966)
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services