forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_exerciser.py
More file actions
185 lines (144 loc) · 4.86 KB
/
api_exerciser.py
File metadata and controls
185 lines (144 loc) · 4.86 KB
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
import gc
import string
import time
import traceback
from builtins import range
from math import floor
from random import choice, randint, sample
from threading import Thread
from pyload.core.remote.thriftbackend.thriftClient import Destination, ThriftClient
def createURLs():
"""
create some urls, some may fail.
"""
urls = []
for x in range(0, randint(20, 100)):
name = "DEBUG_API"
if randint(0, 5) == 5:
name = "" #: this link will fail
urls.append(name + "".join(sample(string.ascii_letters, randint(10, 20))))
return urls
AVOID = (0, 3, 8)
idPool = 0
sumCalled = 0
def startApiExerciser(core, n):
for i in range(n):
APIExerciser(core).start()
class APIExerciser(Thread):
def __init__(self, core, thrift=False, user=None, pw=None):
global idPool
super()
self.daemon = True
self.pyload = core
self.count = 0 #: number of methods
self.time = time.time()
if thrift:
self.api = ThriftClient(user=user, password=pw)
else:
self.api = core.api
self.id = idPool
idPool += 1
# self.start()
def run(self):
self.pyload.log.info("API Excerciser started {}".format(self.id))
with open("error.log", mode="ab") as out:
# core errors are not logged of course
out.write("\n" + "Starting\n")
out.flush()
while True:
try:
self.testAPI()
except Exception:
self.pyload.log.error(
"Excerciser {} throw an execption".format(self.id)
)
traceback.print_exc()
out.write(traceback.format_exc() + 2 * "\n")
out.flush()
if not self.count % 100:
self.pyload.log.info(
"Exerciser {} tested {} api calls".format(self.id, self.count)
)
if not self.count % 1000:
out.flush()
if not sumCalled % 1000: #: not thread safe
self.pyload.log.info(
"Exercisers tested {} api calls".format(sumCalled)
)
persec = sumCalled // (time() - self.time)
self.pyload.log.info(
"Approx. {:.2f} calls per second.".format(persec)
)
self.pyload.log.info(
"Approx. {:.2f} ms per call.".format(1000 // persec)
)
self.pyload.log.info("Collected garbage: {}".format(gc.collect()))
# time.sleep(random() / 500)
def testAPI(self):
global sumCalled
m = [
"statusDownloads",
"statusServer",
"addPackage",
"getPackageData",
"getFileData",
"deleteFiles",
"deletePackages",
"getQueue",
"getCollector",
"getQueueData",
"getCollectorData",
"isCaptchaWaiting",
"getCaptchaTask",
"stopAllDownloads",
"getAllInfo",
"getServices",
"getAccounts",
"getAllUserData",
]
method = choice(m)
# print("Testing:", method)
if hasattr(self, method):
res = getattr(self, method)()
else:
res = getattr(self.api, method)()
self.count += 1
sumCalled += 1
# print(res)
def addPackage(self):
name = "".join(sample(string.ascii_letters, 10))
urls = createURLs()
self.api.addPackage(
name, urls, choice([Destination.Queue, Destination.Collector])
)
def deleteFiles(self):
info = self.api.getQueueData()
if not info:
return
pack = choice(info)
fids = pack.links
if len(fids):
fids = [f.fid for f in sample(fids, randint(1, max(len(fids) // 2, 1)))]
self.api.deleteFiles(fids)
def deletePackages(self):
info = choice([self.api.getQueue(), self.api.getCollector()])
if not info:
return
pids = [p.pid for p in info]
if len(pids):
pids = sample(pids, randint(1, max(floor(len(pids) / 2.5), 1)))
self.api.deletePackages(pids)
def getFileData(self):
info = self.api.getQueueData()
if info:
p = choice(info)
if p.links:
self.api.getFileData(choice(p.links).fid)
def getPackageData(self):
info = self.api.getQueue()
if info:
self.api.getPackageData(choice(info).pid)
def getAccounts(self):
self.api.getAccounts(False)
def getCaptchaTask(self):
self.api.getCaptchaTask(False)