-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_using_tools.py
189 lines (151 loc) · 4.78 KB
/
load_using_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import argparse
from pathlib import Path
import dotenv
from joblib import Parallel, delayed
from timer import Timer
from utils import sqlalchemy_connection_string, run_in_container, num_rows
dotenv.load_dotenv()
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_DB_NAME = os.getenv("POSTGRES_DB_NAME")
CSV_PATH = os.getenv("CSV_PATH")
def parse_args():
parser = argparse.ArgumentParser(
description="Load data into the weather table using external tools."
)
parser.add_argument(
"--method",
choices=["pg_bulkload", "timescaledb_parallel_copy"],
help="How to copy data into the table.",
required=True
)
parser.add_argument(
"--table-type",
choices=["regular", "hyper"],
help="Create a regular PostgreSQL table or a TimescaleDB hypertable.",
required=True
)
parser.add_argument(
"--hours",
type=int,
help="How many hours of ERA5 data to load. Each hour is roughly 1 million rows.",
required=True
)
parser.add_argument(
"--workers",
type=int,
help="Number of parallel workers.",
required=True
)
parser.add_argument(
"--benchmarks-file",
type=str,
help="Filepath to output benchmarks to a CSV file.",
required=True
)
parser.add_argument(
"--parallel-benchmarks-file",
type=str,
help="Filepath to output parallel benchmarks to a CSV file."
)
return parser.parse_args()
def log_benchmark(args, timer, hour):
filepath = args.benchmarks_file
# Create file and write CSV header
if not Path(filepath).exists():
with open(filepath, "a") as file:
file.write(
"method,table_type,workers,hour,num_rows,"
"seconds,rate,units\n"
)
with open(filepath, "a") as file:
file.write(
f"{args.method},{args.table_type},{args.workers},{hour},{num_rows(1)},"
f"{timer.interval},{timer.rate},{timer.units}\n"
)
return
def log_parallel_benchmark(args, timer):
filepath = args.parallel_benchmarks_file
# Create file and write CSV header
if not Path(filepath).exists():
with open(filepath, "a") as file:
file.write(
"method,table_type,workers,hours,num_rows,"
"seconds,rate,units\n"
)
with open(filepath, "a") as file:
file.write(
f"{args.method},{args.table_type},{args.workers},{args.hours},{num_rows(args.hours)},"
f"{timer.interval},{timer.rate},{timer.units}\n"
)
return
def _pg_bulkload(args, n):
timer = Timer(
f"COPYing hour {n} using {args.method} with {args.workers} workers",
n=num_rows(1),
units="inserts"
)
cmd = [
"pg_bulkload",
f"--host={POSTGRES_HOST}",
f"--username={POSTGRES_USER}",
f"--dbname={POSTGRES_DB_NAME}",
f"--input", f"{CSV_PATH}/weather_hour{n}.csv",
"--output", "weather",
"-o", "writer=parallel"
]
with timer:
run_in_container(cmd)
log_benchmark(args, timer, n)
return
def load_data_using_pg_bulkload(args):
run_in_container([
"psql",
"-U", POSTGRES_USER,
"-d", POSTGRES_DB_NAME,
"-c", "CREATE EXTENSION if not exists pg_bulkload;"
])
Parallel(n_jobs=args.workers)(
delayed(_pg_bulkload)(args, n)
for n in range(args.hours)
)
return
def load_data_using_tpc(args):
for n in range(args.hours):
timer = Timer(
f"COPYing hour {n} using {args.method} with {args.workers} workers",
n=num_rows(1),
units="inserts"
)
cmd = [
"timescaledb-parallel-copy",
"--verbose",
"--reporting-period", "10s",
"--connection", sqlalchemy_connection_string(),
"--table", "weather",
"--batch-size", "10000",
"--workers", f"{args.workers}",
"--file", f"{CSV_PATH}/weather_hour{n}.csv"
]
with timer:
run_in_container(cmd)
log_benchmark(args, timer, n)
return
def main(args):
timer = Timer(
f"COPYing {args.hours} hours of data using {args.method} "
f"with {args.workers} workers",
n=num_rows(args.hours),
units="inserts"
)
with timer:
if args.method == "pg_bulkload":
load_data_using_pg_bulkload(args)
elif args.method == "timescaledb_parallel_copy":
load_data_using_tpc(args)
if args.parallel_benchmarks_file:
log_parallel_benchmark(args, timer)
return
if __name__ == "__main__":
main(parse_args())