mik-laj commented on a change in pull request #7650: [AIRFLOW-7008] Add perf 
kit with common used decorators/contexts
URL: https://github.com/apache/airflow/pull/7650#discussion_r408743446
 
 

 ##########
 File path: scripts/perf/perf_kit/memory.py
 ##########
 @@ -0,0 +1,77 @@
+# 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 os
+from contextlib import contextmanager
+
+import psutil
+
+
+def _get_process_memory():
+    process = psutil.Process(os.getpid())
+    return process.memory_info().rss
+
+
+def _human_readable_size(size, decimal_places=3):
+    for unit in ["B", "KiB", "MiB", "GiB", "TiB"]:
+        if size < 1024.0:
+            break
+        size /= 1024.0
+    return f"{size:.{decimal_places}f}{unit}"
+
+
+class TraceMemoryResult:
+    def __init__(self):
+        self.before = 0
+        self.after = 0
+        self.value = 0
+
+
+@contextmanager
+def trace_memory(human_readable=True):
+    """
+    Calculates the amount of difference in free memory before and after script 
execution.
+
+    In other words, how much data the code snippet has used up memory.
+
+    :param human_readable: If yes, the result will be displayed in human 
readable units.
+        If no, the result will be displayed as bytes.
+    """
+    before = _get_process_memory()
+    result = TraceMemoryResult()
+    try:
+        yield result
 
 Review comment:
   It depends on the specific case you want to measure. Sometimes we need to 
measure the amount of memory used together with garbage, and sometimes without. 
It depends on the intention of the particular case. 

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

Reply via email to