Ondrej Kocian 10 år sedan
förälder
incheckning
20de7be139
4 ändrade filer med 1507 tillägg och 0 borttagningar
  1. 11
    0
      setup.py
  2. 0
    0
      szn-qalibs/__init__.py
  3. 109
    0
      szn-qalibs/bottleneck.py
  4. 1387
    0
      szn-qalibs/hoover.py

+ 11
- 0
setup.py Visa fil

@@ -0,0 +1,11 @@
1
+#!/usr/bin/env python
2
+from distutils.core import setup
3
+
4
+setup(name='szn-qalibs',
5
+      version='1.0',
6
+      description='collection of python libs developed for testing purposes',
7
+      author='Seznam.cz, a.s.',
8
+      author_email='opensource@firma.seznam.cz',
9
+      url='https://github.com/seznam/szn-qalibs',
10
+      packages=['szn-qalibs',],
11
+      )

+ 0
- 0
szn-qalibs/__init__.py Visa fil


+ 109
- 0
szn-qalibs/bottleneck.py Visa fil

@@ -0,0 +1,109 @@
1
+import time
2
+
3
+
4
+class FrameState(object):
5
+    """Abstraction and tracking of frame state"""
6
+
7
+    def __init__(self, max_load, size, debug):
8
+        self.start = 0
9
+        self.pos = 0
10
+        self.load = 0
11
+        self.allows = None
12
+        self.time_ok = None
13
+        self.load_ok = None
14
+        self.MAX_LOAD = max_load
15
+        self.SIZE = size
16
+        self.DEBUG_MODE = debug
17
+        self.__reset()
18
+
19
+    def __reset(self):
20
+        self.start = time.time()
21
+        self.pos = 0
22
+        self.load = 0
23
+        self.allows = True
24
+
25
+    def __update(self):
26
+        self.pos = time.time() - self.start
27
+        self.time_ok = self.pos <= self.SIZE
28
+        self.load_ok = self.load <= self.MAX_LOAD - 1
29
+
30
+        if self.time_ok and self.load_ok:
31
+            self.allows = True
32
+        elif not self.time_ok:
33
+            self.__reset()
34
+        elif not self.load_ok:
35
+            self.allows = False
36
+
37
+    def debug(self):
38
+        if self.DEBUG_MODE:
39
+            print("%4d; %4d; %5s; %0.3f; %0.3f; %5s; %5s"
40
+                  % (self.load, self.MAX_LOAD, self.load_ok,
41
+                     self.pos, self.SIZE, self.time_ok,
42
+                     self.allows))
43
+
44
+    def is_closed(self):
45
+        return not self.is_open()
46
+
47
+    def is_open(self):
48
+        self.__update()
49
+        if self.allows:
50
+            self.load += 1
51
+        return self.allows
52
+
53
+
54
+class Throttle(object):
55
+    """Throttle to allow only certain amount of iteration per given time.
56
+
57
+    Usage:
58
+
59
+        t = bottleneck.Throttle(300)
60
+
61
+        while True:
62
+            call_a_load_sensitive_service()
63
+            t.wait()        # ensures above loop will not be called more
64
+                            # than 300 times within 1 minute
65
+
66
+        t = bottleneck.Throttle(10, 1)
67
+
68
+        while True:
69
+            call_a_load_sensitive_service()
70
+            t.wait()        # ensures above loop will not be called more
71
+                            # than 10 times within 1 second
72
+
73
+    Note that the class will not in any way guarantee any even distribution
74
+    of calls in time.  If your loop takes 1ms and you throttle to 1000 loops
75
+    per 10 minutes, all loops will happen in the first second, and the last
76
+    call will block for 599 seconds.
77
+
78
+    """
79
+
80
+    def __init__(self, max_load, frame_size=60, debug=False):
81
+        """Create new Throttle.
82
+
83
+        Only required parameter is `max_load`, which is number of times per
84
+        frame `Throttle.wait()` returns without blocking.  Optionally you can
85
+        specify `frame_size` in seconds, which defaults to 60, and debug,
86
+        which when true, causes printing of some debugging info.
87
+        """
88
+
89
+        self.max_load = max_load
90
+        self.frame_size = frame_size
91
+        self.debug = debug
92
+        self.waiting = True
93
+        self.frame = FrameState(max_load=self.max_load, size=self.frame_size,
94
+                                debug=self.debug)
95
+
96
+    def is_closed(self):
97
+        """True if throttle is closed."""
98
+        return self.frame.is_closed()
99
+
100
+    def is_open(self):
101
+        """True if throttle is open."""
102
+        return self.frame.is_open()
103
+
104
+    def wait(self):
105
+        """Return now if throttle is open, otherwise block until it is."""
106
+        self.frame.debug()
107
+        self.waiting = self.is_closed()
108
+        while self.waiting:
109
+            self.waiting = self.is_closed()

+ 1387
- 0
szn-qalibs/hoover.py
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil