Merge lp:~verterok/txstatsd/py3ksupport into lp:txstatsd

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 108
Merged at revision: 106
Proposed branch: lp:~verterok/txstatsd/py3ksupport
Merge into: lp:txstatsd
Diff against target: 337 lines (+58/-39)
11 files modified
.bzrignore (+1/-0)
txstatsd/metrics/distinctmetric.py (+8/-2)
txstatsd/metrics/metric.py (+1/-1)
txstatsd/stats/uniformsample.py (+4/-3)
txstatsd/tests/metrics/test_distinct.py (+4/-3)
txstatsd/tests/metrics/test_histogrammetric.py (+1/-1)
txstatsd/tests/metrics/test_sli.py (+6/-2)
txstatsd/tests/test_httpinfo.py (+2/-2)
txstatsd/tests/test_metrics.py (+22/-21)
txstatsd/tests/test_router.py (+1/-1)
txstatsd/tests/test_service.py (+8/-3)
To merge this branch: bzr merge lp:~verterok/txstatsd/py3ksupport
Reviewer Review Type Date Requested Status
Sidnei da Silva Approve
Review via email: mp+152169@code.launchpad.net

Commit message

Initial py3k support, only the client side.

Description of the change

Initial py3k support, only the client side.

To post a comment you must log in.
Revision history for this message
Sidnei da Silva (sidnei) :
review: Approve
lp:~verterok/txstatsd/py3ksupport updated
106. By Guillermo Gonzalez

merge with trunk

107. By Guillermo Gonzalez

also support py2.6

108. By Guillermo Gonzalez

