StatsPlugin.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. import time
  2. import cgi
  3. import os
  4. import json
  5. from Plugin import PluginManager
  6. from Config import config
  7. @PluginManager.registerTo("UiRequest")
  8. class UiRequestPlugin(object):
  9. def formatTableRow(self, row, class_name=""):
  10. back = []
  11. for format, val in row:
  12. if val is None:
  13. formatted = "n/a"
  14. elif format == "since":
  15. if val:
  16. formatted = "%.0f" % (time.time() - val)
  17. else:
  18. formatted = "n/a"
  19. else:
  20. formatted = format % val
  21. back.append("<td>%s</td>" % formatted)
  22. return "<tr class='%s'>%s</tr>" % (class_name.encode("utf8"), "".join(back).encode("utf8"))
  23. def getObjSize(self, obj, hpy=None):
  24. if hpy:
  25. return float(hpy.iso(obj).domisize) / 1024
  26. else:
  27. return 0
  28. # /Stats entry point
  29. def actionStats(self):
  30. import gc
  31. import sys
  32. from Ui import UiRequest
  33. from Db import Db
  34. from Crypt import CryptConnection
  35. hpy = None
  36. if self.get.get("size") == "1": # Calc obj size
  37. try:
  38. import guppy
  39. hpy = guppy.hpy()
  40. except:
  41. pass
  42. self.sendHeader()
  43. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  44. yield "This function is disabled on this proxy"
  45. raise StopIteration
  46. s = time.time()
  47. main = sys.modules["main"]
  48. # Style
  49. yield """
  50. <style>
  51. * { font-family: monospace }
  52. table td, table th { text-align: right; padding: 0px 10px }
  53. .connections td { white-space: nowrap }
  54. .serving-False { opacity: 0.3 }
  55. </style>
  56. """
  57. # Memory
  58. yield "rev%s | " % config.rev
  59. yield "%s | " % main.file_server.ip_external_list
  60. yield "Port: %s | " % main.file_server.port
  61. yield "IP Network: %s | " % main.file_server.supported_ip_types
  62. yield "Opened: %s | " % main.file_server.port_opened
  63. yield "Crypt: %s | " % CryptConnection.manager.crypt_supported
  64. yield "In: %.2fMB, Out: %.2fMB | " % (
  65. float(main.file_server.bytes_recv) / 1024 / 1024,
  66. float(main.file_server.bytes_sent) / 1024 / 1024
  67. )
  68. yield "Peerid: %s | " % main.file_server.peer_id
  69. yield "Time correction: %.2fs" % main.file_server.getTimecorrection()
  70. try:
  71. import psutil
  72. process = psutil.Process(os.getpid())
  73. mem = process.get_memory_info()[0] / float(2 ** 20)
  74. yield "Mem: %.2fMB | " % mem
  75. yield "Threads: %s | " % len(process.threads())
  76. yield "CPU: usr %.2fs sys %.2fs | " % process.cpu_times()
  77. yield "Files: %s | " % len(process.open_files())
  78. yield "Sockets: %s | " % len(process.connections())
  79. yield "Calc size <a href='?size=1'>on</a> <a href='?size=0'>off</a>"
  80. except Exception:
  81. pass
  82. yield "<br>"
  83. # Connections
  84. yield "<b>Connections</b> (%s, total made: %s, in: %s, out: %s):<br>" % (
  85. len(main.file_server.connections), main.file_server.last_connection_id, main.file_server.num_incoming, main.file_server.num_outgoing
  86. )
  87. yield "<table class='connections'><tr> <th>id</th> <th>type</th> <th>ip</th> <th>open</th> <th>crypt</th> <th>ping</th>"
  88. yield "<th>buff</th> <th>bad</th> <th>idle</th> <th>open</th> <th>delay</th> <th>cpu</th> <th>out</th> <th>in</th> <th>last sent</th>"
  89. yield "<th>wait</th> <th>version</th> <th>time</th> <th>sites</th> </tr>"
  90. for connection in main.file_server.connections:
  91. if "cipher" in dir(connection.sock):
  92. cipher = connection.sock.cipher()[0]
  93. tls_version = connection.sock.version()
  94. else:
  95. cipher = connection.crypt
  96. tls_version = ""
  97. if "time" in connection.handshake and connection.last_ping_delay:
  98. time_correction = connection.handshake["time"] - connection.handshake_time - connection.last_ping_delay
  99. else:
  100. time_correction = 0.0
  101. yield self.formatTableRow([
  102. ("%3d", connection.id),
  103. ("%s", connection.type),
  104. ("%s:%s", (connection.ip, connection.port)),
  105. ("%s", connection.handshake.get("port_opened")),
  106. ("<span title='%s %s'>%s</span>", (cipher, tls_version, connection.crypt)),
  107. ("%6.3f", connection.last_ping_delay),
  108. ("%s", connection.incomplete_buff_recv),
  109. ("%s", connection.bad_actions),
  110. ("since", max(connection.last_send_time, connection.last_recv_time)),
  111. ("since", connection.start_time),
  112. ("%.3f", max(-1, connection.last_sent_time - connection.last_send_time)),
  113. ("%.3f", connection.cpu_time),
  114. ("%.0fkB", connection.bytes_sent / 1024),
  115. ("%.0fkB", connection.bytes_recv / 1024),
  116. ("<span title='Recv: %s'>%s</span>", (connection.last_cmd_recv, connection.last_cmd_sent)),
  117. ("%s", connection.waiting_requests.keys()),
  118. ("%s r%s", (connection.handshake.get("version"), connection.handshake.get("rev", "?"))),
  119. ("%.2fs", time_correction),
  120. ("%s", connection.sites)
  121. ])
  122. yield "</table>"
  123. # Trackers
  124. yield "<br><br><b>Trackers:</b><br>"
  125. yield "<table class='trackers'><tr> <th>address</th> <th>request</th> <th>successive errors</th> <th>last_request</th></tr>"
  126. for tracker_address, tracker_stat in sorted(sys.modules["Site.SiteAnnouncer"].global_stats.iteritems()):
  127. yield self.formatTableRow([
  128. ("%s", tracker_address),
  129. ("%s", tracker_stat["num_request"]),
  130. ("%s", tracker_stat["num_error"]),
  131. ("%.0f min ago", min(999, (time.time() - tracker_stat["time_request"]) / 60))
  132. ])
  133. yield "</table>"
  134. if "AnnounceShare" in PluginManager.plugin_manager.plugin_names:
  135. yield "<br><br><b>Shared trackers:</b><br>"
  136. yield "<table class='trackers'><tr> <th>address</th> <th>added</th> <th>found</th> <th>latency</th> <th>successive errors</th> <th>last_success</th></tr>"
  137. from AnnounceShare import AnnounceSharePlugin
  138. for tracker_address, tracker_stat in sorted(AnnounceSharePlugin.tracker_storage.getTrackers().iteritems()):
  139. yield self.formatTableRow([
  140. ("%s", tracker_address),
  141. ("%.0f min ago", min(999, (time.time() - tracker_stat["time_added"]) / 60)),
  142. ("%.0f min ago", min(999, (time.time() - tracker_stat.get("time_found", 0)) / 60)),
  143. ("%.3fs", tracker_stat["latency"]),
  144. ("%s", tracker_stat["num_error"]),
  145. ("%.0f min ago", min(999, (time.time() - tracker_stat["time_success"]) / 60)),
  146. ])
  147. yield "</table>"
  148. # Tor hidden services
  149. yield "<br><br><b>Tor hidden services (status: %s):</b><br>" % main.file_server.tor_manager.status.encode("utf8")
  150. for site_address, onion in main.file_server.tor_manager.site_onions.items():
  151. yield "- %-34s: %s<br>" % (site_address, onion.encode("utf8"))
  152. # Db
  153. yield "<br><br><b>Db</b>:<br>"
  154. for db in sys.modules["Db.Db"].opened_dbs:
  155. tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall()]
  156. table_rows = {}
  157. for table in tables:
  158. table_rows[table] = db.execute("SELECT COUNT(*) AS c FROM %s" % table).fetchone()["c"]
  159. db_size = os.path.getsize(db.db_path) / 1024.0 / 1024.0
  160. yield "- %.3fs: %s %.3fMB, table rows: %s<br>" % (
  161. time.time() - db.last_query_time, db.db_path.encode("utf8"), db_size, json.dumps(table_rows, sort_keys=True)
  162. )
  163. # Sites
  164. yield "<br><br><b>Sites</b>:"
  165. yield "<table>"
  166. yield "<tr><th>address</th> <th>connected</th> <th title='connected/good/total'>peers</th> <th>content.json</th> <th>out</th> <th>in</th> </tr>"
  167. for site in sorted(self.server.sites.values(), lambda a, b: cmp(a.address,b.address)):
  168. yield self.formatTableRow([
  169. (
  170. """<a href='#' onclick='document.getElementById("peers_%s").style.display="initial"; return false'>%s</a>""",
  171. (site.address, site.address)
  172. ),
  173. ("%s", [peer.connection.id for peer in site.peers.values() if peer.connection and peer.connection.connected]),
  174. ("%s/%s/%s", (
  175. len([peer for peer in site.peers.values() if peer.connection and peer.connection.connected]),
  176. len(site.getConnectablePeers(100)),
  177. len(site.peers)
  178. )),
  179. ("%s (loaded: %s)", (
  180. len(site.content_manager.contents),
  181. len([key for key, val in dict(site.content_manager.contents).iteritems() if val])
  182. )),
  183. ("%.0fkB", site.settings.get("bytes_sent", 0) / 1024),
  184. ("%.0fkB", site.settings.get("bytes_recv", 0) / 1024),
  185. ], "serving-%s" % site.settings["serving"])
  186. yield "<tr><td id='peers_%s' style='display: none; white-space: pre' colspan=6>" % site.address
  187. for key, peer in site.peers.items():
  188. if peer.time_found:
  189. time_found = int(time.time() - peer.time_found) / 60
  190. else:
  191. time_found = "--"
  192. if peer.connection:
  193. connection_id = peer.connection.id
  194. else:
  195. connection_id = None
  196. if site.content_manager.has_optional_files:
  197. yield "Optional files: %4s " % len(peer.hashfield)
  198. time_added = (time.time() - peer.time_added) / (60 * 60 * 24)
  199. yield "(#%4s, rep: %2s, err: %s, found: %3s min, add: %.1f day) %30s -<br>" % (connection_id, peer.reputation, peer.connection_error, time_found, time_added, key)
  200. yield "<br></td></tr>"
  201. yield "</table>"
  202. # Big files
  203. yield "<br><br><b>Big files</b>:<br>"
  204. for site in self.server.sites.values():
  205. if not site.settings.get("has_bigfile"):
  206. continue
  207. bigfiles = {}
  208. yield """<a href="#" onclick='document.getElementById("bigfiles_%s").style.display="initial"; return false'>%s</a><br>""" % (site.address, site.address)
  209. for peer in site.peers.values():
  210. if not peer.time_piecefields_updated:
  211. continue
  212. for sha512, piecefield in peer.piecefields.iteritems():
  213. if sha512 not in bigfiles:
  214. bigfiles[sha512] = []
  215. bigfiles[sha512].append(peer)
  216. yield "<div id='bigfiles_%s' style='display: none'>" % site.address
  217. for sha512, peers in bigfiles.iteritems():
  218. yield "<br> - " + sha512 + " (hash id: %s)<br>" % site.content_manager.hashfield.getHashId(sha512)
  219. yield "<table>"
  220. for peer in peers:
  221. yield "<tr><td>" + peer.key + "</td><td>" + peer.piecefields[sha512].tostring() + "</td></tr>"
  222. yield "</table>"
  223. yield "</div>"
  224. # Cmd stats
  225. yield "<div style='float: left'>"
  226. yield "<br><br><b>Sent commands</b>:<br>"
  227. yield "<table>"
  228. for stat_key, stat in sorted(main.file_server.stat_sent.items(), lambda a, b: cmp(a[1]["bytes"], b[1]["bytes"]), reverse=True):
  229. yield "<tr><td>%s</td><td style='white-space: nowrap'>x %s =</td><td>%.0fkB</td></tr>" % (stat_key, stat["num"], stat["bytes"] / 1024)
  230. yield "</table>"
  231. yield "</div>"
  232. yield "<div style='float: left; margin-left: 20%; max-width: 50%'>"
  233. yield "<br><br><b>Received commands</b>:<br>"
  234. yield "<table>"
  235. for stat_key, stat in sorted(main.file_server.stat_recv.items(), lambda a, b: cmp(a[1]["bytes"], b[1]["bytes"]), reverse=True):
  236. yield "<tr><td>%s</td><td style='white-space: nowrap'>x %s =</td><td>%.0fkB</td></tr>" % (stat_key, stat["num"], stat["bytes"] / 1024)
  237. yield "</table>"
  238. yield "</div>"
  239. yield "<div style='clear: both'></div>"
  240. # No more if not in debug mode
  241. if not config.debug:
  242. raise StopIteration
  243. # Object types
  244. obj_count = {}
  245. for obj in gc.get_objects():
  246. obj_type = str(type(obj))
  247. if obj_type not in obj_count:
  248. obj_count[obj_type] = [0, 0]
  249. obj_count[obj_type][0] += 1 # Count
  250. obj_count[obj_type][1] += float(sys.getsizeof(obj)) / 1024 # Size
  251. yield "<br><br><b>Objects in memory (types: %s, total: %s, %.2fkb):</b><br>" % (
  252. len(obj_count),
  253. sum([stat[0] for stat in obj_count.values()]),
  254. sum([stat[1] for stat in obj_count.values()])
  255. )
  256. for obj, stat in sorted(obj_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count
  257. yield " - %.1fkb = %s x <a href=\"/Listobj?type=%s\">%s</a><br>" % (stat[1], stat[0], obj, cgi.escape(obj))
  258. # Classes
  259. class_count = {}
  260. for obj in gc.get_objects():
  261. obj_type = str(type(obj))
  262. if obj_type != "<type 'instance'>":
  263. continue
  264. class_name = obj.__class__.__name__
  265. if class_name not in class_count:
  266. class_count[class_name] = [0, 0]
  267. class_count[class_name][0] += 1 # Count
  268. class_count[class_name][1] += float(sys.getsizeof(obj)) / 1024 # Size
  269. yield "<br><br><b>Classes in memory (types: %s, total: %s, %.2fkb):</b><br>" % (
  270. len(class_count),
  271. sum([stat[0] for stat in class_count.values()]),
  272. sum([stat[1] for stat in class_count.values()])
  273. )
  274. for obj, stat in sorted(class_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count
  275. yield " - %.1fkb = %s x <a href=\"/Dumpobj?class=%s\">%s</a><br>" % (stat[1], stat[0], obj, cgi.escape(obj))
  276. from greenlet import greenlet
  277. objs = [obj for obj in gc.get_objects() if isinstance(obj, greenlet)]
  278. yield "<br>Greenlets (%s):<br>" % len(objs)
  279. for obj in objs:
  280. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj).encode("utf8")))
  281. from Worker import Worker
  282. objs = [obj for obj in gc.get_objects() if isinstance(obj, Worker)]
  283. yield "<br>Workers (%s):<br>" % len(objs)
  284. for obj in objs:
  285. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  286. from Connection import Connection
  287. objs = [obj for obj in gc.get_objects() if isinstance(obj, Connection)]
  288. yield "<br>Connections (%s):<br>" % len(objs)
  289. for obj in objs:
  290. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  291. from socket import socket
  292. objs = [obj for obj in gc.get_objects() if isinstance(obj, socket)]
  293. yield "<br>Sockets (%s):<br>" % len(objs)
  294. for obj in objs:
  295. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  296. from msgpack import Unpacker
  297. objs = [obj for obj in gc.get_objects() if isinstance(obj, Unpacker)]
  298. yield "<br>Msgpack unpacker (%s):<br>" % len(objs)
  299. for obj in objs:
  300. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  301. from Site import Site
  302. objs = [obj for obj in gc.get_objects() if isinstance(obj, Site)]
  303. yield "<br>Sites (%s):<br>" % len(objs)
  304. for obj in objs:
  305. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  306. objs = [obj for obj in gc.get_objects() if isinstance(obj, self.server.log.__class__)]
  307. yield "<br>Loggers (%s):<br>" % len(objs)
  308. for obj in objs:
  309. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj.name)))
  310. objs = [obj for obj in gc.get_objects() if isinstance(obj, UiRequest)]
  311. yield "<br>UiRequests (%s):<br>" % len(objs)
  312. for obj in objs:
  313. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  314. from Peer import Peer
  315. objs = [obj for obj in gc.get_objects() if isinstance(obj, Peer)]
  316. yield "<br>Peers (%s):<br>" % len(objs)
  317. for obj in objs:
  318. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  319. objs = [(key, val) for key, val in sys.modules.iteritems() if val is not None]
  320. objs.sort()
  321. yield "<br>Modules (%s):<br>" % len(objs)
  322. for module_name, module in objs:
  323. yield " - %.3fkb: %s %s<br>" % (self.getObjSize(module, hpy), module_name, cgi.escape(repr(module)))
  324. gc.collect() # Implicit grabage collection
  325. yield "Done in %.1f" % (time.time() - s)
  326. def actionDumpobj(self):
  327. import gc
  328. import sys
  329. self.sendHeader()
  330. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  331. yield "This function is disabled on this proxy"
  332. raise StopIteration
  333. # No more if not in debug mode
  334. if not config.debug:
  335. yield "Not in debug mode"
  336. raise StopIteration
  337. class_filter = self.get.get("class")
  338. yield """
  339. <style>
  340. * { font-family: monospace; white-space: pre }
  341. table * { text-align: right; padding: 0px 10px }
  342. </style>
  343. """
  344. objs = gc.get_objects()
  345. for obj in objs:
  346. obj_type = str(type(obj))
  347. if obj_type != "<type 'instance'>" or obj.__class__.__name__ != class_filter:
  348. continue
  349. yield "%.1fkb %s... " % (float(sys.getsizeof(obj)) / 1024, cgi.escape(str(obj)))
  350. for attr in dir(obj):
  351. yield "- %s: %s<br>" % (attr, cgi.escape(str(getattr(obj, attr))))
  352. yield "<br>"
  353. gc.collect() # Implicit grabage collection
  354. def actionListobj(self):
  355. import gc
  356. import sys
  357. self.sendHeader()
  358. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  359. yield "This function is disabled on this proxy"
  360. raise StopIteration
  361. # No more if not in debug mode
  362. if not config.debug:
  363. yield "Not in debug mode"
  364. raise StopIteration
  365. type_filter = self.get.get("type")
  366. yield """
  367. <style>
  368. * { font-family: monospace; white-space: pre }
  369. table * { text-align: right; padding: 0px 10px }
  370. </style>
  371. """
  372. yield "Listing all %s objects in memory...<br>" % cgi.escape(type_filter)
  373. ref_count = {}
  374. objs = gc.get_objects()
  375. for obj in objs:
  376. obj_type = str(type(obj))
  377. if obj_type != type_filter:
  378. continue
  379. refs = [
  380. ref for ref in gc.get_referrers(obj)
  381. if hasattr(ref, "__class__") and
  382. ref.__class__.__name__ not in ["list", "dict", "function", "type", "frame", "WeakSet", "tuple"]
  383. ]
  384. if not refs:
  385. continue
  386. try:
  387. yield "%.1fkb <span title=\"%s\">%s</span>... " % (
  388. float(sys.getsizeof(obj)) / 1024, cgi.escape(str(obj)), cgi.escape(str(obj)[0:100].ljust(100))
  389. )
  390. except:
  391. continue
  392. for ref in refs:
  393. yield " ["
  394. if "object at" in str(ref) or len(str(ref)) > 100:
  395. yield str(ref.__class__.__name__)
  396. else:
  397. yield str(ref.__class__.__name__) + ":" + cgi.escape(str(ref))
  398. yield "] "
  399. ref_type = ref.__class__.__name__
  400. if ref_type not in ref_count:
  401. ref_count[ref_type] = [0, 0]
  402. ref_count[ref_type][0] += 1 # Count
  403. ref_count[ref_type][1] += float(sys.getsizeof(obj)) / 1024 # Size
  404. yield "<br>"
  405. yield "<br>Object referrer (total: %s, %.2fkb):<br>" % (len(ref_count), sum([stat[1] for stat in ref_count.values()]))
  406. for obj, stat in sorted(ref_count.items(), key=lambda x: x[1][0], reverse=True)[0:30]: # Sorted by count
  407. yield " - %.1fkb = %s x %s<br>" % (stat[1], stat[0], cgi.escape(str(obj)))
  408. gc.collect() # Implicit grabage collection
  409. def actionBenchmark(self):
  410. import sys
  411. import gc
  412. from contextlib import contextmanager
  413. output = self.sendHeader()
  414. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  415. yield "This function is disabled on this proxy"
  416. raise StopIteration
  417. @contextmanager
  418. def benchmark(name, standard):
  419. s = time.time()
  420. output("- %s" % name)
  421. try:
  422. yield 1
  423. except Exception, err:
  424. output("<br><b>! Error: %s</b><br>" % err)
  425. taken = time.time() - s
  426. if taken > 0:
  427. multipler = standard / taken
  428. else:
  429. multipler = 99
  430. if multipler < 0.3:
  431. speed = "Sloooow"
  432. elif multipler < 0.5:
  433. speed = "Ehh"
  434. elif multipler < 0.8:
  435. speed = "Goodish"
  436. elif multipler < 1.2:
  437. speed = "OK"
  438. elif multipler < 1.7:
  439. speed = "Fine"
  440. elif multipler < 2.5:
  441. speed = "Fast"
  442. elif multipler < 3.5:
  443. speed = "WOW"
  444. else:
  445. speed = "Insane!!"
  446. output("%.3fs [x%.2f: %s]<br>" % (taken, multipler, speed))
  447. time.sleep(0.01)
  448. yield """
  449. <style>
  450. * { font-family: monospace }
  451. table * { text-align: right; padding: 0px 10px }
  452. </style>
  453. """
  454. yield "Benchmarking ZeroNet %s (rev%s) Python %s on: %s...<br>" % (config.version, config.rev, sys.version, sys.platform)
  455. t = time.time()
  456. # CryptBitcoin
  457. yield "<br>CryptBitcoin:<br>"
  458. from Crypt import CryptBitcoin
  459. # seed = CryptBitcoin.newSeed()
  460. # yield "- Seed: %s<br>" % seed
  461. seed = "e180efa477c63b0f2757eac7b1cce781877177fe0966be62754ffd4c8592ce38"
  462. with benchmark("hdPrivatekey x 10", 0.7):
  463. for i in range(10):
  464. privatekey = CryptBitcoin.hdPrivatekey(seed, i * 10)
  465. yield "."
  466. valid = "5JsunC55XGVqFQj5kPGK4MWgTL26jKbnPhjnmchSNPo75XXCwtk"
  467. assert privatekey == valid, "%s != %s" % (privatekey, valid)
  468. data = "Hello" * 1024 # 5k
  469. with benchmark("sign x 10", 0.35):
  470. for i in range(10):
  471. yield "."
  472. sign = CryptBitcoin.sign(data, privatekey)
  473. valid = "G1GXaDauZ8vX/N9Jn+MRiGm9h+I94zUhDnNYFaqMGuOiBHB+kp4cRPZOL7l1yqK5BHa6J+W97bMjvTXtxzljp6w="
  474. assert sign == valid, "%s != %s" % (sign, valid)
  475. address = CryptBitcoin.privatekeyToAddress(privatekey)
  476. if CryptBitcoin.opensslVerify: # Openssl avalible
  477. with benchmark("openssl verify x 100", 0.37):
  478. for i in range(100):
  479. if i % 10 == 0:
  480. yield "."
  481. ok = CryptBitcoin.verify(data, address, sign)
  482. assert ok, "does not verify from %s" % address
  483. else:
  484. yield " - openssl verify x 100...not avalible :(<br>"
  485. openssl_verify_bk = CryptBitcoin.opensslVerify # Emulate openssl not found in any way
  486. CryptBitcoin.opensslVerify = None
  487. with benchmark("pure-python verify x 10", 1.6):
  488. for i in range(10):
  489. yield "."
  490. ok = CryptBitcoin.verify(data, address, sign)
  491. assert ok, "does not verify from %s" % address
  492. CryptBitcoin.opensslVerify = openssl_verify_bk
  493. # CryptHash
  494. yield "<br>CryptHash:<br>"
  495. from Crypt import CryptHash
  496. from cStringIO import StringIO
  497. data = StringIO("Hello" * 1024 * 1024) # 5m
  498. with benchmark("sha256 5M x 10", 0.6):
  499. for i in range(10):
  500. data.seek(0)
  501. hash = CryptHash.sha256sum(data)
  502. yield "."
  503. valid = "8cd629d9d6aff6590da8b80782a5046d2673d5917b99d5603c3dcb4005c45ffa"
  504. assert hash == valid, "%s != %s" % (hash, valid)
  505. data = StringIO("Hello" * 1024 * 1024) # 5m
  506. with benchmark("sha512 5M x 10", 0.6):
  507. for i in range(10):
  508. data.seek(0)
  509. hash = CryptHash.sha512sum(data)
  510. yield "."
  511. valid = "9ca7e855d430964d5b55b114e95c6bbb114a6d478f6485df93044d87b108904d"
  512. assert hash == valid, "%s != %s" % (hash, valid)
  513. with benchmark("os.urandom(256) x 1000", 0.0065):
  514. for i in range(10):
  515. for y in range(100):
  516. data = os.urandom(256)
  517. yield "."
  518. # Msgpack
  519. import msgpack
  520. yield "<br>Msgpack: (version: %s)<br>" % ".".join(map(str, msgpack.version))
  521. binary = 'fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv'
  522. data = {"int": 1024*1024*1024, "float": 12345.67890, "text": "hello"*1024, "binary": binary}
  523. with benchmark("pack 5K x 10 000", 0.78):
  524. for i in range(10):
  525. for y in range(1000):
  526. data_packed = msgpack.packb(data)
  527. yield "."
  528. valid = """\x84\xa3int\xce@\x00\x00\x00\xa4text\xda\x14\x00hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello\xa5float\xcb@\xc8\x1c\xd6\xe61\xf8\xa1\xa6binary\xda\x01\x00fqv\xf0\x1a"e\x10,\xbe\x9cT\x9e(\xa5]u\x072C\x8c\x15\xa2\xa8\x93Sw)\x19\x02\xdd\t\xfb\xf67\x88\xd9\xee\x86\xa1\xe4\xb6,\xc6\x14\xbb\xd7$z\x1d\xb2\xda\x85\xf5\xa0\x97^\x01*\xaf\xd3\xb0!\xb7\x9d\xea\x89\xbbh8\xa1"\xa7]e(@\xa2\xa5g\xb7[\xae\x8eE\xc2\x9fL\xb6s\x19\x19\r\xc8\x04S\xd0N\xe4]?/\x01\xea\xf6\xec\xd1\xb3\xc2\x91\x86\xd7\xf4K\xdf\xc2lV\xf4\xe8\x80\xfc\x8ep\xbb\x82\xb3\x86\x98F\x1c\xecS\xc8\x15\xcf\xdc\xf1\xed\xfc\xd8\x18r\xf9\x80\x0f\xfa\x8cO\x97(\x0b]\xf1\xdd\r\xe7\xbf\xed\x06\xbd\x1b?\xc5\xa0\xd7a\x82\xf3\xa8\xe6@\xf3\ri\xa1\xb10\xf6\xd4W\xbc\x86\x1a\xbb\xfd\x94!bS\xdb\xaeM\x92\x00#\x0b\xf7\xad\xe9\xc2\x8e\x86\xbfi![%\xd31]\xc6\xfc2\xc9\xda\xc6v\x82P\xcc\xa9\xea\xb9\xff\xf6\xc8\x17iD\xcf\xf3\xeeI\x04\xe9\xa1\x19\xbb\x01\x92\xf5nn4K\xf8\xbb\xc6\x17e>\xa7 \xbbv"""
  529. assert data_packed == valid, "%s<br>!=<br>%s" % (repr(data_packed), repr(valid))
  530. with benchmark("unpack 5K x 10 000", 1.2):
  531. for i in range(10):
  532. for y in range(1000):
  533. data_unpacked = msgpack.unpackb(data_packed)
  534. yield "."
  535. assert data == data_unpacked, "%s != %s" % (data_unpacked, data)
  536. with benchmark("streaming unpack 5K x 10 000", 1.4):
  537. for i in range(10):
  538. unpacker = msgpack.Unpacker()
  539. for y in range(1000):
  540. unpacker.feed(data_packed)
  541. for data_unpacked in unpacker:
  542. pass
  543. yield "."
  544. assert data == data_unpacked, "%s != %s" % (data_unpacked, data)
  545. # Db
  546. from Db import Db
  547. import sqlite3
  548. yield "<br>Db: (version: %s, API: %s)<br>" % (sqlite3.sqlite_version, sqlite3.version)
  549. schema = {
  550. "db_name": "TestDb",
  551. "db_file": "%s/benchmark.db" % config.data_dir,
  552. "maps": {
  553. ".*": {
  554. "to_table": {
  555. "test": "test"
  556. }
  557. }
  558. },
  559. "tables": {
  560. "test": {
  561. "cols": [
  562. ["test_id", "INTEGER"],
  563. ["title", "TEXT"],
  564. ["json_id", "INTEGER REFERENCES json (json_id)"]
  565. ],
  566. "indexes": ["CREATE UNIQUE INDEX test_key ON test(test_id, json_id)"],
  567. "schema_changed": 1426195822
  568. }
  569. }
  570. }
  571. if os.path.isfile("%s/benchmark.db" % config.data_dir):
  572. os.unlink("%s/benchmark.db" % config.data_dir)
  573. with benchmark("Open x 10", 0.13):
  574. for i in range(10):
  575. db = Db(schema, "%s/benchmark.db" % config.data_dir)
  576. db.checkTables()
  577. db.close()
  578. yield "."
  579. db = Db(schema, "%s/benchmark.db" % config.data_dir)
  580. db.checkTables()
  581. import json
  582. with benchmark("Insert x 10 x 1000", 1.0):
  583. for u in range(10): # 10 user
  584. data = {"test": []}
  585. for i in range(1000): # 1000 line of data
  586. data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
  587. json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
  588. db.updateJson("%s/test_%s.json" % (config.data_dir, u))
  589. os.unlink("%s/test_%s.json" % (config.data_dir, u))
  590. yield "."
  591. with benchmark("Buffered insert x 100 x 100", 1.3):
  592. cur = db.getCursor()
  593. cur.execute("BEGIN")
  594. cur.logging = False
  595. for u in range(100, 200): # 100 user
  596. data = {"test": []}
  597. for i in range(100): # 1000 line of data
  598. data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
  599. json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
  600. db.updateJson("%s/test_%s.json" % (config.data_dir, u), cur=cur)
  601. os.unlink("%s/test_%s.json" % (config.data_dir, u))
  602. if u % 10 == 0:
  603. yield "."
  604. cur.execute("COMMIT")
  605. yield " - Total rows in db: %s<br>" % db.execute("SELECT COUNT(*) AS num FROM test").fetchone()[0]
  606. with benchmark("Indexed query x 1000", 0.25):
  607. found = 0
  608. cur = db.getCursor()
  609. cur.logging = False
  610. for i in range(1000): # 1000x by test_id
  611. res = cur.execute("SELECT * FROM test WHERE test_id = %s" % i)
  612. for row in res:
  613. found += 1
  614. if i % 100 == 0:
  615. yield "."
  616. assert found == 20000, "Found: %s != 20000" % found
  617. with benchmark("Not indexed query x 100", 0.6):
  618. found = 0
  619. cur = db.getCursor()
  620. cur.logging = False
  621. for i in range(100): # 1000x by test_id
  622. res = cur.execute("SELECT * FROM test WHERE json_id = %s" % i)
  623. for row in res:
  624. found += 1
  625. if i % 10 == 0:
  626. yield "."
  627. assert found == 18900, "Found: %s != 18900" % found
  628. with benchmark("Like query x 100", 1.8):
  629. found = 0
  630. cur = db.getCursor()
  631. cur.logging = False
  632. for i in range(100): # 1000x by test_id
  633. res = cur.execute("SELECT * FROM test WHERE title LIKE '%%message %s%%'" % i)
  634. for row in res:
  635. found += 1
  636. if i % 10 == 0:
  637. yield "."
  638. assert found == 38900, "Found: %s != 11000" % found
  639. db.close()
  640. if os.path.isfile("%s/benchmark.db" % config.data_dir):
  641. os.unlink("%s/benchmark.db" % config.data_dir)
  642. gc.collect() # Implicit grabage collection
  643. # Zip
  644. yield "<br>Compression:<br>"
  645. import zipfile
  646. test_data = "Test" * 1024
  647. file_name = "\xc3\x81rv\xc3\xadzt\xc5\xb0r\xc5\x91t\xc3\xbck\xc3\xb6r\xc3\xb3g\xc3\xa9p\xe4\xb8\xad\xe5\x8d\x8e%s.txt"
  648. with benchmark("Zip pack x 10", 0.12):
  649. for i in range(10):
  650. with zipfile.ZipFile('%s/test.zip' % config.data_dir, 'w') as archive:
  651. for y in range(100):
  652. zip_info = zipfile.ZipInfo(file_name % y, (1980,1,1,0,0,0))
  653. zip_info.compress_type = zipfile.ZIP_DEFLATED
  654. zip_info.create_system = 3
  655. archive.writestr(zip_info, test_data)
  656. yield "."
  657. hash = CryptHash.sha512sum(open("%s/test.zip" % config.data_dir, "rb"))
  658. valid = "f6ef623e6653883a1758db14aa593350e26c9dc53a8406d6e6defd6029dbd483"
  659. assert hash == valid, "Invalid hash: %s != %s<br>" % (hash, valid)
  660. with benchmark("Zip unpack x 10", 0.2):
  661. for i in range(10):
  662. with zipfile.ZipFile('%s/test.zip' % config.data_dir) as archive:
  663. for y in range(100):
  664. assert archive.read(file_name % y) == test_data
  665. yield "."
  666. if os.path.isfile("%s/test.zip" % config.data_dir):
  667. os.unlink("%s/test.zip" % config.data_dir)
  668. # Tar.gz
  669. import tarfile
  670. import struct
  671. # Monkey patch _init_write_gz to use fixed date in order to keep the hash independent from datetime
  672. def nodate_write_gzip_header(self):
  673. self.mtime = 0
  674. original_write_gzip_header(self)
  675. import gzip
  676. original_write_gzip_header = gzip.GzipFile._write_gzip_header
  677. gzip.GzipFile._write_gzip_header = nodate_write_gzip_header
  678. test_data_io = StringIO("Test" * 1024)
  679. with benchmark("Tar.gz pack x 10", 0.3):
  680. for i in range(10):
  681. with tarfile.open('%s/test.tar.gz' % config.data_dir, 'w:gz') as archive:
  682. for y in range(100):
  683. test_data_io.seek(0)
  684. tar_info = tarfile.TarInfo(file_name % y)
  685. tar_info.size = 4 * 1024
  686. archive.addfile(tar_info, test_data_io)
  687. yield "."
  688. hash = CryptHash.sha512sum(open("%s/test.tar.gz" % config.data_dir, "rb"))
  689. valid = "4704ebd8c987ed6f833059f1de9c475d443b0539b8d4c4cb8b49b26f7bbf2d19"
  690. assert hash == valid, "Invalid hash: %s != %s<br>" % (hash, valid)
  691. with benchmark("Tar.gz unpack x 10", 0.2):
  692. for i in range(10):
  693. with tarfile.open('%s/test.tar.gz' % config.data_dir, 'r:gz') as archive:
  694. for y in range(100):
  695. assert archive.extractfile(file_name % y).read() == test_data
  696. yield "."
  697. if os.path.isfile("%s/test.tar.gz" % config.data_dir):
  698. os.unlink("%s/test.tar.gz" % config.data_dir)
  699. # Tar.bz2
  700. import tarfile
  701. test_data_io = StringIO("Test" * 1024)
  702. with benchmark("Tar.bz2 pack x 10", 2.0):
  703. for i in range(10):
  704. with tarfile.open('%s/test.tar.bz2' % config.data_dir, 'w:bz2') as archive:
  705. for y in range(100):
  706. test_data_io.seek(0)
  707. tar_info = tarfile.TarInfo(file_name % y)
  708. tar_info.size = 4 * 1024
  709. archive.addfile(tar_info, test_data_io)
  710. yield "."
  711. hash = CryptHash.sha512sum(open("%s/test.tar.bz2" % config.data_dir, "rb"))
  712. valid = "90cba0b4d9abaa37b830bf37e4adba93bfd183e095b489ebee62aaa94339f3b5"
  713. assert hash == valid, "Invalid hash: %s != %s<br>" % (hash, valid)
  714. with benchmark("Tar.bz2 unpack x 10", 0.5):
  715. for i in range(10):
  716. with tarfile.open('%s/test.tar.bz2' % config.data_dir, 'r:bz2') as archive:
  717. for y in range(100):
  718. assert archive.extractfile(file_name % y).read() == test_data
  719. yield "."
  720. if os.path.isfile("%s/test.tar.bz2" % config.data_dir):
  721. os.unlink("%s/test.tar.bz2" % config.data_dir)
  722. yield "<br>Done. Total: %.2fs" % (time.time() - t)
  723. def actionGcCollect(self):
  724. import gc
  725. self.sendHeader()
  726. yield str(gc.collect())