1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
8 import cStringIO as StringIO
13 from twisted.spread import banana
14 from twisted.internet import protocol
16 iterationCount = 10000
19 r = range( iterationCount )
20 def setUp(self, encClass):
21 self.io = StringIO.StringIO()
23 self.enc.makeConnection(protocol.FileWrapper(self.io))
24 self.enc._selectDialect("none")
25 self.enc.expressionReceived = self.putResult
27 def putResult(self, result):
31 self.enc.connectionLost()
34 def testEncode(self, value):
35 starttime = time.time()
37 self.enc.sendEncoded(value)
40 print ' Encode took %s seconds' % (endtime - starttime)
41 return endtime - starttime
43 def testDecode(self, value):
44 self.enc.sendEncoded(value)
45 encoded = self.io.getvalue()
46 starttime = time.time()
48 self.enc.dataReceived(encoded)
50 print ' Decode took %s seconds' % (endtime - starttime)
51 return endtime - starttime
53 def performTest(self, method, data, encClass):
58 def runTests(self, testData):
59 print 'Test data is: %s' % testData
60 print ' Using Pure Python Banana:'
61 self.performTest(self.testEncode, testData, banana.Banana)
62 self.performTest(self.testDecode, testData, banana.Banana)
65 print 'Doing %s iterations of each test.' % iterationCount
67 testData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
68 bench.runTests(testData)
69 testData = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
70 bench.runTests(testData)
71 testData = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
72 bench.runTests(testData)
73 testData = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
74 bench.runTests(testData)
75 testData = [1l, 2l, 3l, 4l, 5l, 6l, 7l, 8l, 9l, 10l]
76 bench.runTests(testData)
77 testData = [1, 2, [3, 4], [30.5, 40.2], 5, ["six", "seven", ["eight", 9]], [10], []]
78 bench.runTests(testData)