-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.py
250 lines (216 loc) · 7.61 KB
/
work.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
import datetime
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
import csv
import urllib
import json
import itertools
# <<CONFIG POINT 1>>
# Count of queries
COUNT = 10_000
# Do the test with only non recursive queries
DO_NORMAL = True
# Do the test with only recursive queries
DO_RECURSIVE = True
exclude = []
exclude.append("http://www.bigdata.com/")
exclude.append("http://www.bigdata.com/")
print("read tsv file...")
# total number of queries
sample_r = []
count = 0
with open("wdlogsh.tsv") as file:
tsv_file = csv.reader(file, delimiter="\t")
for line in tsv_file:
count = count + 1
# decode the query and remove header, a simple pattern is applied to filter path queries
if count > 1:
query = (
urllib.parse.unquote_plus(line[0])
.replace("> *", ">*")
.replace("> / <", ">/<")
)
sample_r.append(query)
print("sample size:", len(sample_r))
def filter(x: str):
# Remove MINUS queries, unsupported by qEndpoint
return "http://www.bigdata.com" in x or "MINUS" in x
sample_no_minus = itertools.filterfalse(filter, sample_r)
sample = [x for x in sample_no_minus]
def filter(x: str):
# Filter path queries
return (">*" in x or ">+" in x or " | " in x or ">?" in x or " / " in x or "^(" in x) and ("REGEX" not in x)
# Get recursive (path) queries
rec_queries_startt = set(i for (i, e) in enumerate(sample) if filter(e))
rec_queries_all = [e for e in sample if filter(e)]
rec_queries_5k = rec_queries_all[:5000]
print(len(rec_queries_5k))
class ComputedInfo:
def __init__(self, id: str, host1: str, name: str):
self.id = id
self.name = name
self.time1 = []
self.number_result1 = []
self.error1 = []
self.sparql1 = SPARQLWrapper(
host1,
agent="D063520 (https://the-qa-company.com/; dennis.diefenbach@the-qa-company.com)",
)
self.sparql1.setReturnFormat(JSON)
if not (DO_NORMAL or DO_RECURSIVE):
print("no DO_ set", f=sys.stderr)
exit - 1
# <<CONFIG POINT 2>>
# the list of SPARQL endpoints
# (query url, id, name)
sparlq = [ComputedInfo(id, url, name) for url, id, name in [
("https://qlever.cs.uni-freiburg.de/api/wikidata", "ql", "QLever"),
("https://wikidata.demo.openlinksw.com/sparql", "vs", "Virtuoso"),
("http://query.wikidata.org/sparql", "wd", "Blazegraph"),
("http://51.158.120.80:1234/api/endpoint/sparql", "qe", "qEndpoint"),
]]
if DO_NORMAL:
count = 8000
for query in sample[8000:]:
# print(query.replace('\n', ' '))
if count >= COUNT:
break
for i in range(len(sparlq)):
sp = sparlq[i]
try:
print(f"{count}", sp.name + "...",
end="", file=sys.stderr, flush=True)
before = datetime.datetime.now()
sp.sparql1.setQuery(query + "")
ret = sp.sparql1.queryAndConvert()
delta = datetime.datetime.now() - before
microseconds = delta.total_seconds() * 1000
sp.time1.append(microseconds)
if "boolean" in ret:
if ret["boolean"] == "True":
sp.number_result1.append(1)
else:
sp.number_result1.append(0)
else:
rv = 0
for r in ret["results"]["bindings"]:
rv += 1
sp.number_result1.append(rv)
print("Done", end="", file=sys.stderr, flush=True)
sp.error1.append(False)
except Exception as inst:
print(inst, file=sys.stderr)
sp.error1.append(True)
count += 1
if count % 1000 == 0:
with open("results_pre_" + str(count) + ".json", "w") as f:
json.dump(
{
"precount": count,
"engines": [
{
"id": x.id,
"name": x.name,
"time1": x.time1,
"number_result1": x.number_result1,
"error1": x.error1
} for x in sparlq
]
},
f,
indent=2,
)
print(file=sys.stderr)
for i in range(len(sparlq)):
sp = sparlq[i]
print("result wrote in file results.json")
# Write end file
with open("results.json", "w") as f:
json.dump(
{
"engines": [
{
"id": x.id,
"name": x.name,
"time1": x.time1,
"number_result1": x.number_result1,
"error1": x.error1
} for x in sparlq
]
},
f,
indent=2,
)
if DO_RECURSIVE:
count = 0
for query in rec_queries_5k:
# print(query.replace('\n', ' '))
if count >= COUNT:
break
for i in range(len(sparlq)):
sp = sparlq[i]
try:
print(f"{count}", sp.name + "...",
end="", file=sys.stderr, flush=True)
before = datetime.datetime.now()
sp.sparql1.setQuery(query + "")
ret = sp.sparql1.queryAndConvert()
delta = datetime.datetime.now() - before
microseconds = delta.total_seconds() * 1000
sp.time1.append(microseconds)
if "boolean" in ret:
if ret["boolean"] == "True":
sp.number_result1.append(1)
else:
sp.number_result1.append(0)
else:
rv = 0
for r in ret["results"]["bindings"]:
rv += 1
sp.number_result1.append(rv)
print("Done", end="", file=sys.stderr, flush=True)
sp.error1.append(False)
except Exception as inst:
print(inst, file=sys.stderr)
sp.error1.append(True)
count += 1
if count % 100 == 0:
with open("results_rec_pre_" + str(count) + ".json", "w") as f:
json.dump(
{
"precount": count,
"engines": [
{
"id": x.id,
"name": x.name,
"time1": x.time1,
"number_result1": x.number_result1,
"error1": x.error1
} for x in sparlq
]
},
f,
indent=2,
)
print(file=sys.stderr)
for i in range(len(sparlq)):
sp = sparlq[i]
print("result wrote in file results.json")
# Write end file
with open("results_rec.json", "w") as f:
json.dump(
{
"engines": [
{
"id": x.id,
"name": x.name,
"time1": x.time1,
"number_result1": x.number_result1,
"error1": x.error1
} for x in sparlq
]
},
f,
indent=2,
)