-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjames-crash.py
142 lines (111 loc) · 3.86 KB
/
james-crash.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
import psycopg2
import time
import random
#import concurrent.futures
import threading
from datetime import datetime
from configparser import ConfigParser
import os,sys
script_directory = os.path.dirname(os.path.realpath(__file__))
config = ConfigParser()
config.read(script_directory + os.sep + 'config.ini')
conn_info = dict(config.items('betabridge'))
host = conn_info['host']
user = conn_info['user']
database = conn_info['database']
password = conn_info['password']
conn = psycopg2.connect(host=host, dbname=database, port=5432, user=user, password=password)
conn.autocommit = True
cur = conn.cursor()
# Timeout at 9 seconds
cur.execute("SET SESSION statement_timeout = '19000'")
# Construct our table list with tables and their SRIDs
# and make sure they're not empty
tables_with_shape_stmt = """
select table_schema,table_name from information_schema.columns
where column_name = 'shape'
AND (table_schema = 'import' or table_schema = 'viewer')
"""
cur.execute(tables_with_shape_stmt)
results = cur.fetchall()
tables_with_shape = [x[0] + '.' + x[1] for x in results]
tables_and_srids = []
for table in tables_with_shape:
tsplit = table.split('.')
# exclude tables with 'test' in their name
if 'test' in tsplit[1]:
continue
# Find out if it's empty first
stmt=f'SELECT shape FROM {table} where shape is not null LIMIT 1'
cur.execute(stmt)
result = cur.fetchone()
if not result:
continue
# Get the SRID and add to new list as a tuple with table name
stmt = f"SELECT Find_SRID('{tsplit[0]}', '{tsplit[1]}', 'shape')"
cur.execute(stmt)
srid = cur.fetchone()[0]
if int(srid) == 2272 or int(srid) == 4326 or int(srid) == 3857 or int(srid) == 6565:
tables_and_srids.append( (table, int(srid)) )
else:
print(srid)
def wait_until_9th_second():
'''Wait until the 9th second of every 10 seconds'''
while True:
now = datetime.now()
if (now.second % 10) != 0:
x = (now.second % 10) % 9
if x == 0:
print(now.strftime("%H:%M:%S"))
return
else:
time.sleep(0.95)
def every_20th_second():
'''Wait until the 9th second of every 10 seconds'''
while True:
now = datetime.now()
x = (now.second % 20)
if x == 0:
print(now.strftime("%H:%M:%S"))
return
else:
time.sleep(0.95)
def james_query():
try:
# Running queries async on a single connection seems to give us bad results, so make one conn/cur per async run.
async_conn = psycopg2.connect(host=host, dbname=database, port=5432, user=user, password=password)
conn.autocommit = True
async_cur = conn.cursor()
# Timeout at 9 seconds
async_cur.execute("SET SESSION statement_timeout = '9000'")
start = time.time()
async_cur.execute(open("james.sql", "r").read())
results = async_cur.fetchall()
#print(f'Returned results: {len(results)}')
end = time.time() - start
end = '%7f'%(end)
# ljust provides auto indendation
msg = f'duration: {end}, results: {len(results)}'
print(msg)
async_conn.close()
return end
except Exception as e:
async_conn.close()
print(str(e))
return str(e)
if __name__ == '__main__':
amount = int(sys.argv[1])
print(amount)
thread_dict = {}
loop_start = time.time()
for i in range(1,amount+1):
thread_dict[i] = threading.Thread(target=james_query)
#thread_dict[i] = threading.Thread(target=intersect_select, args=(rand_table,srid,))
for key in thread_dict.keys():
thread_dict[key].start()
for key in thread_dict.keys():
thread_dict[key].join()
loop_end = time.time() - loop_start
print(f'Loop duration: {loop_end}')
thread_dict = {}
print('Done.')