/historical/hive_speed.py

https://bitbucket.org/lindenlab/apiary/ · Python · 129 lines · 83 code · 21 blank · 25 comment · 10 complexity · c27044b03fa0eaf660bd41535d3d67f4 MD5 · raw file

  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. import os
  26. import random
  27. import time
  28. import hive
  29. class StatWorker(hive.Worker):
  30. def __init__(self, options, arguments):
  31. hive.Worker.__init__(self, options, arguments)
  32. self._events = 0
  33. self._bytes = 0
  34. def start(self):
  35. self._events = 0
  36. self._bytes = 0
  37. def event(self, data):
  38. self._events += 1
  39. self._bytes += len(data)
  40. #time.sleep(0.01)
  41. def end(self):
  42. return ("%s,%d,%d" % (self._id, self._events, self._bytes))
  43. class StatCentral(hive.Central):
  44. def __init__(self, options, arguments):
  45. hive.Central.__init__(self, options, arguments)
  46. self.results = {}
  47. self._nseq = 2000
  48. self._nevents = 5
  49. self._nlen = 800
  50. self._seqcount = 0
  51. self._seqs = []
  52. self._seqevents = {}
  53. self._data = None
  54. def next(self):
  55. shy = self._seqcount < self._nseq
  56. empty = len(self._seqs) == 0
  57. if shy and (empty or random.randint(0,10) == 0):
  58. self._seqcount += 1
  59. seq = "Q%d" % (self._seqcount)
  60. self._seqs.append(seq)
  61. self._seqevents[seq] = 0
  62. self.start(seq)
  63. return True
  64. if empty:
  65. return False
  66. seq = random.choice(self._seqs)
  67. i = self._seqevents[seq]
  68. if i >= self._nevents:
  69. del self._seqevents[seq]
  70. self._seqs = self._seqevents.keys()
  71. self.end(seq)
  72. return True
  73. self._seqevents[seq] = i + 1
  74. self.event(seq, self.gendata())
  75. return True
  76. def result(self, seq, data):
  77. (worker, events, bytes) = data.split(',')
  78. if worker not in self.results:
  79. self.results[worker] = { 'sequences': 0, 'events': 0, 'bytes': 0 }
  80. r = self.results[worker]
  81. r['sequences'] += 1
  82. r['events'] += int(events)
  83. r['bytes'] += int(bytes)
  84. def gendata(self):
  85. if self._data is None:
  86. s = "abcdefghijklmnopqrstuvwxyz"
  87. while len(s) < self._nlen:
  88. s += s
  89. self._data = s[0:self._nlen]
  90. return self._data
  91. def main(self):
  92. t = - time.time()
  93. c = - time.clock()
  94. hive.Central.main(self)
  95. c += time.clock()
  96. t += time.time()
  97. print ("Timing: %f process clock, %f wall clock" % (c, t))
  98. print ("Central: %d sequences @ %d events @ %d bytes"
  99. % (self._seqcount, self._nevents, self._nlen))
  100. for k,r in self.results.iteritems():
  101. print ("Worker %s:" % k), (
  102. '%(sequences)d sequences @ %(events)d events @ %(bytes)d bytes'
  103. % r)
  104. class StatHive(hive.Hive):
  105. def __init__(self):
  106. hive.Hive.__init__(self, StatCentral, StatWorker)
  107. if __name__ == '__main__':
  108. StatHive().main()