__init__.py 1.62 KB
Newer Older
Mario Hock's avatar
Mario Hock committed
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import psutil
import time


## XXX for interactive debugging only
def RELOAD():
    print ("import importlib")
    print ("importlib.reload(cpunetlog)")


MEASUREMENT_INTERVAL = 0.2

class Measurement:
    last_measurement = None

    @staticmethod
    def get_time():
        """ Unified/comparable clock access """
        return time.time()

    @classmethod
    def update_last_measurement(cls, t):
        cls.last_measurement = t

    ## Exceptions
    class TaintedResultsException(Exception):
        pass


    ## ***
    def __init__(self):
        ## sanity check
        if ( self.last_measurement ):
            if ( self.get_time() - self.last_measurement < 0.09 ):
                raise self.TaintedResultsException

        ## timing
        t = self.get_time()

        ## * measurements *
        self.cpu = psutil.cpu_percent(interval=0, percpu=True)

        ## timing
        t2 = time.time()
        assert( t2-t < 0.001 )   ## XXX should be fast!!

        ## store timespan for these statistics (if reasonable)
        if ( self.last_measurement ):
            self.timespan = t - self.last_measurement
            self.valid = True
        else:
            self.timespan = None
            self.valid = False

        ## update class variable
        self.update_last_measurement(t)

    def __str__(self):
        return "Timespan: " + str(self.timespan) + "; CPU: " + str(self.cpu)



## XXX for interactive debugging only
def test():
    print( Measurement() )


## init
init_measure = Measurement()

# measure
time.sleep(MEASUREMENT_INTERVAL)
m1 = Measurement()

## show
print( m1 )