Skip to content

Commit d042f92

Browse files
committed
Fixed data load per trial.
1 parent 94f8ad6 commit d042f92

File tree

6 files changed

+37
-58
lines changed

6 files changed

+37
-58
lines changed

pgopttune/objective/objective.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ def change_tune_param(self, trial):
3535

3636
def run_workload(self, trial):
3737
if (int(trial.number) == 0) or (int(trial.number) % self.data_load_interval == 0):
38-
self.workload.data_load() # data load
38+
if self.workload.check_exist_backup_database():
39+
# Recreate the database using the backed up database as a template
40+
self.workload.drop_database()
41+
self.workload.create_database_use_backup_database()
42+
else:
43+
self.workload.data_load() # data load
44+
self.workload.create_backup_database() # backup database
45+
self.workload.vacuum_database() # vacuum analyze
3946
self.params.reset_database() # cache free and database restart
4047
objective_value = self.workload.run() # benchmark run
4148
return objective_value

pgopttune/workload/my_workload.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,9 @@ def __init__(self, postgres_server_config: PostgresServerConfig, my_workload_con
2121
os.environ['PGPASSWORD'] = postgres_server_config.password
2222

2323
def data_load(self):
24-
if self._check_exist_backup_database():
25-
# Recreate the database using the backed up database as a template
26-
self._drop_database()
27-
self._create_database_use_backup_database()
28-
else:
29-
data_load_cmd = self.my_workload_config.data_load_command
30-
logger.debug('run my workload data load command : {}'.format(data_load_cmd))
31-
run_command(data_load_cmd)
32-
self._create_backup_database() # backup database
33-
self.vacuum_database() # vacuum analyze
24+
data_load_cmd = self.my_workload_config.data_load_command
25+
logger.debug('run my workload data load command : {}'.format(data_load_cmd))
26+
run_command(data_load_cmd)
3427

3528
def run(self):
3629
run_workload_command = self.my_workload_config.run_workload_command

pgopttune/workload/oltpbench.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,16 @@ def __init__(self, postgres_server_config: PostgresServerConfig, oltpbench_confi
2121
self.config_hash = get_file_hash(oltpbench_config.oltpbench_config_path)
2222

2323
def data_load(self):
24-
if self._check_exist_backup_database():
25-
# Recreate the database using the backed up database as a template
26-
self._drop_database()
27-
self._create_database_use_backup_database()
28-
else: # First data load
29-
cwd = os.getcwd()
30-
config_path = os.path.join(cwd, self.oltpbench_config.oltpbench_config_path)
31-
data_load_cmd = "{}/oltpbenchmark -b {} -c {} --create=true --load=true".format(
32-
self.oltpbench_config.oltpbench_path,
33-
self.oltpbench_config.benchmark_kind,
34-
config_path)
35-
os.chdir(self.oltpbench_config.oltpbench_path)
36-
logger.debug('run oltpbench data load command : {}'.format(data_load_cmd))
37-
run_command(data_load_cmd)
38-
os.chdir(cwd)
39-
self._create_backup_database() # backup database
40-
self.vacuum_database() # vacuum analyze
24+
cwd = os.getcwd()
25+
config_path = os.path.join(cwd, self.oltpbench_config.oltpbench_config_path)
26+
data_load_cmd = "{}/oltpbenchmark -b {} -c {} --create=true --load=true".format(
27+
self.oltpbench_config.oltpbench_path,
28+
self.oltpbench_config.benchmark_kind,
29+
config_path)
30+
os.chdir(self.oltpbench_config.oltpbench_path)
31+
logger.debug('run oltpbench data load command : {}'.format(data_load_cmd))
32+
run_command(data_load_cmd)
33+
os.chdir(cwd)
4134

4235
def run(self, measurement_time_second: int = None):
4336
grep_string = "requests\/sec"

pgopttune/workload/pgbench.py

+8-15
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,14 @@ def __init__(self, postgres_server_config: PostgresServerConfig, pgbench_config:
1919
os.environ['PGPASSWORD'] = postgres_server_config.password
2020

2121
def data_load(self):
22-
if self._check_exist_backup_database():
23-
# Recreate the database using the backed up database as a template
24-
self._drop_database()
25-
self._create_database_use_backup_database()
26-
else:
27-
data_load_cmd = "{}/pgbench -h {} -p {} -U {} {} -i -s {}".format(self.postgres_server_config.pgbin,
28-
self.postgres_server_config.host,
29-
self.postgres_server_config.port,
30-
self.postgres_server_config.user,
31-
self.postgres_server_config.database,
32-
self.pgbench_config.scale_factor)
33-
logger.debug('run pgbench data load command : {}'.format(data_load_cmd))
34-
run_command(data_load_cmd)
35-
self._create_backup_database() # backup database
36-
self.vacuum_database() # vacuum analyze
22+
data_load_cmd = "{}/pgbench -h {} -p {} -U {} {} -i -s {}".format(self.postgres_server_config.pgbin,
23+
self.postgres_server_config.host,
24+
self.postgres_server_config.port,
25+
self.postgres_server_config.user,
26+
self.postgres_server_config.database,
27+
self.pgbench_config.scale_factor)
28+
logger.debug('run pgbench data load command : {}'.format(data_load_cmd))
29+
run_command(data_load_cmd)
3730

3831
def run(self, measurement_time_second: int = None):
3932
grep_string = "excluding"

pgopttune/workload/star_schema_benchmark.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@ def __init__(self, postgres_server_config: PostgresServerConfig, ssb_config: Sta
1818
self.backup_database_prefix = 'ssb_backup_'
1919

2020
def data_load(self):
21-
if self._check_exist_backup_database():
22-
# Recreate the database using the backed up database as a template
23-
self._drop_database()
24-
self._create_database_use_backup_database()
25-
else:
26-
self._create_table() # create table
27-
self._truncate_table() # truncate table
28-
self._data_file_generate() # create data file
29-
self._load_data() # data load
30-
self._create_backup_database() # backup database
31-
self.vacuum_database() # vacuum database
21+
self._create_table() # create table
22+
self._truncate_table() # truncate table
23+
self._data_file_generate() # create data file
24+
self._load_data() # data load
3225

3326
def _create_table(self):
3427
for table_sql_filepath in self.ssb_config.table_sql_filepath_list:

pgopttune/workload/workload.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_number_of_xact_commit(self):
4141
xact_commit = cur.fetchone()["xact_commit"]
4242
return xact_commit
4343

44-
def _check_exist_backup_database(self):
44+
def check_exist_backup_database(self):
4545
check_exist_backup_database_sql = "SELECT count(datname) FROM pg_database WHERE datname = %s"
4646
with get_pg_connection(dsn=self.postgres_server_config.dsn) as conn:
4747
conn.set_session(autocommit=True)
@@ -57,7 +57,7 @@ def _check_exist_backup_database(self):
5757
return False
5858

5959
@retry(stop_max_attempt_number=5, wait_fixed=10000)
60-
def _create_backup_database(self):
60+
def create_backup_database(self):
6161
logger.debug(
6262
"Start backing up the database. Database : {} ".format(self.postgres_server_config.database))
6363

@@ -74,7 +74,7 @@ def _get_backup_database_name(self):
7474
return self.backup_database_prefix + self.config_hash
7575

7676
@retry(stop_max_attempt_number=5, wait_fixed=10000)
77-
def _drop_database(self):
77+
def drop_database(self):
7878
drop_database_sql = "DROP DATABASE {} ".format(self.postgres_server_config.database)
7979
backup_database_dsn = self._get_backup_database_dsn()
8080
with get_pg_connection(dsn=backup_database_dsn) as conn:
@@ -84,7 +84,7 @@ def _drop_database(self):
8484
logger.debug("The database has been deleted. Database : {}".format(self.postgres_server_config.database))
8585

8686
@retry(stop_max_attempt_number=5, wait_fixed=10000)
87-
def _create_database_use_backup_database(self):
87+
def create_database_use_backup_database(self):
8888
create_database_use_backup_sql = "CREATE DATABASE {} TEMPLATE {}".format(self.postgres_server_config.database,
8989
self._get_backup_database_name())
9090
backup_database_dsn = self._get_backup_database_dsn()

0 commit comments

Comments
 (0)