Skip to content

Commit d31088c

Browse files
committed
Added the warm_up_command parameter so that any command can be executed before the workload is executed.
1 parent 03ac5d6 commit d31088c

File tree

8 files changed

+41
-10
lines changed

8 files changed

+41
-10
lines changed

conf/postgres_opttune.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ required_recovery_time_second = 0
2727
benchmark = my_workload # Benchmark tool name('my_workload' or 'sampled_workload' or 'pgbench' or 'oltpbench' or 'star_schema_benchmark')
2828
parameter_json_dir = ./conf/
2929
number_trail = 100 # Number of benchmarks to run for turning
30-
data_load_interval = 10 # Specify the data load interval by the number of benchmarks
30+
data_load_interval = 10 # Specify the execution interval of data_load_command by the number of benchmarks
31+
warm_up_interval = 1 # Specify the execution interval of warm_up_command by the number of benchmarks
3132
sample_mode = TPE # Sampling mode(TPE, RandomSampler, SkoptSampler, CmaEsSampler)
3233
debug = False # debug mode
3334
save_study_history = True # Whether to save study history
@@ -38,6 +39,7 @@ history_database_url = sqlite:///study-history.db # Example PostgreSQL. postgr
3839
work_directory = current_directory # Specifies the directory where the workload will run. Example: /opt/test
3940
# The default value of current_directory runs the workload without moving the directory.
4041
data_load_command = /usr/pgsql-12/bin/pgbench -i -s 10 tpcc
42+
warm_up_command = psql -c "SELECT 'warm up!'"
4143
run_workload_command = /usr/pgsql-12/bin/pgbench tpcc -T 1200
4244

4345
[sampled-workload]

pgopttune/config/my_workload_config.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def work_directory(self):
1414
def data_load_command(self):
1515
return self.get_parameter_value('data_load_command')
1616

17+
@property
18+
def warm_up_command(self):
19+
return self.get_parameter_value('warm_up_command')
20+
1721
@property
1822
def run_workload_command(self):
1923
return self.get_parameter_value('run_workload_command')

pgopttune/config/tune_config.py

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def number_trail(self):
3030
def data_load_interval(self):
3131
return int(self.get_parameter_value('data_load_interval'))
3232

33+
@property
34+
def warm_up_interval(self):
35+
return int(self.get_parameter_value('warm_up_interval'))
36+
3337
@property
3438
def sample_mode(self):
3539
return self.get_parameter_value('sample_mode')

