Skip to content

Commit 30dad42

Browse files
committed
Fixed so that the workload to be tuned can be specified by the following two parameters.
1. data_load_command parameter 2. run_workload_command parameter
1 parent 73df309 commit 30dad42

9 files changed

+121
-20
lines changed

conf/postgres_opttune.conf

+20-14
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ ssh_port = 22 # ssh port
1919
ssh_password = postgres # pg_os_user's ssh password
2020

2121
[turning]
22-
study_name = pgbench_study # study name
22+
study_name = my_workload_study # study name
2323
required_recovery_time_second = 0
2424
# The maximum recovery time allowed by the user in case of a PostgreSQL crash,
2525
# which is used to estimate the wax_wal_size parameter.
2626
# Note: The default value of 0 does not perform the estimation of the wax_wal_size parameter.
27-
benchmark = pgbench # Benchmark tool name('sampled_workload' or pgbench' or 'oltpbench' or 'star_schema_benchmark')
27+
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
3030
data_load_interval = 10 # Specify the data load interval by the number of benchmarks
@@ -34,10 +34,26 @@ save_study_history = True # Whether to save study history
3434
load_study_history = True # Whether to load study history if a study name already exists.
3535
history_database_url = sqlite:///study-history.db # Example PostgreSQL. postgresql://postgres@localhost/study_history
3636

37-
[sampled_workload]
38-
sampled_workload_save_file = workload_data/2020-09-13_202209.011708-2020-09-13_202239.011973.pkl
37+
[my-workload]
38+
data_load_command = /usr/pgsql-12/bin/pgbench -i -s 10 tpcc
39+
run_workload_command = /usr/pgsql-12/bin/pgbench tpcc -T 1200
40+
41+
[sampled-workload]
3942
# File saved using workload_sampler.py
43+
sampled_workload_save_file = workload_data/2020-09-13_202209.011708-2020-09-13_202239.011973.pkl
44+
45+
[workload-sampling]
46+
workload_sampling_time_second = 30
47+
# Time (in seconds) to sample the workload running on the database in the [PostgreSQL] section
48+
my_workload_save_dir = ./workload_data/ # workload save directory
49+
# Database settings to temporarily store workload information
50+
pghost = localhost # PostgreSQL server host
51+
pgport = 5432 # PostgreSQL server port
52+
pguser = postgres # PostgreSQL user name(Database user)
53+
pgpassword = postgres12 # PostgreSQL user password(Database user)
54+
pgdatabase = sampling # PostgreSQL Database
4055

56+
##### Experimental settings
4157
[pgbench]
4258
scale_factor = 10 # pgbench scale factor
4359
clients = 10 # Number of clients
@@ -60,13 +76,3 @@ sql_key = Q1.1, Q2.1, Q3.1
6076
# Please specify the name of the file in pgopttune/workload/star_schema_sql/ directory(sql_file_path parameter diretory)
6177
# (e.g., Q1.1,Q2,1).
6278

63-
[workload-sampling]
64-
workload_sampling_time_second = 30
65-
# Time (in seconds) to sample the workload running on the database in the [PostgreSQL] section
66-
my_workload_save_dir = ./workload_data/ # workload save directory
67-
# Database settings to temporarily store workload information
68-
pghost = localhost # PostgreSQL server host
69-
pgport = 5432 # PostgreSQL server port
70-
pguser = postgres # PostgreSQL user name(Database user)
71-
pgpassword = postgres12 # PostgreSQL user password(Database user)
72-
pgdatabase = sampling # PostgreSQL Database
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from pgopttune.config.config import Config
2+
3+
4+
class MyWorkloadConfig(Config):
5+
def __init__(self, conf_path, section='my-workload'):
6+
super().__init__(conf_path)
7+
self.config_dict = dict(self.config.items(section))
8+
9+
@property
10+
def data_load_command(self):
11+
return self.get_parameter_value('data_load_command')
12+
13+
@property
14+
def run_workload_command(self):
15+
return self.get_parameter_value('run_workload_command')

pgopttune/config/sampled_workload_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
class SampledWorkloadConfig(Config):
6-
def __init__(self, conf_path, section='sampled_workload'):
6+
def __init__(self, conf_path, section='sampled-workload'):
77
super().__init__(conf_path)
88
self.conf_path = conf_path
99
self.config_dict = dict(self.config.items(section))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from logging import getLogger
2+
from pgopttune.workload.my_workload import MyWorkLoad
3+
from pgopttune.objective.objective import Objective
4+
from pgopttune.config.postgres_server_config import PostgresServerConfig
5+
from pgopttune.config.tune_config import TuneConfig
6+
from pgopttune.config.my_workload_config import MyWorkloadConfig
7+
8+
logger = getLogger(__name__)
9+
10+
11+
class ObjectiveMyWorkload(Objective):
12+
13+
def __init__(self,
14+
postgres_server_config: PostgresServerConfig,
15+
tune_config: TuneConfig,
16+
my_workload_config: MyWorkloadConfig):
17+
super().__init__(postgres_server_config, tune_config)
18+
self.workload = MyWorkLoad(postgres_server_config, my_workload_config)

pgopttune/objective/objective_sampled_workload.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ObjectiveSampledWorkload(Objective):
1313
def __init__(self,
1414
postgres_server_config: PostgresServerConfig,
1515
tune_config: TuneConfig,
16-
my_workload_config: SampledWorkloadConfig):
16+
sampled_workload_config: SampledWorkloadConfig):
1717
super().__init__(postgres_server_config, tune_config)
18-
self.workload = SampledWorkload.load_sampled_workload(my_workload_config.my_workload_save_file,
18+
self.workload = SampledWorkload.load_sampled_workload(sampled_workload_config.my_workload_save_file,
1919
postgres_server_config=postgres_server_config)

pgopttune/workload/my_workload.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import time
3+
from logging import getLogger
4+
from .workload import Workload
5+
from pgopttune.utils.command import run_command
6+
from pgopttune.config.postgres_server_config import PostgresServerConfig
7+
from pgopttune.config.my_workload_config import MyWorkloadConfig
8+
9+
logger = getLogger(__name__)
10+
11+
12+
class MyWorkLoad(Workload):
13+
def __init__(self, postgres_server_config: PostgresServerConfig, my_workload_config: MyWorkloadConfig):
14+
super().__init__(postgres_server_config)
15+
self.my_workload_config = my_workload_config
16+
os.environ['PGHOST'] = postgres_server_config.host
17+
os.environ['PGPORT'] = postgres_server_config.port
18+
os.environ['PGDATABASE'] = postgres_server_config.database
19+
os.environ['PGUSER'] = postgres_server_config.user
20+
os.environ['PGPASSWORD'] = postgres_server_config.password
21+
22+
def data_load(self):
23+
data_load_cmd = self.my_workload_config.data_load_command
24+
logger.debug('run my workload data load command : {}'.format(data_load_cmd))
25+
run_command(data_load_cmd)
26+
self.vacuum_database() # vacuum analyze
27+
28+
def run(self):
29+
run_workload_command = self.my_workload_config.run_workload_command
30+
start_number_of_xact_commit = self.get_number_of_xact_commit()
31+
workload_start_time = time.time() # start measurement time
32+
run_command(run_workload_command) # run workload
33+
# command_result = run_command(run_workload_command) # run workload
34+
# logger.info(command_result.stdout.decode("utf8"))
35+
workload_elapsed_times = time.time() - workload_start_time
36+
# logger.info(workload_elapsed_times)
37+
time.sleep(1) # default PGSTAT_STAT_INTERVAL(500ms)
38+
workload_number_of_xact_commit = self.get_number_of_xact_commit() - start_number_of_xact_commit
39+
# logger.info(workload_number_of_xact_commit)
40+
tps = self.calculate_transaction_per_second(workload_number_of_xact_commit, workload_elapsed_times)
41+
# logger.info("tps : {}".format(tps))
42+
return tps
43+
44+
@staticmethod
45+
def calculate_transaction_per_second(number_of_xact_commit, elapsed_seconds):
46+
return round(number_of_xact_commit / elapsed_seconds, 6)

pgopttune/workload/workload.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from logging import getLogger
2+
from psycopg2.extras import DictCursor
23
from pgopttune.utils.pg_connect import get_pg_connection
34
from pgopttune.config.postgres_server_config import PostgresServerConfig
45

@@ -26,3 +27,12 @@ def execute_sql_file(self, sql_filepath):
2627
with conn.cursor() as cur:
2728
cur.execute(open(sql_filepath, "r").read())
2829
logger.debug("finish execute {}".format(sql_filepath))
30+
31+
def get_number_of_xact_commit(self):
32+
get_number_of_xact_commit_sql = "SELECT xact_commit FROM pg_stat_database WHERE datname = %s"
33+
with get_pg_connection(dsn=self.postgres_server_config.dsn) as conn:
34+
conn.set_session(autocommit=True)
35+
with conn.cursor(cursor_factory=DictCursor) as cur:
36+
cur.execute(get_number_of_xact_commit_sql, (self.postgres_server_config.database,))
37+
xact_commit = cur.fetchone()["xact_commit"]
38+
return xact_commit

sampling_workload.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def main(
3030
"[turning]\n"
3131
"benchmark = sampled_workload \n"
3232
":\n"
33-
"[sampled_workload]\n"
33+
"[sampled-workload]\n"
3434
"sampled_workload_save_file = {}".format(workload_save_file_path))
3535

3636

tune.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pgopttune.utils.logger import logging_dict
88
from pgopttune.sampler.sampler import get_sampler
99
from pgopttune.study.study import create_study
10+
from pgopttune.objective.objective_my_workload import ObjectiveMyWorkload
1011
from pgopttune.objective.objective_pgbench import ObjectivePgbench
1112
from pgopttune.objective.objective_oltpbench import ObjectiveOltpbench
1213
from pgopttune.objective.objective_star_schema_benchmark import ObjectiveStarSchemaBenchmark
@@ -16,6 +17,7 @@
1617
from pgopttune.recovery.pg_recovery import Recovery
1718
from pgopttune.config.postgres_server_config import PostgresServerConfig
1819
from pgopttune.config.tune_config import TuneConfig
20+
from pgopttune.config.my_workload_config import MyWorkloadConfig
1921
from pgopttune.config.pgbench_config import PgbenchConfig
2022
from pgopttune.config.oltpbench_config import OltpbenchConfig
2123
from pgopttune.config.star_schema_benchmark_config import StarSchemaBenchmarkConfig
@@ -36,8 +38,12 @@ def main(
3638
optuna.logging.disable_default_handler() # Stop showing logs in sys.stderr.
3739

3840
# set objective
41+
# my workload
42+
if tune_config.benchmark == 'my_workload':
43+
my_workload_config = MyWorkloadConfig(conf_path) # pgbench config
44+
objective = ObjectiveMyWorkload(postgres_server_config, tune_config, my_workload_config)
3945
# pgbench
40-
if tune_config.benchmark == 'pgbench':
46+
elif tune_config.benchmark == 'pgbench':
4147
pgbench_config = PgbenchConfig(conf_path) # pgbench config
4248
objective = ObjectivePgbench(postgres_server_config, tune_config, pgbench_config)
4349
# oltpbench
@@ -48,7 +54,7 @@ def main(
4854
elif tune_config.benchmark == 'star_schema_benchmark':
4955
star_schema_benchmark_config = StarSchemaBenchmarkConfig(conf_path) # star schema benchmark config
5056
objective = ObjectiveStarSchemaBenchmark(postgres_server_config, tune_config, star_schema_benchmark_config)
51-
# my workload (save using sampling_workload.py)
57+
# sampled workload (save using sampling_workload.py)
5258
elif tune_config.benchmark == 'sampled_workload':
5359
sampled_workload_config = SampledWorkloadConfig(conf_path) # my workload sampled config
5460
objective = ObjectiveSampledWorkload(postgres_server_config, tune_config, sampled_workload_config)

0 commit comments

Comments
 (0)