~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/bananabench.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
 
 
5
import sys
 
6
import time
 
7
try:
 
8
    import cStringIO as StringIO
 
9
except ImportError:
 
10
    import StringIO
 
11
    
 
12
# Twisted Imports
 
13
from twisted.spread import banana
 
14
from twisted.internet import protocol
 
15
 
 
16
iterationCount = 10000
 
17
 
 
18
class BananaBench:
 
19
    r = range( iterationCount )
 
20
    def setUp(self, encClass):
 
21
        self.io = StringIO.StringIO()
 
22
        self.enc = encClass()
 
23
        self.enc.makeConnection(protocol.FileWrapper(self.io))
 
24
        self.enc._selectDialect("none")
 
25
        self.enc.expressionReceived = self.putResult
 
26
 
 
27
    def putResult(self, result):
 
28
        self.result = result
 
29
 
 
30
    def tearDown(self):
 
31
        self.enc.connectionLost()
 
32
        del self.enc
 
33
 
 
34
    def testEncode(self, value):
 
35
        starttime = time.time()
 
36
        for i in self.r:
 
37
            self.enc.sendEncoded(value)
 
38
            self.io.truncate(0)
 
39
        endtime = time.time()
 
40
        print '    Encode took %s seconds' % (endtime - starttime)
 
41
        return endtime - starttime
 
42
 
 
43
    def testDecode(self, value):
 
44
        self.enc.sendEncoded(value)
 
45
        encoded = self.io.getvalue()
 
46
        starttime = time.time()
 
47
        for i in self.r:
 
48
            self.enc.dataReceived(encoded)
 
49
        endtime = time.time()
 
50
        print '    Decode took %s seconds' % (endtime - starttime)
 
51
        return endtime - starttime
 
52
 
 
53
    def performTest(self, method, data, encClass):
 
54
        self.setUp(encClass)
 
55
        method(data)
 
56
        self.tearDown()
 
57
 
 
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)
 
63
 
 
64
bench = BananaBench()
 
65
print 'Doing %s iterations of each test.' % iterationCount
 
66
print ''
 
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)
 
79