|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import contextlib
|
4 | 5 | import glob
|
5 | 6 | import json
|
@@ -29,37 +30,61 @@ def cwd(path):
|
29 | 30 | os.chdir(curdir)
|
30 | 31 |
|
31 | 32 |
|
32 |
| -with get_new_node('node1') as node: |
33 |
| - node.init() |
34 |
| - node.append_conf('postgresql.conf', conf) |
35 |
| - node.start() |
| 33 | +async def insert_data(con, files, table_name, table_name_c): |
| 34 | + while True: |
| 35 | + try: |
| 36 | + filename = files.pop() |
| 37 | + print(filename) |
| 38 | + except IndexError: |
| 39 | + break |
36 | 40 |
|
37 |
| - node.safe_psql('create extension jsonbd') |
| 41 | + with open(filename, 'r') as f: |
| 42 | + data = json.load(f) |
38 | 43 |
|
39 |
| - for name, root_dir in sources: |
40 |
| - table_name = name |
41 |
| - table_name_c = '%s_c' % name |
| 44 | + if isinstance(data, dict): |
| 45 | + if 'rounds' in data: |
| 46 | + for obj in data['rounds']: |
| 47 | + sql = 'insert into {} values ($1)' |
| 48 | + for i in range(100): |
| 49 | + await con.execute(sql.format(table_name), json.dumps(obj)) |
| 50 | + await con.execute(sql.format(table_name_c), json.dumps(obj)) |
42 | 51 |
|
43 |
| - node.safe_psql('create table %s(a jsonb)' % table_name) |
44 |
| - node.safe_psql('create table %s(a jsonb compression jsonbd)' % table_name_c) |
45 |
| - node.safe_psql('alter table %s alter column a set storage external' % table_name) |
46 | 52 |
|
47 |
| - with node.connect() as con: |
| 53 | +def main(loop): |
| 54 | + with get_new_node('node1') as node: |
| 55 | + node.init() |
| 56 | + node.append_conf('postgresql.conf', conf) |
| 57 | + node.start() |
| 58 | + |
| 59 | + node.safe_psql('postgres', 'create extension jsonbd') |
| 60 | + |
| 61 | + connections = [node.connect() for i in range(3)] |
| 62 | + |
| 63 | + for name, root_dir in sources: |
| 64 | + table_name = name |
| 65 | + table_name_c = '%s_c' % name |
| 66 | + |
| 67 | + node.safe_psql('postgres', 'create table %s(a jsonb)' % table_name) |
| 68 | + node.safe_psql('postgres', 'create table %s(a jsonb compression jsonbd)' % table_name_c) |
| 69 | + node.safe_psql('postgres', 'alter table %s alter column a set storage external' % table_name) |
| 70 | + |
| 71 | + files = [] |
48 | 72 | with cwd(os.path.abspath(root_dir)):
|
49 | 73 | for filename in glob.iglob('**/*.json', recursive=True):
|
50 | 74 | if filename == 'package.json':
|
51 | 75 | continue
|
52 | 76 |
|
53 |
| - with open(filename, 'r') as f: |
54 |
| - data = json.load(f) |
| 77 | + files.append(filename) |
| 78 | + |
| 79 | + coroutines = [insert_data(con, files, table_name, table_name_c) |
| 80 | + for con in connections] |
| 81 | + loop.run_until_complete(asyncio.gather(*coroutines)) |
| 82 | + |
| 83 | + print(node.safe_psql('postgres', "select pg_size_pretty(pg_total_relation_size('%s'))" % table_name)) |
| 84 | + print(node.safe_psql('postgres', "select pg_size_pretty(pg_total_relation_size('%s'))" % table_name_c)) |
55 | 85 |
|
56 |
| - if isinstance(data, dict): |
57 |
| - if 'rounds' in data: |
58 |
| - for obj in data['rounds']: |
59 |
| - sql = 'insert into {} values (%s)' |
60 |
| - for i in range(10): |
61 |
| - con.execute(sql.format(table_name), (json.dumps(obj), )) |
62 |
| - con.execute(sql.format(table_name_c), (json.dumps(obj), )) |
63 | 86 |
|
64 |
| - print(node.safe_psql("select pg_size_pretty(pg_total_relation_size('%s'))" % table_name)) |
65 |
| - print(node.safe_psql("select pg_size_pretty(pg_total_relation_size('%s'))" % table_name_c)) |
| 87 | +if __name__ == '__main__': |
| 88 | + loop = asyncio.get_event_loop() |
| 89 | + main(loop) |
| 90 | + loop.close() |
0 commit comments