betodealmeida commented on a change in pull request #13561: URL: https://github.com/apache/superset/pull/13561#discussion_r596403926
########## File path: scripts/benchmark_migration.py ########## @@ -0,0 +1,127 @@ +# 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 logging +import time +from collections import defaultdict +from pathlib import Path +from typing import Dict, List, Type + +import click +from flask_appbuilder import Model +from flask_migrate import downgrade, upgrade +from sqlalchemy.orm import Session + +from superset import db +from superset.utils.data import add_to_model, find_models, import_migration_script + +logger = logging.getLogger(__name__) + + [email protected]() [email protected]("filepath") [email protected]("--limit", default=1000, help="Maximum number of entities.") [email protected]("--force", is_flag=True, help="Do not prompt for confirmation.") +def main(filepath: str, limit: int = 1000, force: bool = False) -> None: + session = db.session() + + print(f"Importing migration script: {filepath}") + module = import_migration_script(Path(filepath)) + + revision: str = getattr(module, "revision", "") + down_revision: str = getattr(module, "down_revision", "") + if not revision or not down_revision: + raise Exception( + "Not a valid migration script, couldn't find down_revision/revision" + ) + + print(f"Migration goes from {down_revision} to {revision}") + current_revision = db.engine.execute( + "SELECT version_num FROM alembic_version" + ).scalar() + print(f"Current version of the DB is {current_revision}") + + print("\nIdentifying models used in the migration:") + models = find_models(module) + model_rows: Dict[Type[Model], int] = {} + for model in models: + rows = session.query(model).count() + print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})") + model_rows[model] = rows + + if not force: + click.confirm( + f"\nDowngrade DB to {down_revision} and start benchmark?", abort=True + ) + downgrade(revision=down_revision) + + print("Benchmarking migration") + results: Dict[str, float] = {} + start = time.time() + upgrade(revision=revision) + duration = time.time() - start + results["Current"] = duration + print(f"Migration on current DB took: {duration:.2f} seconds") + + min_entities = 10 + new_models: Dict[Type[Model], List[Model]] = defaultdict(list) + while min_entities <= limit: + downgrade(revision=down_revision) + print(f"Running with at least {min_entities} entities of each model") Review comment: For a minimum, since we don't delete existing data to ensure an exact number. ---------------------------------------------------------------- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
