forked from omergertel/pyformance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_pymetrics.py
94 lines (79 loc) · 3.07 KB
/
example_pymetrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import datetime
import os
import threading
import gc
import multiprocessing
try:
import resource
except ImportError:
resource = None
# windows
import psutil
from pyformance import global_registry
class Collector(object):
# TODO: use meters and histograms instead of gauges if possible
def __init__(self, registry=None):
if registry is None:
registry = global_registry()
self.registry = registry
self._memory_usage = 0
def collect_memory(self):
if resource:
usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
else:
process = psutil.Process(os.getpid())
usage = process.get_memory_info()[0] / float(2 ** 20)
increase = usage - self._memory_usage
self._memory_usage = usage
self.registry.gauge("python.memory.usage").set_value(usage)
self.registry.gauge("python.memory.increase").set_value(increase)
def collect_threads(self):
counter = 0
alive = 0
daemon = 0
for thread in threading.enumerate():
counter +=1
if thread.isDaemon():
daemon += 1
if thread.isAlive():
alive += 1
#switch_interval = sys.getcheckinterval()
self.registry.gauge("python.thread.count").set_value(counter)
self.registry.gauge("python.thread.daemon").set_value(daemon)
self.registry.gauge("python.thread.alive").set_value(alive)
def collect_garbage(self):
(count0, count1, count2) = gc.get_count()
(threshold0, threshold1, threshold2) = gc.get_threshold()
object_count = len(gc.get_objects())
referrers_count = len(gc.get_referrers())
referents_count = len(gc.get_referents())
self.registry.gauge("python.gc.collection.count0").set_value(count0)
self.registry.gauge("python.gc.collection.count1").set_value(count1)
self.registry.gauge("python.gc.collection.count2").set_value(count2)
self.registry.gauge("python.gc.objects.count").set_value(object_count)
self.registry.gauge("python.gc.referrers.count").set_value(referrers_count)
self.registry.gauge("python.gc.referents.count").set_value(referents_count)
def collect_processes(self):
counter = 0
alive = 0
daemon = 0
for proc in multiprocessing.active_children():
counter += 1
if proc.is_alive():
alive += 1
if proc.daemon:
daemon += 1
self.registry.gauge("python.processes.count").set_value(counter)
self.registry.gauge("python.processes.alive").set_value(alive)
self.registry.gauge("python.processes.daemon").set_value(daemon)
def collect(self):
self.collect_memory()
self.collect_garbage()
self.collect_threads()
self.collect_processes()
if __name__ == "__main__":
from pyformance.reporters import ConsoleReporter
reporter = ConsoleReporter()
col = Collector()
col.collect()
reporter.report_now()