simplify version_info check

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file '.bzrignore'
--- .bzrignore 2011-07-07 03:29:34 +0000
+++ .bzrignore 2013-03-08 13:29:21 +0000
@@ -1,2 +1,3 @@
1_trial_temp*1_trial_temp*
2twisted/plugins/dropin.cache2twisted/plugins/dropin.cache
3__pycache__
34
=== modified file 'txstatsd/metrics/distinctmetric.py'
--- txstatsd/metrics/distinctmetric.py 2012-06-28 17:29:26 +0000
+++ txstatsd/metrics/distinctmetric.py 2013-03-08 13:29:21 +0000
@@ -28,8 +28,9 @@
28"""28"""
29import random29import random
30import time30import time
31import sys
3132
32from zope.interface import implements33from zope.interface import implementer, implements
3334
34from txstatsd.metrics.metric import Metric35from txstatsd.metrics.metric import Metric
35from txstatsd.itxstatsd import IMetric36from txstatsd.itxstatsd import IMetric
@@ -121,7 +122,8 @@
121 Keeps an estimate of the distinct numbers of items seen on various122 Keeps an estimate of the distinct numbers of items seen on various
122 sliding windows of time.123 sliding windows of time.
123 """124 """
124 implements(IMetric)125 if sys.version_info[0:2] == (2,6):
126 implements(IMetric)
125127
126 def __init__(self, name, wall_time_func=time.time, prefix=""):128 def __init__(self, name, wall_time_func=time.time, prefix=""):
127 """Construct a metric we expect to be periodically updated.129 """Construct a metric we expect to be periodically updated.
@@ -166,3 +168,7 @@
166 for item, value in items.iteritems():168 for item, value in items.iteritems():
167 metrics.append((self.prefix + self.name + item, value, timestamp))169 metrics.append((self.prefix + self.name + item, value, timestamp))
168 return metrics170 return metrics
171
172# if we are running anything >= 2.7
173if sys.version_info[0:2] >= (2,7):
174 DistinctMetricReporter = implementer(IMetric)(DistinctMetricReporter)
169175
=== modified file 'txstatsd/metrics/metric.py'
--- txstatsd/metrics/metric.py 2012-06-28 17:29:26 +0000
+++ txstatsd/metrics/metric.py 2013-03-08 13:29:21 +0000
@@ -64,4 +64,4 @@
64 def write(self, data):64 def write(self, data):
65 """Message the C{data} to the C{StatsD} server."""65 """Message the C{data} to the C{StatsD} server."""
66 if self.connection is not None:66 if self.connection is not None:
67 self.connection.write(data)67 self.connection.write(data.encode('utf-8'))
6868
=== modified file 'txstatsd/stats/uniformsample.py'
--- txstatsd/stats/uniformsample.py 2012-06-28 17:29:26 +0000
+++ txstatsd/stats/uniformsample.py 2013-03-08 13:29:21 +0000
@@ -27,7 +27,7 @@
27 """27 """
28 A random sample of a stream of values. Uses Vitter's Algorithm R to28 A random sample of a stream of values. Uses Vitter's Algorithm R to
29 produce a statistically representative sample.29 produce a statistically representative sample.
30 30
31 See:31 See:
32 - U{Random Sampling with a Reservoir32 - U{Random Sampling with a Reservoir
33 <http://www.cs.umd.edu/~samir/498/vitter.pdf>}33 <http://www.cs.umd.edu/~samir/498/vitter.pdf>}
@@ -42,7 +42,8 @@
42 self._values = [0 for i in range(reservoir_size)]42 self._values = [0 for i in range(reservoir_size)]
43 self._count = 043 self._count = 0
44 self.clear()44 self.clear()
45 45 self.maxint = getattr(sys, 'maxint', sys.maxsize)
46
46 def clear(self):47 def clear(self):
47 self._values = [0 for i in range(len(self._values))]48 self._values = [0 for i in range(len(self._values))]
48 self._count = 049 self._count = 0
@@ -56,7 +57,7 @@
56 if self._count <= len(self._values):57 if self._count <= len(self._values):
57 self._values[self._count - 1] = value58 self._values[self._count - 1] = value
58 else:59 else:
59 r = random.randint(1, sys.maxint) % self._count60 r = random.randint(1, self.maxint) % self._count
60 if r < len(self._values):61 if r < len(self._values):
61 self._values[r] = value62 self._values[r] = value
6263
6364
=== modified file 'txstatsd/tests/metrics/test_distinct.py'
--- txstatsd/tests/metrics/test_distinct.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/metrics/test_distinct.py 2013-03-08 13:29:21 +0000
@@ -18,6 +18,7 @@
18# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,18# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE19# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.20# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21from __future__ import print_function
2122
22import random23import random
2324
@@ -45,7 +46,7 @@
45 from scipy.stats import chi246 from scipy.stats import chi2
46 except ImportError:47 except ImportError:
47 raise SkipTest("Missing chi2, skipping")48 raise SkipTest("Missing chi2, skipping")
48 49
49 N = 1000050 N = 10000
5051
51 for (bits, buckets) in [(-1, 1024), (24, 256),52 for (bits, buckets) in [(-1, 1024), (24, 256),
@@ -61,7 +62,7 @@
61 value = sum(((x - N / buckets) ** 2) / (N / buckets) for x in bins)62 value = sum(((x - N / buckets) ** 2) / (N / buckets) for x in bins)
62 pval = chi2.cdf(value, N)63 pval = chi2.cdf(value, N)
63 if pval > 0.5:64 if pval > 0.5:
64 print bins, pval65 print(bins, pval)
65 self.assertTrue(pval < 0.5, "bits %s, pval == %s" % (bits, pval))66 self.assertTrue(pval < 0.5, "bits %s, pval == %s" % (bits, pval))
66 test_chi_square.skip = "Takes too long to run every time."67 test_chi_square.skip = "Takes too long to run every time."
6768
@@ -110,7 +111,7 @@
110111
111112
112class TestPlugin(TestCase):113class TestPlugin(TestCase):
113 114
114 def test_factory(self):115 def test_factory(self):
115 self.assertTrue(distinct_plugin.distinct_metric_factory in \116 self.assertTrue(distinct_plugin.distinct_metric_factory in \
116 list(getPlugins(IMetricFactory)))117 list(getPlugins(IMetricFactory)))
117118
=== modified file 'txstatsd/tests/metrics/test_histogrammetric.py'
--- txstatsd/tests/metrics/test_histogrammetric.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/metrics/test_histogrammetric.py 2013-03-08 13:29:21 +0000
@@ -102,7 +102,7 @@
102 histogram.update(i)102 histogram.update(i)
103103
104 hist = histogram.histogram()104 hist = histogram.histogram()
105 self.assertEquals(sum(hist), 10000)105 self.assertEqual(sum(hist), 10000)
106106
107 total = sum(hist)107 total = sum(hist)
108 binsize = int(total / len(hist))108 binsize = int(total / len(hist))
109109
=== modified file 'txstatsd/tests/metrics/test_sli.py'
--- txstatsd/tests/metrics/test_sli.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/metrics/test_sli.py 2013-03-08 13:29:21 +0000
@@ -20,8 +20,12 @@
20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE20# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.21# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
23import ConfigParser23try:
24from cStringIO import StringIO24 import ConfigParser
25 from StringIO import StringIO
26except ImportError:
27 import configparser as ConfigParser
28 from io import StringIO
25from twisted.trial.unittest import TestCase29from twisted.trial.unittest import TestCase
2630
27from twisted.plugins.sli_plugin import SLIMetricFactory31from twisted.plugins.sli_plugin import SLIMetricFactory
2832
=== modified file 'txstatsd/tests/test_httpinfo.py'
--- txstatsd/tests/test_httpinfo.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/test_httpinfo.py 2013-03-08 13:29:21 +0000
@@ -111,7 +111,7 @@
111 def test_httpinfo_error(self):111 def test_httpinfo_error(self):
112 try:112 try:
113 data = yield self.get_results("status", last_flush_duration=30)113 data = yield self.get_results("status", last_flush_duration=30)
114 except HttpException, e:114 except HttpException as e:
115 self.assertEquals(e.response.code, 500)115 self.assertEquals(e.response.code, 500)
116 else:116 else:
117 self.fail("Not 500")117 self.fail("Not 500")
@@ -121,7 +121,7 @@
121 try:121 try:
122 data = yield self.get_results("metrics/gorets",122 data = yield self.get_results("metrics/gorets",
123 timer_metrics={'gorets': 100})123 timer_metrics={'gorets': 100})
124 except HttpException, e:124 except HttpException as e:
125 self.assertEquals(e.response.code, 404)125 self.assertEquals(e.response.code, 404)
126 else:126 else:
127 self.fail("Not 404")127 self.fail("Not 404")
128128
=== modified file 'txstatsd/tests/test_metrics.py'
--- txstatsd/tests/test_metrics.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/test_metrics.py 2013-03-08 13:29:21 +0000
@@ -52,28 +52,29 @@
52 """Test reporting of a gauge metric sample."""52 """Test reporting of a gauge metric sample."""
53 self.metrics.gauge('gauge', 102)53 self.metrics.gauge('gauge', 102)
54 self.assertEqual(self.connection.data,54 self.assertEqual(self.connection.data,
55 'txstatsd.tests.gauge:102|g')55 b'txstatsd.tests.gauge:102|g')
5656
57 def test_meter(self):57 def test_meter(self):
58 """Test reporting of a meter metric sample."""58 """Test reporting of a meter metric sample."""
59 self.metrics.meter('meter', 3)59 self.metrics.meter('meter', 3)
60 self.assertEqual(self.connection.data,60 self.assertEqual(self.connection.data,
61 'txstatsd.tests.meter:3|m')61 b'txstatsd.tests.meter:3|m')
6262
63 def test_counter(self):63 def test_counter(self):
64 """Test the increment and decrement operations."""64 """Test the increment and decrement operations."""
65 self.metrics.increment('counter', 18)65 self.metrics.increment('counter', 18)
66 self.assertEqual(self.connection.data,66 self.assertEqual(self.connection.data,
67 'txstatsd.tests.counter:18|c')67 b'txstatsd.tests.counter:18|c')
68 self.metrics.decrement('counter', 9)68 self.metrics.decrement('counter', 9)
69 self.assertEqual(self.connection.data,69 self.assertEqual(self.connection.data,
70 'txstatsd.tests.counter:-9|c')70 b'txstatsd.tests.counter:-9|c')
7171
72 def test_timing(self):72 def test_timing(self):
73 """Test the timing operation."""73 """Test the timing operation."""
74 self.metrics.timing('timing', 101.1234)74 self.metrics.timing('timing', 101.1234)
75 self.assertEqual(self.connection.data,75 # to support both 2 and 3, can't use assertRegex
76 'txstatsd.tests.timing:101123.4|ms')76 match = re.match(b'txstatsd.tests.timing:101123.4[0-9]*|ms', self.connection.data)
77 self.assertFalse(match is None)
7778
78 def test_timing_automatic(self):79 def test_timing_automatic(self):
79 """Test the automatic timing operation with explicit reset"""80 """Test the automatic timing operation with explicit reset"""
@@ -85,9 +86,9 @@
8586
86 elapsed = time.time() - start_time87 elapsed = time.time() - start_time
8788
88 label, val, units = re.split(":|\|", self.connection.data)89 label, val, units = re.split(b":|\|", self.connection.data)
89 self.assertEqual(label, 'txstatsd.tests.timing')90 self.assertEqual(label, b'txstatsd.tests.timing')
90 self.assertEqual(units, 'ms')91 self.assertEqual(units, b'ms')
91 self.assertTrue(100 <= float(val) <= elapsed * 1000)92 self.assertTrue(100 <= float(val) <= elapsed * 1000)
9293
93 def test_timing_automatic_implicit_reset(self):94 def test_timing_automatic_implicit_reset(self):
@@ -100,34 +101,34 @@
100101
101 elapsed = time.time() - start_time102 elapsed = time.time() - start_time
102103
103 label, val, units = re.split(":|\|", self.connection.data)104 label, val, units = re.split(b":|\|", self.connection.data)
104 self.assertEqual(label, 'txstatsd.tests.timing')105 self.assertEqual(label, b'txstatsd.tests.timing')
105 self.assertEqual(units, 'ms')106 self.assertEqual(units, b'ms')
106 self.assertTrue(100 <= float(val) <= elapsed * 1000)107 self.assertTrue(100 <= float(val) <= elapsed * 1000)
107108
108 def test_generic(self):109 def test_generic(self):
109 """Test the GenericMetric class."""110 """Test the GenericMetric class."""
110 self.metrics.report('users', "pepe", "pd")111 self.metrics.report('users', "pepe", "pd")
111 self.assertEqual(self.connection.data,112 self.assertEqual(self.connection.data,
112 'txstatsd.tests.users:pepe|pd')113 b'txstatsd.tests.users:pepe|pd')
113114
114 def test_generic_extra(self):115 def test_generic_extra(self):
115 """Test the GenericMetric class."""116 """Test the GenericMetric class."""
116 self.metrics.report('users', "pepe", "pd", 100)117 self.metrics.report('users', "pepe", "pd", 100)
117 self.assertEqual(self.connection.data,118 self.assertEqual(self.connection.data,
118 'txstatsd.tests.users:pepe|pd|100')119 b'txstatsd.tests.users:pepe|pd|100')
119120
120 def test_empty_namespace(self):121 def test_empty_namespace(self):
121 """Test reporting of an empty namespace."""122 """Test reporting of an empty namespace."""
122 self.metrics.namespace = None123 self.metrics.namespace = None
123 self.metrics.gauge('gauge', 213)124 self.metrics.gauge('gauge', 213)
124 self.assertEqual(self.connection.data,125 self.assertEqual(self.connection.data,
125 'gauge:213|g')126 b'gauge:213|g')
126127
127 self.metrics.namespace = ''128 self.metrics.namespace = ''
128 self.metrics.gauge('gauge', 413)129 self.metrics.gauge('gauge', 413)
129 self.assertEqual(self.connection.data,130 self.assertEqual(self.connection.data,
130 'gauge:413|g')131 b'gauge:413|g')
131132
132133
133class TestExtendedMetrics(TestMetrics):134class TestExtendedMetrics(TestMetrics):
@@ -139,21 +140,21 @@
139 """Test the increment and decrement operations."""140 """Test the increment and decrement operations."""
140 self.metrics.increment('counter', 18)141 self.metrics.increment('counter', 18)
141 self.assertEqual(self.connection.data,142 self.assertEqual(self.connection.data,
142 'txstatsd.tests.counter:18|c')143 b'txstatsd.tests.counter:18|c')
143 self.metrics.decrement('counter', 9)144 self.metrics.decrement('counter', 9)
144 self.assertEqual(self.connection.data,145 self.assertEqual(self.connection.data,
145 'txstatsd.tests.counter:9|c')146 b'txstatsd.tests.counter:9|c')
146147
147 def test_sli(self):148 def test_sli(self):
148 """Test SLI call."""149 """Test SLI call."""
149 self.metrics.sli('users', 100)150 self.metrics.sli('users', 100)
150 self.assertEqual(self.connection.data,151 self.assertEqual(self.connection.data,
151 'txstatsd.tests.users:100|sli')152 b'txstatsd.tests.users:100|sli')
152153
153 self.metrics.sli('users', 200, 2)154 self.metrics.sli('users', 200, 2)
154 self.assertEqual(self.connection.data,155 self.assertEqual(self.connection.data,
155 'txstatsd.tests.users:200|sli|2')156 b'txstatsd.tests.users:200|sli|2')
156157
157 self.metrics.sli_error('users')158 self.metrics.sli_error('users')
158 self.assertEqual(self.connection.data,159 self.assertEqual(self.connection.data,
159 'txstatsd.tests.users:error|sli')160 b'txstatsd.tests.users:error|sli')
160161
=== modified file 'txstatsd/tests/test_router.py'
--- txstatsd/tests/test_router.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/test_router.py 2013-03-08 13:29:21 +0000
@@ -179,7 +179,7 @@
179179
180 class Collect(DatagramProtocol):180 class Collect(DatagramProtocol):
181181
182 def datagramReceived(cself, data, (host, port)):182 def datagramReceived(cself, data, host_port):
183 self.got_data(data)183 self.got_data(data)
184184
185 self.port = reactor.listenUDP(0, Collect())185 self.port = reactor.listenUDP(0, Collect())
186186
=== modified file 'txstatsd/tests/test_service.py'
--- txstatsd/tests/test_service.py 2012-06-28 17:29:26 +0000
+++ txstatsd/tests/test_service.py 2013-03-08 13:29:21 +0000
@@ -20,8 +20,12 @@
20# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.20# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
22import tempfile22import tempfile
23import ConfigParser23try:
24from StringIO import StringIO24 import ConfigParser
25 from StringIO import StringIO
26except ImportError:
27 import configparser as ConfigParser
28 from io import StringIO
2529
26from twisted.trial.unittest import TestCase30from twisted.trial.unittest import TestCase
2731
@@ -203,7 +207,8 @@
203 def __init__(self):207 def __init__(self):
204 self.monitor_response = None208 self.monitor_response = None
205209
206 def datagramReceived(self, data, (host, port)):210 def datagramReceived(self, data, host_port):
211 host, port = host_port
207 self.monitor_response = data212 self.monitor_response = data
208213
209214

Subscribers

People subscribed via source and target branches