Created
November 19, 2015 19:17
-
-
Save nimbusscale/67dc8ce54786fc3c847a to your computer and use it in GitHub Desktop.
writes as many keys as it can to a specified prefix within a specified number of seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import datetime as dt | |
import hashlib | |
import json | |
import requests | |
import socket | |
import time | |
import consul | |
parser = argparse.ArgumentParser( | |
description = 'benchmark tool for consul') | |
parser.add_argument( | |
'--duration', '-d', | |
required = True, | |
type = int, | |
help = "home long to run the test for" | |
) | |
parser.add_argument( | |
'--prefix', '-p', | |
required = True, | |
help = "prefix prepended to all keys written by the test" | |
) | |
args = parser.parse_args() | |
test_prefix = args.prefix | |
hostname = socket.gethostname() | |
duration = args.duration | |
def write_keys_benchmark(key_prefix,duration): | |
"""writes as many keys as it can in the alloted times and | |
returns a dict with the results""" | |
c = consul.Consul() | |
time_end = time.time() + duration | |
key_counter = 0 | |
true_counter = 0 | |
false_counter = 0 | |
start = dt.datetime.now() | |
while time.time() < time_end: | |
key = key_prefix + "/key_" + str(key_counter) | |
value = hashlib.sha1(key).hexdigest() | |
write_result = c.kv.put(key, value) | |
if write_result == True: | |
true_counter += 1 | |
elif write_result == False: | |
false_counter += 1 | |
key_counter += 1 | |
end = dt.datetime.now() | |
return_dict = {} | |
return_dict['true'] = true_counter | |
return_dict['false'] = false_counter | |
return_dict['start'] = str(start) | |
return_dict['end'] = str(end) | |
return return_dict | |
def main(): | |
key_prefix = test_prefix + hostname | |
results = write_keys_benchmark(key_prefix,duration) | |
results_json = json.dumps(results) | |
print(results_json) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment