Skip to content

Instantly share code, notes, and snippets.

@alq666
Created November 28, 2015 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alq666/fee4d754305add822bf7 to your computer and use it in GitHub Desktop.
Save alq666/fee4d754305add822bf7 to your computer and use it in GitHub Desktop.
Micro-benchmarks for gauge deltas
import timeit
import random
acc = None
def sample():
value = random.random()
if value >= 0.5:
return "+" + str(value)
else:
return str(value)
#
def gauge_delta():
global acc
value = sample()
try:
if len(str(value)) > 0 and str(value)[0] in ('+'):
if acc is None:
acc = 0
acc += float(value)
else:
acc = float(value)
except ValueError:
raise Exception("Cannot parse gauge value")
def gauge():
value = sample()
try:
acc = int(value)
except ValueError:
try:
acc = float(value)
except ValueError:
raise Exception("Metric value must be a number")
def gauge_fast():
value = sample()
try:
acc = float(value)
except ValueError:
raise Exception("Metric value must be a number")
def main():
print "--- Gauge only ---"
print timeit.repeat("gauge()", "from __main__ import gauge")
print "--- Gauge delta ---"
print timeit.repeat("gauge_delta()", "from __main__ import gauge_delta, acc")
print "--- Gauge fast ---"
print timeit.repeat("gauge_fast()", "from __main__ import gauge_fast")
if __name__ == '__main__':
main()
@alq666
Copy link
Author

alq666 commented Nov 28, 2015

➜  dd-agent git:(alq666/gauge-delta) ✗ python microbench_gaugedelta.py
--- Gauge only ---
[3.3604989051818848, 3.368734121322632, 3.3440170288085938]
--- Gauge delta ---
[1.3949830532073975, 1.3639380931854248, 1.349412202835083]
--- Gauge fast ---
[0.9618160724639893, 1.0241508483886719, 1.0283880233764648]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment