Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / pbbenchserver.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 """Server for PB benchmark."""
6
7 from zope.interface import implements
8
9 from twisted.spread import pb
10 from twisted.internet import reactor
11 from twisted.cred.portal import IRealm
12
13 class PBBenchPerspective(pb.Avatar):
14     callsPerSec = 0
15     def __init__(self):
16         pass
17     
18     def perspective_simple(self):
19         self.callsPerSec = self.callsPerSec + 1
20         return None
21
22     def printCallsPerSec(self):
23         print '(s) cps:', self.callsPerSec
24         self.callsPerSec = 0
25         reactor.callLater(1, self.printCallsPerSec)
26
27     def perspective_complexTypes(self):
28         return ['a', 1, 1l, 1.0, [], ()]
29
30
31 class SimpleRealm:
32     implements(IRealm)
33
34     def requestAvatar(self, avatarId, mind, *interfaces):
35         if pb.IPerspective in interfaces:
36             p = PBBenchPerspective()
37             p.printCallsPerSec()
38             return pb.IPerspective, p, lambda : None
39         else:
40             raise NotImplementedError("no interface")
41
42
43 def main():
44     from twisted.cred.portal import Portal
45     from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse
46     portal = Portal(SimpleRealm())
47     checker = InMemoryUsernamePasswordDatabaseDontUse()
48     checker.addUser("benchmark", "benchmark")
49     portal.registerChecker(checker)
50     reactor.listenTCP(8787, pb.PBServerFactory(portal))
51     reactor.run()
52
53 if __name__ == '__main__':
54     main()