pgopttune/objective/objective.py

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __init__(self,
1414
self.params = PostgresTuneParameter(postgres_server_config, # turning parameter
1515
params_json_dir=tune_config.parameter_json_dir)
1616
self.data_load_interval = tune_config.data_load_interval
17+
self.warm_up_interval = tune_config.warm_up_interval
1718
self.workload = None
1819

1920
def __call__(self, trial):
@@ -39,6 +40,8 @@ def run_workload(self, trial):
3940
self.workload.prepare_workload_database()
4041
self.workload.vacuum_database() # vacuum analyze
4142
self.params.reset_database() # cache free and database restart
43+
if (int(trial.number) == 0) or (int(trial.number) % self.warm_up_interval == 0):
44+
self.workload.warm_up() # run warm_up_command
4245
objective_value = self.workload.run() # benchmark run
4346
return objective_value
4447

pgopttune/parameter/pg_parameter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _reload_conf(self):
109109

110110
def _free_cache(self):
111111
free_cache_cmd = 'sudo bash -c "sync"; sudo bash -c "echo 1 > /proc/sys/vm/drop_caches"'
112-
112+
logger.debug("Free pagecache.")
113113
# localhost PostgreSQL
114114
if self.postgres_server_config.host == '127.0.0.1' or self.postgres_server_config.host == 'localhost':
115115
run_command(free_cache_cmd)

pgopttune/workload/my_workload.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,25 @@ def __init__(self, postgres_server_config: PostgresServerConfig, my_workload_con
2323

2424
def data_load(self):
2525
cwd = os.getcwd()
26-
if self.my_workload_config.work_directory != "current_directory":
27-
os.chdir(self.my_workload_config.work_directory)
26+
self._change_work_directory()
2827
data_load_cmd = self.my_workload_config.data_load_command
29-
logger.debug('run my workload data load command : {}'.format(data_load_cmd))
28+
logger.debug('Run data_load_command : {}'.format(data_load_cmd))
3029
run_command(data_load_cmd)
3130
if self.my_workload_config.work_directory != "current_directory":
3231
os.chdir(cwd)
3332

34-
def run(self, measurement_time_second: int = None):
33+
def warm_up(self):
3534
cwd = os.getcwd()
35+
self._change_work_directory()
36+
warm_up_command = self.my_workload_config.warm_up_command
37+
logger.debug('Run warm_up_command : {}'.format(warm_up_command))
38+
run_command(warm_up_command)
3639
if self.my_workload_config.work_directory != "current_directory":
37-
os.chdir(self.my_workload_config.work_directory)
40+
os.chdir(cwd)
41+
42+
def run(self, measurement_time_second: int = None):
43+
cwd = os.getcwd()
44+
self._change_work_directory()
3845
run_workload_command = self.my_workload_config.run_workload_command
3946
start_number_of_xact_commit = self.get_number_of_xact_commit()
4047
workload_start_time = time.time() # start measurement time
@@ -59,6 +66,10 @@ def run(self, measurement_time_second: int = None):
5966
tps = self.calculate_transaction_per_second(workload_number_of_xact_commit, self.workload_elapsed_time)
6067
return tps
6168

69+
def _change_work_directory(self):
70+
if self.my_workload_config.work_directory != "current_directory":
71+
os.chdir(self.my_workload_config.work_directory)
72+
6273
@staticmethod
6374
def calculate_transaction_per_second(number_of_xact_commit, elapsed_seconds):
6475
return round(number_of_xact_commit / elapsed_seconds, 6)

pgopttune/workload/sampled_workload.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,14 @@ def load_sampled_workload(cls, load_file_path, postgres_server_config: PostgresS
109109
workload.postgres_server_config = postgres_server_config
110110
return workload
111111

112-
@staticmethod
113-
def data_load():
112+
def data_load(self):
114113
# TODO:
115114
logger.warning("At the moment, in the sampled workload, The data reload function is not implemented.")
116115

116+
def warm_up(self):
117+
# TODO:
118+
logger.warning("At the moment, in the sampled workload, The warm up function is not implemented.")
119+
117120
def _run_transaction(self, transaction_index=0):
118121
# logger.debug("Transaction's statement : {}".format(self.my_transactions[transaction_index].statement))
119122
transaction_elapsed_time = self.my_transactions[transaction_index].run(self.postgres_server_config)

pgopttune/workload/workload.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ def prepare_workload_database(self):
2626
def data_load(self):
2727
raise NotImplementedError("subclasses of Workload must provide a data_load() method.")
2828

29+
def warm_up(self):
30+
raise NotImplementedError("subclasses of Workload must provide a warm_up() method.")
31+
2932
def run(self, measurement_time_second: int = None):
3033
raise NotImplementedError("subclasses of Workload must provide a run() method.")
3134

3235
def vacuum_database(self):
3336
"""
3437
run vacuum analyze
3538
"""
39+
logger.debug("Run VACUUM ANALYZE.")
3640
vacuum_analyze_sql = "VACUUM ANALYZE"
3741
with get_pg_connection(dsn=self.postgres_server_config.dsn) as conn:
3842
conn.set_session(autocommit=True)
@@ -116,4 +120,4 @@ def _get_backup_database_dsn(self):
116120
self.postgres_server_config.password,
117121
self.postgres_server_config.host,
118122
self.postgres_server_config.port,
119-
self._get_backup_database_name())
123+
self._get_backup_database_name())

0 commit comments

Comments
 (0)