瀏覽代碼

Stresstests: Show human numbers/sizes

Ask Solem 12 年之前
父節點
當前提交
c2c71dbf20
共有 3 個文件被更改,包括 32 次插入11 次删除
  1. 18 1
      celery/utils/debug.py
  2. 14 3
      funtests/stress/stress.py
  3. 0 7
      funtests/stress/stress_data.py

+ 18 - 1
celery/utils/debug.py

@@ -106,8 +106,25 @@ def sample(x, n, k=0):
         k += j
         k += j
 
 
 
 
+UNITS = (
+    (2 ** 40.0, 'TB'),
+    (2 ** 30.0, 'GB'),
+    (2 ** 20.0, 'MB'),
+    (2 ** 10.0, 'kB'),
+    (0.0, '{0!d}b'),
+)
+
+
+def hfloat(f, p=5):
+    i = int(f)
+    return i if i == f else '{0:.{p}}'.format(f, p=p)
+
+
 def humanbytes(s):
 def humanbytes(s):
-    return '{0}MB'.format(format_d(s // 1024))
+    return next(
+        hfloat(s / div if div else s) + unit
+        for div, unit in UNITS if s >= div
+    )
 
 
 
 
 def mem_rss():
 def mem_rss():

+ 14 - 3
funtests/stress/stress.py

@@ -17,8 +17,9 @@ from celery import Celery, group, VERSION_BANNER
 from celery.bin.base import Command, Option
 from celery.bin.base import Command, Option
 from celery.exceptions import TimeoutError, SoftTimeLimitExceeded
 from celery.exceptions import TimeoutError, SoftTimeLimitExceeded
 from celery.five import range, values
 from celery.five import range, values
-from celery.utils.debug import blockdetection
+from celery.utils.debug import blockdetection, humanbytes
 from celery.utils.text import pluralize
 from celery.utils.text import pluralize
+from celery.utils.timeutils import humanize_seconds
 
 
 # Should be run with workers running using these options:
 # Should be run with workers running using these options:
 #
 #
@@ -34,7 +35,17 @@ from celery.utils.text import pluralize
 #
 #
 #  7) celery -A stress worker -c1 --maxtasksperchild=1 -- celery.acks_late=1
 #  7) celery -A stress worker -c1 --maxtasksperchild=1 -- celery.acks_late=1
 
 
-from stress_data import Data
+class Data(object):
+
+    def __init__(self, label, data):
+        self.label = label
+        self.data = data
+
+    def __str__(self):
+        return '<Data: {0} {1}>'.format(
+            self.label, humanbytes(len(self.data)),
+        )
+    __unicode__ = __repr__ = __str__
 
 
 BIG = Data("BIG", 'x' * 2 ** 20 * 8)
 BIG = Data("BIG", 'x' * 2 ** 20 * 8)
 SMALL = Data("SMALL", 'e' * 1024)
 SMALL = Data("SMALL", 'e' * 1024)
@@ -241,7 +252,7 @@ class Suite(object):
             finally:
             finally:
                 print('{0} {1} iterations in {2}s'.format(
                 print('{0} {1} iterations in {2}s'.format(
                     'failed after' if failed else 'completed',
                     'failed after' if failed else 'completed',
-                    i + 1, time() - t,
+                    i + 1, humanize_seconds(time() - t),
                 ))
                 ))
 
 
     def termbysig(self):
     def termbysig(self):

+ 0 - 7
funtests/stress/stress_data.py

@@ -1,7 +0,0 @@
-class Data(object):
-    def __init__(self, label, data):
-        self.label = label
-        self.data = data
-    def __str__(self):
-        return 'Data(%s)' % self.label
-    __unicode__ = __repr__ = __str__