# Path: tests/performance/django_spanner/models.py
# class Author(models.Model):
#     id = models.IntegerField(primary_key=True)
#     first_name = models.CharField(max_length=20)
#     last_name = models.CharField(max_length=20)
#     rating = models.CharField(max_length=50)
# 
# Path: tests/settings.py
# DATABASE_NAME = os.getenv("DJANGO_SPANNER_DB", "django_test_db")
# 
# INSTANCE_ID = os.environ.get(
#     "GOOGLE_CLOUD_TESTS_SPANNER_INSTANCE", "spanner-django-python-systest"
# )
# 
# Path: tests/system/django_spanner/utils.py
# def setup_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if not Config.DATABASE.exists():
#         creation = DatabaseCreation(connection)
#         creation._create_test_db(verbosity=0, autoclobber=False, keepdb=True)
# 
#     # Running migrations on the db.
#     call_command("migrate", interactive=False)
# 
# def setup_instance():
#     if USE_EMULATOR:
#         from google.auth.credentials import AnonymousCredentials
# 
#         Config.CLIENT = Client(
#             project=PROJECT_ID, credentials=AnonymousCredentials()
#         )
#     else:
#         Config.CLIENT = Client()
# 
#     retry = RetryErrors(exceptions.ServiceUnavailable)
# 
#     configs = list(retry(Config.CLIENT.list_instance_configs)())
# 
#     instances = retry(_list_instances)()
#     EXISTING_INSTANCES[:] = instances
# 
#     # Delete test instances that are older than an hour.
#     cutoff = int(time.time()) - 1 * 60 * 60
#     instance_pbs = Config.CLIENT.list_instances(
#         "labels.python-spanner-systests:true"
#     )
#     for instance_pb in instance_pbs:
#         instance = Instance.from_pb(instance_pb, Config.CLIENT)
#         if "created" not in instance.labels:
#             continue
#         create_time = int(instance.labels["created"])
#         if create_time > cutoff:
#             continue
#         if not USE_EMULATOR:
#             # Instance cannot be deleted while backups exist.
#             for backup_pb in instance.list_backups():
#                 backup = Backup.from_pb(backup_pb, instance)
#                 backup.delete()
#         instance.delete()
# 
#     if CREATE_INSTANCE:
#         if not USE_EMULATOR:
#             # Defend against back-end returning configs for regions we aren't
#             # actually allowed to use.
#             configs = [config for config in configs if "-us-" in config.name]
# 
#         if not configs:
#             raise ValueError("List instance configs failed in module set up.")
# 
#         Config.INSTANCE_CONFIG = configs[0]
#         config_name = configs[0].name
#         create_time = str(int(time.time()))
#         labels = {"django-spanner-systests": "true", "created": create_time}
# 
#         Config.INSTANCE = Config.CLIENT.instance(
#             INSTANCE_ID, config_name, labels=labels
#         )
#         if not Config.INSTANCE.exists():
#             created_op = Config.INSTANCE.create()
#             created_op.result(
#                 SPANNER_OPERATION_TIMEOUT_IN_SECONDS
#             )  # block until completion
#         else:
#             Config.INSTANCE.reload()
# 
#     else:
#         Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID)
#         Config.INSTANCE.reload()


import random
import time
import unittest
import pandas as pd
import pytest
from django.db import connection
from google.api_core.exceptions import Aborted
from google.cloud import spanner_dbapi
from google.cloud.spanner_v1 import Client, KeySet
from tests.performance.django_spanner.models import Author
from tests.settings import DATABASE_NAME, INSTANCE_ID
from tests.system.django_spanner.utils import setup_database, setup_instance
            rows = list(
                snapshot.execute_sql("SELECT * FROM Author ORDER BY last_name")
            )
            if len(rows) != 100:
                raise ValueError("Wrong number of rows read")

    def _cleanup(self):
        """Drop the test table."""
        conn = spanner_dbapi.connect(INSTANCE_ID, DATABASE_NAME)
        conn.database.update_ddl(["DROP TABLE Author"])
        conn.close()

    def run(self):
        """Execute every test case."""
        measures = {}
        for method in (
            self.insert_one_row_with_fetch_after,
            self.read_one_row,
            self.insert_many_rows,
            self.select_many_rows,
            self.insert_many_rows_with_mutations,
        ):
            method(measures)
        self._cleanup()
        return measures


@pytest.mark.django_db()
class BenchmarkTest(unittest.TestCase):
    def setUp(self):

