Commit 4ab50afd authored by Alan Mitchell's avatar Alan Mitchell
Browse files

Class to post sensor readings.

parent 46bde4fc
......@@ -4,6 +4,8 @@
import threading
import time
import sys
import datetime
import random
import json
import yaml
......@@ -53,5 +55,72 @@ def results_time():
return tc/TRIES, load01/TRIES, load05/TRIES, load15/TRIES, dbsize/TRIES
class ReadingPoster(threading.Thread):
'''Posts sensor readings
'''
def __init__ (self, sensor_id, delay=0.0):
"""
'sensor_id': the sensor_id to use in the post
'delay': delay between reading posts. Randomness is added
to cause delay to vary from 0 to 2 x 'delay'
The timestamp used in each post is random. The value is always 1.0.
"""
# run constructor of base class
threading.Thread.__init__(self)
self.url = 'https://bmon.ahfctest.webfactional.com/readingdb/reading/%s/store/' % sensor_id
self.delay = delay
# If only thing left running are daemon threads, Python will exit.
self.daemon = True
# controls whether thread continues to post
self.stop = False
# counts number of readings posted
self.reading_count = 0
def stop_posting(self):
'''Call to stop thread from posting.
'''
self.stop = True
def run(self):
data = {'storeKey': 'StorageKey', 'val': 1.0}
dt_base = datetime.datetime.now()
while True:
if self.stop:
return
# delay a random amount of time that averages to the requested delay.
time.sleep(self.delay * 2.0 * random.random())
dt_new = dt_base + datetime.timedelta(seconds=random.random()*1e7)
data['ts'] = dt_new.strftime('%Y-%m-%d %H:%M:%S')
requests.get(self.url, params=data, verify=False)
self.reading_count += 1
if __name__ == '__main__':
print results_time()
#print results_time()
TOT_POST_TIME = 5.0 # seconds
posters = []
for i in range(5):
sensor_id = 'test_%03d' % i
poster = ReadingPoster(sensor_id, 0.0)
posters.append(poster)
poster.start()
time.sleep(TOT_POST_TIME)
total_reads = 0
for p in posters:
p.stop_posting()
total_reads += p.reading_count
print '%.1f posts / second' % (total_reads / TOT_POST_TIME)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment