StatsPlugin.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import time
  2. import cgi
  3. import os
  4. from Plugin import PluginManager
  5. from Config import config
  6. @PluginManager.registerTo("UiRequest")
  7. class UiRequestPlugin(object):
  8. def formatTableRow(self, row):
  9. back = []
  10. for format, val in row:
  11. if val is None:
  12. formatted = "n/a"
  13. elif format == "since":
  14. if val:
  15. formatted = "%.0f" % (time.time() - val)
  16. else:
  17. formatted = "n/a"
  18. else:
  19. formatted = format % val
  20. back.append("<td>%s</td>" % formatted)
  21. return "<tr>%s</tr>" % "".join(back)
  22. def getObjSize(self, obj, hpy=None):
  23. if hpy:
  24. return float(hpy.iso(obj).domisize) / 1024
  25. else:
  26. return 0
  27. # /Stats entry point
  28. def actionStats(self):
  29. import gc
  30. import sys
  31. from Ui import UiRequest
  32. from Db import Db
  33. from Crypt import CryptConnection
  34. hpy = None
  35. if self.get.get("size") == "1": # Calc obj size
  36. try:
  37. import guppy
  38. hpy = guppy.hpy()
  39. except:
  40. pass
  41. self.sendHeader()
  42. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  43. yield "This function is disabled on this proxy"
  44. raise StopIteration
  45. s = time.time()
  46. main = sys.modules["main"]
  47. # Style
  48. yield """
  49. <style>
  50. * { font-family: monospace }
  51. table td, table th { text-align: right; padding: 0px 10px }
  52. .serving-False { color: gray }
  53. </style>
  54. """
  55. # Memory
  56. try:
  57. yield "rev%s | " % config.rev
  58. yield "%s | " % config.ip_external
  59. yield "Opened: %s | " % main.file_server.port_opened
  60. yield "Crypt: %s | " % CryptConnection.manager.crypt_supported
  61. yield "In: %.2fMB, Out: %.2fMB | " % (
  62. float(main.file_server.bytes_recv) / 1024 / 1024,
  63. float(main.file_server.bytes_sent) / 1024 / 1024
  64. )
  65. yield "Peerid: %s | " % main.file_server.peer_id
  66. import psutil
  67. process = psutil.Process(os.getpid())
  68. mem = process.get_memory_info()[0] / float(2 ** 20)
  69. yield "Mem: %.2fMB | " % mem
  70. yield "Threads: %s | " % len(process.threads())
  71. yield "CPU: usr %.2fs sys %.2fs | " % process.cpu_times()
  72. yield "Files: %s | " % len(process.open_files())
  73. yield "Sockets: %s | " % len(process.connections())
  74. yield "Calc size <a href='?size=1'>on</a> <a href='?size=0'>off</a>"
  75. except Exception:
  76. pass
  77. yield "<br>"
  78. # Connections
  79. yield "<b>Connections</b> (%s, total made: %s):<br>" % (
  80. len(main.file_server.connections), main.file_server.last_connection_id
  81. )
  82. yield "<table><tr> <th>id</th> <th>proto</th> <th>type</th> <th>ip</th> <th>open</th> <th>crypt</th> <th>ping</th>"
  83. 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>"
  84. yield "<th>waiting</th> <th>version</th> <th>sites</th> </tr>"
  85. for connection in main.file_server.connections:
  86. if "cipher" in dir(connection.sock):
  87. cipher = connection.sock.cipher()[0]
  88. else:
  89. cipher = connection.crypt
  90. yield self.formatTableRow([
  91. ("%3d", connection.id),
  92. ("%s", connection.protocol),
  93. ("%s", connection.type),
  94. ("%s:%s", (connection.ip, connection.port)),
  95. ("%s", connection.handshake.get("port_opened")),
  96. ("<span title='%s'>%s</span>", (connection.crypt, cipher)),
  97. ("%6.3f", connection.last_ping_delay),
  98. ("%s", connection.incomplete_buff_recv),
  99. ("%s", connection.bad_actions),
  100. ("since", max(connection.last_send_time, connection.last_recv_time)),
  101. ("since", connection.start_time),
  102. ("%.3f", connection.last_sent_time - connection.last_send_time),
  103. ("%.3fs", connection.cpu_time),
  104. ("%.0fkB", connection.bytes_sent / 1024),
  105. ("%.0fkB", connection.bytes_recv / 1024),
  106. ("%s", connection.last_cmd),
  107. ("%s", connection.waiting_requests.keys()),
  108. ("%s r%s", (connection.handshake.get("version"), connection.handshake.get("rev", "?"))),
  109. ("%s", connection.sites)
  110. ])
  111. yield "</table>"
  112. # Tor hidden services
  113. yield "<br><br><b>Tor hidden services (status: %s):</b><br>" % main.file_server.tor_manager.status
  114. for site_address, onion in main.file_server.tor_manager.site_onions.items():
  115. yield "- %-34s: %s<br>" % (site_address, onion)
  116. # Db
  117. yield "<br><br><b>Db</b>:<br>"
  118. for db in sys.modules["Db.Db"].opened_dbs:
  119. yield "- %.3fs: %s<br>" % (time.time() - db.last_query_time, db.db_path)
  120. # Sites
  121. yield "<br><br><b>Sites</b>:"
  122. yield "<table>"
  123. 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>"
  124. for site in sorted(self.server.sites.values(), lambda a, b: cmp(a.address,b.address)):
  125. yield self.formatTableRow([
  126. (
  127. """<a href='#' class='serving-%s' onclick='document.getElementById("peers_%s").style.display="initial"; return false'>%s</a>""",
  128. (site.settings["serving"], site.address, site.address)
  129. ),
  130. ("%s", [peer.connection.id for peer in site.peers.values() if peer.connection and peer.connection.connected]),
  131. ("%s/%s/%s", (
  132. len([peer for peer in site.peers.values() if peer.connection and peer.connection.connected]),
  133. len(site.getConnectablePeers(100)),
  134. len(site.peers)
  135. )),
  136. ("%s (loaded: %s)", (
  137. len(site.content_manager.contents),
  138. len([key for key, val in dict(site.content_manager.contents).iteritems() if val])
  139. )),
  140. ("%.0fkB", site.settings.get("bytes_sent", 0) / 1024),
  141. ("%.0fkB", site.settings.get("bytes_recv", 0) / 1024),
  142. ])
  143. yield "<tr><td id='peers_%s' style='display: none; white-space: pre' colspan=6>" % site.address
  144. for key, peer in site.peers.items():
  145. if peer.time_found:
  146. time_found = int(time.time() - peer.time_found) / 60
  147. else:
  148. time_found = "--"
  149. if peer.connection:
  150. connection_id = peer.connection.id
  151. else:
  152. connection_id = None
  153. if site.content_manager.hashfield:
  154. yield "Optional files: %4s " % len(peer.hashfield)
  155. yield "(#%4s, err: %s, found: %5s min ago) %30s -<br>" % (connection_id, peer.connection_error, time_found, key)
  156. yield "<br></td></tr>"
  157. yield "</table>"
  158. # No more if not in debug mode
  159. if not config.debug:
  160. raise StopIteration
  161. # Object types
  162. obj_count = {}
  163. for obj in gc.get_objects():
  164. obj_type = str(type(obj))
  165. if obj_type not in obj_count:
  166. obj_count[obj_type] = [0, 0]
  167. obj_count[obj_type][0] += 1 # Count
  168. obj_count[obj_type][1] += float(sys.getsizeof(obj)) / 1024 # Size
  169. yield "<br><br><b>Objects in memory (types: %s, total: %s, %.2fkb):</b><br>" % (
  170. len(obj_count),
  171. sum([stat[0] for stat in obj_count.values()]),
  172. sum([stat[1] for stat in obj_count.values()])
  173. )
  174. for obj, stat in sorted(obj_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count
  175. yield " - %.1fkb = %s x <a href=\"/Listobj?type=%s\">%s</a><br>" % (stat[1], stat[0], obj, cgi.escape(obj))
  176. # Classes
  177. class_count = {}
  178. for obj in gc.get_objects():
  179. obj_type = str(type(obj))
  180. if obj_type != "<type 'instance'>":
  181. continue
  182. class_name = obj.__class__.__name__
  183. if class_name not in class_count:
  184. class_count[class_name] = [0, 0]
  185. class_count[class_name][0] += 1 # Count
  186. class_count[class_name][1] += float(sys.getsizeof(obj)) / 1024 # Size
  187. yield "<br><br><b>Classes in memory (types: %s, total: %s, %.2fkb):</b><br>" % (
  188. len(class_count),
  189. sum([stat[0] for stat in class_count.values()]),
  190. sum([stat[1] for stat in class_count.values()])
  191. )
  192. for obj, stat in sorted(class_count.items(), key=lambda x: x[1][0], reverse=True): # Sorted by count
  193. yield " - %.1fkb = %s x <a href=\"/Dumpobj?class=%s\">%s</a><br>" % (stat[1], stat[0], obj, cgi.escape(obj))
  194. from greenlet import greenlet
  195. objs = [obj for obj in gc.get_objects() if isinstance(obj, greenlet)]
  196. yield "<br>Greenlets (%s):<br>" % len(objs)
  197. for obj in objs:
  198. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  199. from Worker import Worker
  200. objs = [obj for obj in gc.get_objects() if isinstance(obj, Worker)]
  201. yield "<br>Workers (%s):<br>" % len(objs)
  202. for obj in objs:
  203. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  204. from Connection import Connection
  205. objs = [obj for obj in gc.get_objects() if isinstance(obj, Connection)]
  206. yield "<br>Connections (%s):<br>" % len(objs)
  207. for obj in objs:
  208. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  209. from socket import socket
  210. objs = [obj for obj in gc.get_objects() if isinstance(obj, socket)]
  211. yield "<br>Sockets (%s):<br>" % len(objs)
  212. for obj in objs:
  213. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  214. from msgpack import Unpacker
  215. objs = [obj for obj in gc.get_objects() if isinstance(obj, Unpacker)]
  216. yield "<br>Msgpack unpacker (%s):<br>" % len(objs)
  217. for obj in objs:
  218. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  219. from Site import Site
  220. objs = [obj for obj in gc.get_objects() if isinstance(obj, Site)]
  221. yield "<br>Sites (%s):<br>" % len(objs)
  222. for obj in objs:
  223. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  224. objs = [obj for obj in gc.get_objects() if isinstance(obj, self.server.log.__class__)]
  225. yield "<br>Loggers (%s):<br>" % len(objs)
  226. for obj in objs:
  227. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj.name)))
  228. objs = [obj for obj in gc.get_objects() if isinstance(obj, UiRequest)]
  229. yield "<br>UiRequests (%s):<br>" % len(objs)
  230. for obj in objs:
  231. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  232. from Peer import Peer
  233. objs = [obj for obj in gc.get_objects() if isinstance(obj, Peer)]
  234. yield "<br>Peers (%s):<br>" % len(objs)
  235. for obj in objs:
  236. yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), cgi.escape(repr(obj)))
  237. objs = [(key, val) for key, val in sys.modules.iteritems() if val is not None]
  238. objs.sort()
  239. yield "<br>Modules (%s):<br>" % len(objs)
  240. for module_name, module in objs:
  241. yield " - %.3fkb: %s %s<br>" % (self.getObjSize(module, hpy), module_name, cgi.escape(repr(module)))
  242. gc.collect() # Implicit grabage collection
  243. yield "Done in %.1f" % (time.time() - s)
  244. def actionDumpobj(self):
  245. import gc
  246. import sys
  247. self.sendHeader()
  248. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  249. yield "This function is disabled on this proxy"
  250. raise StopIteration
  251. # No more if not in debug mode
  252. if not config.debug:
  253. yield "Not in debug mode"
  254. raise StopIteration
  255. class_filter = self.get.get("class")
  256. yield """
  257. <style>
  258. * { font-family: monospace; white-space: pre }
  259. table * { text-align: right; padding: 0px 10px }
  260. </style>
  261. """
  262. objs = gc.get_objects()
  263. for obj in objs:
  264. obj_type = str(type(obj))
  265. if obj_type != "<type 'instance'>" or obj.__class__.__name__ != class_filter:
  266. continue
  267. yield "%.1fkb %s... " % (float(sys.getsizeof(obj)) / 1024, cgi.escape(str(obj)))
  268. for attr in dir(obj):
  269. yield "- %s: %s<br>" % (attr, cgi.escape(str(getattr(obj, attr))))
  270. yield "<br>"
  271. gc.collect() # Implicit grabage collection
  272. def actionListobj(self):
  273. import gc
  274. import sys
  275. self.sendHeader()
  276. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  277. yield "This function is disabled on this proxy"
  278. raise StopIteration
  279. # No more if not in debug mode
  280. if not config.debug:
  281. yield "Not in debug mode"
  282. raise StopIteration
  283. type_filter = self.get.get("type")
  284. yield """
  285. <style>
  286. * { font-family: monospace; white-space: pre }
  287. table * { text-align: right; padding: 0px 10px }
  288. </style>
  289. """
  290. yield "Listing all %s objects in memory...<br>" % cgi.escape(type_filter)
  291. ref_count = {}
  292. objs = gc.get_objects()
  293. for obj in objs:
  294. obj_type = str(type(obj))
  295. if obj_type != type_filter:
  296. continue
  297. refs = [
  298. ref for ref in gc.get_referrers(obj)
  299. if hasattr(ref, "__class__") and
  300. ref.__class__.__name__ not in ["list", "dict", "function", "type", "frame", "WeakSet", "tuple"]
  301. ]
  302. if not refs:
  303. continue
  304. try:
  305. yield "%.1fkb <span title=\"%s\">%s</span>... " % (
  306. float(sys.getsizeof(obj)) / 1024, cgi.escape(str(obj)), cgi.escape(str(obj)[0:100].ljust(100))
  307. )
  308. except:
  309. continue
  310. for ref in refs:
  311. yield " ["
  312. if "object at" in str(ref) or len(str(ref)) > 100:
  313. yield str(ref.__class__.__name__)
  314. else:
  315. yield str(ref.__class__.__name__) + ":" + cgi.escape(str(ref))
  316. yield "] "
  317. ref_type = ref.__class__.__name__
  318. if ref_type not in ref_count:
  319. ref_count[ref_type] = [0, 0]
  320. ref_count[ref_type][0] += 1 # Count
  321. ref_count[ref_type][1] += float(sys.getsizeof(obj)) / 1024 # Size
  322. yield "<br>"
  323. yield "<br>Object referrer (total: %s, %.2fkb):<br>" % (len(ref_count), sum([stat[1] for stat in ref_count.values()]))
  324. for obj, stat in sorted(ref_count.items(), key=lambda x: x[1][0], reverse=True)[0:30]: # Sorted by count
  325. yield " - %.1fkb = %s x %s<br>" % (stat[1], stat[0], cgi.escape(str(obj)))
  326. gc.collect() # Implicit grabage collection
  327. def actionBenchmark(self):
  328. import sys
  329. import gc
  330. from contextlib import contextmanager
  331. output = self.sendHeader()
  332. if "Multiuser" in PluginManager.plugin_manager.plugin_names and not config.multiuser_local:
  333. yield "This function is disabled on this proxy"
  334. raise StopIteration
  335. @contextmanager
  336. def benchmark(name, standard):
  337. s = time.time()
  338. output("- %s" % name)
  339. try:
  340. yield 1
  341. except Exception, err:
  342. output("<br><b>! Error: %s</b><br>" % err)
  343. taken = time.time() - s
  344. multipler = standard / taken
  345. if multipler < 0.3:
  346. speed = "Sloooow"
  347. elif multipler < 0.5:
  348. speed = "Ehh"
  349. elif multipler < 0.8:
  350. speed = "Goodish"
  351. elif multipler < 1.2:
  352. speed = "OK"
  353. elif multipler < 1.7:
  354. speed = "Fine"
  355. elif multipler < 2.5:
  356. speed = "Fast"
  357. elif multipler < 3.5:
  358. speed = "WOW"
  359. else:
  360. speed = "Insane!!"
  361. output("%.3fs [x%.2f: %s]<br>" % (taken, multipler, speed))
  362. time.sleep(0.01)
  363. yield """
  364. <style>
  365. * { font-family: monospace }
  366. table * { text-align: right; padding: 0px 10px }
  367. </style>
  368. """
  369. yield "Benchmarking ZeroNet %s (rev%s) Python %s on: %s...<br>" % (config.version, config.rev, sys.version, sys.platform)
  370. t = time.time()
  371. # CryptBitcoin
  372. yield "<br>CryptBitcoin:<br>"
  373. from Crypt import CryptBitcoin
  374. # seed = CryptBitcoin.newSeed()
  375. # yield "- Seed: %s<br>" % seed
  376. seed = "e180efa477c63b0f2757eac7b1cce781877177fe0966be62754ffd4c8592ce38"
  377. with benchmark("hdPrivatekey x 10", 0.7):
  378. for i in range(10):
  379. privatekey = CryptBitcoin.hdPrivatekey(seed, i * 10)
  380. yield "."
  381. valid = "5JsunC55XGVqFQj5kPGK4MWgTL26jKbnPhjnmchSNPo75XXCwtk"
  382. assert privatekey == valid, "%s != %s" % (privatekey, valid)
  383. data = "Hello" * 1024 # 5k
  384. with benchmark("sign x 10", 0.35):
  385. for i in range(10):
  386. yield "."
  387. sign = CryptBitcoin.sign(data, privatekey)
  388. valid = "HFGXaDauZ8vX/N9Jn+MRiGm9h+I94zUhDnNYFaqMGuOi+4+BbWHjuwmx0EaKNV1G+kP0tQDxWu0YApxwxZbSmZU="
  389. assert sign == valid, "%s != %s" % (sign, valid)
  390. address = CryptBitcoin.privatekeyToAddress(privatekey)
  391. if CryptBitcoin.opensslVerify: # Openssl avalible
  392. with benchmark("openssl verify x 100", 0.37):
  393. for i in range(100):
  394. if i % 10 == 0:
  395. yield "."
  396. ok = CryptBitcoin.verify(data, address, sign)
  397. assert ok, "does not verify from %s" % address
  398. else:
  399. yield " - openssl verify x 100...not avalible :(<br>"
  400. openssl_verify_bk = CryptBitcoin.opensslVerify # Emulate openssl not found in any way
  401. CryptBitcoin.opensslVerify = None
  402. with benchmark("pure-python verify x 10", 1.6):
  403. for i in range(10):
  404. yield "."
  405. ok = CryptBitcoin.verify(data, address, sign)
  406. assert ok, "does not verify from %s" % address
  407. CryptBitcoin.opensslVerify = openssl_verify_bk
  408. # CryptHash
  409. yield "<br>CryptHash:<br>"
  410. from Crypt import CryptHash
  411. from cStringIO import StringIO
  412. data = StringIO("Hello" * 1024 * 1024) # 5m
  413. with benchmark("sha256 5M x 10", 0.6):
  414. for i in range(10):
  415. data.seek(0)
  416. hash = CryptHash.sha256sum(data)
  417. yield "."
  418. valid = "8cd629d9d6aff6590da8b80782a5046d2673d5917b99d5603c3dcb4005c45ffa"
  419. assert hash == valid, "%s != %s" % (hash, valid)
  420. data = StringIO("Hello" * 1024 * 1024) # 5m
  421. with benchmark("sha512 5M x 10", 0.6):
  422. for i in range(10):
  423. data.seek(0)
  424. hash = CryptHash.sha512sum(data)
  425. yield "."
  426. valid = "9ca7e855d430964d5b55b114e95c6bbb114a6d478f6485df93044d87b108904d"
  427. assert hash == valid, "%s != %s" % (hash, valid)
  428. with benchmark("os.urandom(256) x 100 000", 0.65):
  429. for i in range(10):
  430. for y in range(10000):
  431. data = os.urandom(256)
  432. yield "."
  433. # Msgpack
  434. yield "<br>Msgpack:<br>"
  435. import msgpack
  436. 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'
  437. data = {"int": 1024*1024*1024, "float": 12345.67890, "text": "hello"*1024, "binary": binary}
  438. with benchmark("pack 5K x 10 000", 0.78):
  439. for i in range(10):
  440. for y in range(1000):
  441. data_packed = msgpack.packb(data)
  442. yield "."
  443. 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"""
  444. assert data_packed == valid, "%s<br>!=<br>%s" % (repr(data_packed), repr(valid))
  445. with benchmark("unpack 5K x 10 000", 1.2):
  446. for i in range(10):
  447. for y in range(1000):
  448. data_unpacked = msgpack.unpackb(data_packed)
  449. yield "."
  450. assert data == data_unpacked, "%s != %s" % (data_unpack, data)
  451. with benchmark("streaming unpack 5K x 10 000", 1.4):
  452. for i in range(10):
  453. unpacker = msgpack.Unpacker()
  454. for y in range(1000):
  455. unpacker.feed(data_packed)
  456. for data_unpacked in unpacker:
  457. pass
  458. yield "."
  459. assert data == data_unpacked, "%s != %s" % (data_unpack, data)
  460. # Db
  461. yield "<br>Db:<br>"
  462. from Db import Db
  463. schema = {
  464. "db_name": "TestDb",
  465. "db_file": "%s/benchmark.db" % config.data_dir,
  466. "maps": {
  467. ".*": {
  468. "to_table": {
  469. "test": "test"
  470. }
  471. }
  472. },
  473. "tables": {
  474. "test": {
  475. "cols": [
  476. ["test_id", "INTEGER"],
  477. ["title", "TEXT"],
  478. ["json_id", "INTEGER REFERENCES json (json_id)"]
  479. ],
  480. "indexes": ["CREATE UNIQUE INDEX test_key ON test(test_id, json_id)"],
  481. "schema_changed": 1426195822
  482. }
  483. }
  484. }
  485. if os.path.isfile("%s/benchmark.db" % config.data_dir):
  486. os.unlink("%s/benchmark.db" % config.data_dir)
  487. with benchmark("Open x 10", 0.13):
  488. for i in range(10):
  489. db = Db(schema, "%s/benchmark.db" % config.data_dir)
  490. db.checkTables()
  491. db.close()
  492. yield "."
  493. db = Db(schema, "%s/benchmark.db" % config.data_dir)
  494. db.checkTables()
  495. import json
  496. with benchmark("Insert x 10 x 1000", 1.0):
  497. for u in range(10): # 10 user
  498. data = {"test": []}
  499. for i in range(1000): # 1000 line of data
  500. data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
  501. json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
  502. db.loadJson("%s/test_%s.json" % (config.data_dir, u))
  503. os.unlink("%s/test_%s.json" % (config.data_dir, u))
  504. yield "."
  505. with benchmark("Buffered insert x 100 x 100", 1.3):
  506. cur = db.getCursor()
  507. cur.execute("BEGIN")
  508. cur.logging = False
  509. for u in range(100, 200): # 100 user
  510. data = {"test": []}
  511. for i in range(100): # 1000 line of data
  512. data["test"].append({"test_id": i, "title": "Testdata for %s message %s" % (u, i)})
  513. json.dump(data, open("%s/test_%s.json" % (config.data_dir, u), "w"))
  514. db.loadJson("%s/test_%s.json" % (config.data_dir, u), cur=cur)
  515. os.unlink("%s/test_%s.json" % (config.data_dir, u))
  516. if u % 10 == 0:
  517. yield "."
  518. cur.execute("COMMIT")
  519. yield " - Total rows in db: %s<br>" % db.execute("SELECT COUNT(*) AS num FROM test").fetchone()[0]
  520. with benchmark("Indexed query x 1000", 0.25):
  521. found = 0
  522. cur = db.getCursor()
  523. cur.logging = False
  524. for i in range(1000): # 1000x by test_id
  525. res = cur.execute("SELECT * FROM test WHERE test_id = %s" % i)
  526. for row in res:
  527. found += 1
  528. if i % 100 == 0:
  529. yield "."
  530. assert found == 20000, "Found: %s != 20000" % found
  531. with benchmark("Not indexed query x 100", 0.6):
  532. found = 0
  533. cur = db.getCursor()
  534. cur.logging = False
  535. for i in range(100): # 1000x by test_id
  536. res = cur.execute("SELECT * FROM test WHERE json_id = %s" % i)
  537. for row in res:
  538. found += 1
  539. if i % 10 == 0:
  540. yield "."
  541. assert found == 18900, "Found: %s != 18900" % found
  542. with benchmark("Like query x 100", 1.8):
  543. found = 0
  544. cur = db.getCursor()
  545. cur.logging = False
  546. for i in range(100): # 1000x by test_id
  547. res = cur.execute("SELECT * FROM test WHERE title LIKE '%%message %s%%'" % i)
  548. for row in res:
  549. found += 1
  550. if i % 10 == 0:
  551. yield "."
  552. assert found == 38900, "Found: %s != 11000" % found
  553. db.close()
  554. if os.path.isfile("%s/benchmark.db" % config.data_dir):
  555. os.unlink("%s/benchmark.db" % config.data_dir)
  556. gc.collect() # Implicit grabage collection
  557. yield "<br>Done. Total: %.2fs" % (time.time() - t)
  558. def actionGcCollect(self):
  559. import gc
  560. self.sendHeader()
  561. yield str(gc.collect())