# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import Pyro4
import time

class Data(object):
  def __init__(self, _iv = 0, _sv = ''):
    self.iv = _iv
    self.sv = _sv
    
def testFunctional(name):
    tst = Pyro4.Proxy(name)
    print(tst.add(123, 456))
    print(tst.dup('ABC'))
    d2 = tst.process(Data(123,'ABC'))
    print('%d %s' % (d2.iv,d2.sv))

def testInstantiation(name):
    tst = Pyro4.Proxy(name)
    for i in range(0, 2):
        print(tst.getCounter())
    tst = Pyro4.Proxy(name)
    for i in range(0, 2):
        print(tst.getCounter())

def testPerformance(name):
    tst = Pyro4.Proxy(name)
    REP = 10000
    t1 = time.time()
    for i in range(0, REP):
        tst.noop()
    t2 = time.time()
    print('%d requests per second' % (REP / (t2 - t1)))

Pyro4.config.SERIALIZER = 'dill'
Pyro4.config.NS_HOST = 'localhost'
Pyro4.config.NS_PORT = 9090

NAME = 'PYRONAME:Test'
testFunctional(NAME)
testInstantiation(NAME)
testPerformance(NAME)
