fire_fake.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- encoding: utf-8 -**
  2. from __future__ import absolute_import, print_function
  3. # Import system libs
  4. import sys
  5. import datetime
  6. # Import salt libs
  7. import salt.config
  8. import salt.client.raet
  9. try:
  10. opts = salt.config.master_config('/etc/salt/master')
  11. except OSError:
  12. print('Could not open master config. Do you need to be root?')
  13. sys.exit(1)
  14. class SwarmController(object):
  15. '''
  16. A controlling class for instantiating and controlling worker procs
  17. '''
  18. def __init__(self, opts):
  19. self.opts = opts
  20. self.client = salt.client.raet.LocalClient(mopts=opts)
  21. self.total_complete = 0
  22. self.run_time = 90 # The number of seconds to run for
  23. self.reqs_sec = 5000 # The number of requests / second to shoot for
  24. self.granularity = 200 # Re-calibrate once for this many runs
  25. self.period_sleep = 0.01 # The number of seconds to initially sleep between pubs
  26. self.ramp_sleep = 0.001 # The number of seconds to ramp up up or down by per calibration
  27. self.start_time = None # The timestamp for the initiation of the test run
  28. def run(self):
  29. '''
  30. Run the sequence in a loop
  31. '''
  32. last_check = 0
  33. self.start_time = datetime.datetime.now()
  34. goal = self.reqs_sec * self.run_time
  35. while True:
  36. self.fire_it()
  37. last_check += 1
  38. if last_check > self.granularity:
  39. self.calibrate()
  40. last_check = 0
  41. if self.total_complete > goal:
  42. print('Test complete')
  43. break
  44. def fire_it(self):
  45. '''
  46. Send the pub!
  47. '''
  48. self.client.pub('silver', 'test.ping')
  49. self.total_complete += 1
  50. def calibrate(self):
  51. '''
  52. Re-calibrate the speed
  53. '''
  54. elapsed_time = datetime.datetime.now() - self.start_time
  55. #remaining_time = self.run_time - elapsed_time
  56. #remaining_requests = (self.reqs_sec * self.run_time) - self.total_complete
  57. # Figure out what the reqs/sec has been up to this point and then adjust up or down
  58. runtime_reqs_sec = self.total_complete / elapsed_time.total_seconds()
  59. print('Recalibrating. Current reqs/sec: {0}'.format(runtime_reqs_sec))
  60. return
  61. controller = SwarmController(opts)
  62. controller.run()