# Path: tests/performance/django_spanner/models.py
# class Author(models.Model):
#     id = models.IntegerField(primary_key=True)
#     first_name = models.CharField(max_length=20)
#     last_name = models.CharField(max_length=20)
#     rating = models.CharField(max_length=50)
# 
# Path: tests/settings.py
# DATABASE_NAME = os.getenv("DJANGO_SPANNER_DB", "django_test_db")
# 
# INSTANCE_ID = os.environ.get(
#     "GOOGLE_CLOUD_TESTS_SPANNER_INSTANCE", "spanner-django-python-systest"
# )
# 
# Path: tests/system/django_spanner/utils.py
# def setup_database():
#     Config.DATABASE = Config.INSTANCE.database(DATABASE_NAME)
#     if not Config.DATABASE.exists():
#         creation = DatabaseCreation(connection)
#         creation._create_test_db(verbosity=0, autoclobber=False, keepdb=True)
# 
#     # Running migrations on the db.
#     call_command("migrate", interactive=False)
# 
# def setup_instance():
#     if USE_EMULATOR:
#         from google.auth.credentials import AnonymousCredentials
# 
#         Config.CLIENT = Client(
#             project=PROJECT_ID, credentials=AnonymousCredentials()
#         )
#     else:
#         Config.CLIENT = Client()
# 
#     retry = RetryErrors(exceptions.ServiceUnavailable)
# 
#     configs = list(retry(Config.CLIENT.list_instance_configs)())
# 
#     instances = retry(_list_instances)()
#     EXISTING_INSTANCES[:] = instances
# 
#     # Delete test instances that are older than an hour.
#     cutoff = int(time.time()) - 1 * 60 * 60
#     instance_pbs = Config.CLIENT.list_instances(
#         "labels.python-spanner-systests:true"
#     )
#     for instance_pb in instance_pbs:
#         instance = Instance.from_pb(instance_pb, Config.CLIENT)
#         if "created" not in instance.labels:
#             continue
#         create_time = int(instance.labels["created"])
#         if create_time > cutoff:
#             continue
#         if not USE_EMULATOR:
#             # Instance cannot be deleted while backups exist.
#             for backup_pb in instance.list_backups():
#                 backup = Backup.from_pb(backup_pb, instance)
#                 backup.delete()
#         instance.delete()
# 
#     if CREATE_INSTANCE:
#         if not USE_EMULATOR:
#             # Defend against back-end returning configs for regions we aren't
#             # actually allowed to use.
#             configs = [config for config in configs if "-us-" in config.name]
# 
#         if not configs:
#             raise ValueError("List instance configs failed in module set up.")
# 
#         Config.INSTANCE_CONFIG = configs[0]
#         config_name = configs[0].name
#         create_time = str(int(time.time()))
#         labels = {"django-spanner-systests": "true", "created": create_time}
# 
#         Config.INSTANCE = Config.CLIENT.instance(
#             INSTANCE_ID, config_name, labels=labels
#         )
#         if not Config.INSTANCE.exists():
#             created_op = Config.INSTANCE.create()
#             created_op.result(
#                 SPANNER_OPERATION_TIMEOUT_IN_SECONDS
#             )  # block until completion
#         else:
#             Config.INSTANCE.reload()
# 
#     else:
#         Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID)
#         Config.INSTANCE.reload()


# Path: tests/performance/django_spanner/test_benchmark.py
import random
import time
import unittest
import pandas as pd
import pytest
from django.db import connection
from google.api_core.exceptions import Aborted
from google.cloud import spanner_dbapi
from google.cloud.spanner_v1 import Client, KeySet
from tests.performance.django_spanner.models import Author
from tests.settings import DATABASE_NAME, INSTANCE_ID
from tests.system.django_spanner.utils import setup_database, setup_instance

            rows = list(
                snapshot.execute_sql("SELECT * FROM Author ORDER BY last_name")
            )
            if len(rows) != 100:
                raise ValueError("Wrong number of rows read")

    def _cleanup(self):
        """Drop the test table."""
        conn = spanner_dbapi.connect(INSTANCE_ID, DATABASE_NAME)
        conn.database.update_ddl(["DROP TABLE Author"])
        conn.close()

    def run(self):
        """Execute every test case."""
        measures = {}
        for method in (
            self.insert_one_row_with_fetch_after,
            self.read_one_row,
            self.insert_many_rows,
            self.select_many_rows,
            self.insert_many_rows_with_mutations,
        ):
            method(measures)
        self._cleanup()
        return measures


@pytest.mark.django_db()
class BenchmarkTest(unittest.TestCase):
    def setUp(self):

        setup_instance()