Created
April 18, 2017 16:09
-
-
Save SegFaultAX/d2d7f873cb9167963d8cd1c990f9825d to your computer and use it in GitHub Desktop.
Simulate flakey and flapping counters in Graphite to differentiate "max" and "last" aggregations [python]
This file contains hidden or 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 pprint | |
| import csv | |
| import random | |
| import functools | |
| import itertools | |
| def coroutine(f): | |
| @functools.wraps(f) | |
| def coro(*args, **kwargs): | |
| c = f(*args, **kwargs) | |
| next(c) | |
| def cb(*args, **kwargs): | |
| if not (args or kwargs): | |
| return next(c) | |
| else: | |
| return c.send(*args, **kwargs) | |
| return cb | |
| return coro | |
| @coroutine | |
| def service(uptime): | |
| while True: | |
| if random.random() < uptime: | |
| yield True | |
| else: | |
| yield False | |
| def counter_for(service, size=6, max_dist=10, reset=0): | |
| v = reset | |
| while True: | |
| ps = [] | |
| bucket_count = 0 | |
| for i in range(size): | |
| if not service(): | |
| v = reset | |
| inc = random.randint(0, max_dist) | |
| bucket_count += inc | |
| v += inc | |
| ps.append(v) | |
| yield ps, bucket_count | |
| NUM_BUCKETS = 1000 | |
| flappy_service = service(0.85) | |
| stable_service = service(0.99) | |
| flappy_counter = counter_for(flappy_service) | |
| flappy_points = list(itertools.islice(flappy_counter, NUM_BUCKETS)) | |
| stable_counter = counter_for(stable_service) | |
| stable_points = list(itertools.islice(stable_counter, NUM_BUCKETS)) | |
| def aggregate(points, aggfn): | |
| for bucket, bucket_count in points: | |
| yield aggfn(bucket) | |
| last = lambda e: e[-1] | |
| flappy_agg_max = aggregate(flappy_points, max) | |
| stable_agg_max = aggregate(stable_points, max) | |
| flappy_agg_last = aggregate(flappy_points, last) | |
| stable_agg_last = aggregate(stable_points, last) | |
| flappy_vals = zip(flappy_agg_max, flappy_agg_last) | |
| stable_vals = zip(stable_agg_max, stable_agg_last) | |
| with open("points.csv", "w") as fd: | |
| writer = csv.writer(fd) | |
| writer.writerow(("flappy",)) | |
| writer.writerow(("max", "last")) | |
| writer.writerows(flappy_vals) | |
| writer.writerow(("stable",)) | |
| writer.writerow(("max", "last")) | |
| writer.writerows(stable_vals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment