nuclearpinguin commented on a change in pull request #7370: [AIRFLOW-6590] Use 
batch db operations in jobs
URL: https://github.com/apache/airflow/pull/7370#discussion_r377710664
 
 

 ##########
 File path: scripts/perf/sql_queries.py
 ##########
 @@ -0,0 +1,178 @@
+# 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 time import sleep, time
+from typing import List, NamedTuple, Optional, Tuple
+
+import pandas as pd
+
+# Setup environment before any Airflow import
+DAG_FOLDER = "/opt/airflow/scripts/perf/dags"
+os.environ["AIRFLOW__CORE__DAGS_FOLDER"] = DAG_FOLDER
+os.environ["AIRFLOW__DEBUG__SQLALCHEMY_STATS"] = "True"
+os.environ["AIRFLOW__CORE__LOAD_EXAMPLES"] = "False"
+
+# Here we setup simpler logger to avoid any code changes in
+# Airflow core code base
+LOG_LEVEL = "INFO"
+LOG_FILE = "/files/sql_stats.log"  # Default to run in Breeze
+
+os.environ[
+    "AIRFLOW__LOGGING__LOGGING_CONFIG_CLASS"
+] = "scripts.perf.sql_queries.DEBUG_LOGGING_CONFIG"
+
+DEBUG_LOGGING_CONFIG = {
+    "version": 1,
+    "disable_existing_loggers": False,
+    "formatters": {"airflow": {"format": "%(message)s"}},
+    "handlers": {
+        "console": {"class": "logging.StreamHandler"},
+        "task": {
+            "class": "logging.FileHandler",
+            "formatter": "airflow",
+            "filename": LOG_FILE,
+        },
+        "processor": {
+            "class": "logging.FileHandler",
+            "formatter": "airflow",
+            "filename": LOG_FILE,
+        },
+    },
+    "loggers": {
+        "airflow.processor": {
+            "handlers": ["processor"],
+            "level": LOG_LEVEL,
+            "propagate": False,
+        },
+        "airflow.task": {"handlers": ["task"], "level": LOG_LEVEL, 
"propagate": False},
+        "flask_appbuilder": {
+            "handler": ["console"],
+            "level": LOG_LEVEL,
+            "propagate": True,
+        },
+    },
+    "root": {"handlers": ["console", "task"], "level": LOG_LEVEL},
+}
+
+
+class Query(NamedTuple):
+    function: str
+    file: str
+    location: int
+    sql: str
+    stack: str
+    time: float
+
+    def __str__(self):
+        sql = self.sql if len(self.sql) < 110 else f"{self.sql[:111]}..."
+        return f"{self.function} in {self.file}:{self.location}: {sql}"
+
+    def __eq__(self, other):
+        return (
+            self.function == other.function
+            and self.sql == other.sql
+            and self.location == other.location
+            and self.file == other.file
+        )
+
+    def to_dict(self):
+        return dict(zip(("function", "file", "location", "sql", "stack", 
"time"), self))
+
+
+def reset_db():
+    from airflow.utils.db import resetdb
+
+    resetdb()
+
+
+def run_scheduler_job(with_db_reset=False) -> None:
+    from airflow.jobs.scheduler_job import SchedulerJob
+
+    if with_db_reset:
+        reset_db()
+    SchedulerJob(subdir=DAG_FOLDER, do_pickle=False, num_runs=3).run()
+
+
+def is_query(line: str) -> bool:
+    return "@SQLALCHEMY" in line and "|$" in line
 
 Review comment:
   Is it necessary? I am unable to find any reason why this can be helpful :)

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


With regards,
Apache Git Services

Reply via email to