viff

changeset 765:563bcc37fe47

New test application that measures memory usage.
author Martin Geisler <mg@daimi.au.dk>
date Tue, 20 May 2008 14:13:33 +0200
parents 6377495664f8
children c8810dc507f3
files apps/gc-test.py
diffstat 1 files changed, 82 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/gc-test.py	Tue May 20 14:13:33 2008 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# Copyright 2008 VIFF Development Team.
     1.7 +#
     1.8 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.9 +#
    1.10 +# VIFF is free software: you can redistribute it and/or modify it
    1.11 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.12 +# published by the Free Software Foundation, either version 3 of the
    1.13 +# License, or (at your option) any later version.
    1.14 +#
    1.15 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.16 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.17 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.18 +# Public License for more details.
    1.19 +#
    1.20 +# You should have received a copy of the GNU Lesser General Public
    1.21 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.22 +
    1.23 +# This application will is meant for 3 players. They will run forever,
    1.24 +# sharing and summing integers. After 100 iterations the time per
    1.25 +# iteration and the current memory allocation is printed. The
    1.26 +# allocation should stay *constant* since shares are released at the
    1.27 +# same speed as they are allocated.
    1.28 +
    1.29 +import sys
    1.30 +import gc
    1.31 +from time import time
    1.32 +from pprint import pprint
    1.33 +
    1.34 +from twisted.internet import reactor
    1.35 +
    1.36 +from viff.field import GF
    1.37 +from viff.runtime import create_runtime
    1.38 +from viff.config import load_config
    1.39 +from viff.util import find_prime
    1.40 +
    1.41 +id, players = load_config(sys.argv[1])
    1.42 +
    1.43 +
    1.44 +def memory_usage():
    1.45 +    """Read memory usage of the current process."""
    1.46 +    status = None
    1.47 +    try:
    1.48 +        # This will only work on systems with a /proc file system
    1.49 +        # (like Linux).
    1.50 +        status = open('/proc/self/status', 'r')
    1.51 +        for line in status:
    1.52 +            if line.startswith('VmRSS'):
    1.53 +                parts = line.split()
    1.54 +                return parts[1]
    1.55 +        return 'unknown'
    1.56 +    finally:
    1.57 +        if status is not None:
    1.58 +            status.close()
    1.59 +
    1.60 +class Protocol:
    1.61 +
    1.62 +    def __init__(self, runtime):
    1.63 +        self.Zp = GF(find_prime(2**64))
    1.64 +        self.runtime = runtime
    1.65 +        self.last_time = time()
    1.66 +        self.share_next(0)
    1.67 +
    1.68 +    def share_next(self, n):
    1.69 +        if isinstance(n, self.Zp):
    1.70 +            n = n.value
    1.71 +        
    1.72 +        if n % 100 == 0:
    1.73 +            now = time()
    1.74 +            memory = memory_usage()
    1.75 +            print "Iteration %d: %.1f ms/iteration, allocated %s KiB" \
    1.76 +                  % (n, 10*(now - self.last_time), memory)
    1.77 +            self.last_time = now
    1.78 +
    1.79 +        x, y, z = self.runtime.shamir_share([1, 2, 3], self.Zp, n + 1)
    1.80 +        n = self.runtime.open(x + y - z)
    1.81 +        n.addCallback(self.share_next)
    1.82 +
    1.83 +pre_runtime = create_runtime(id, players, 1)
    1.84 +pre_runtime.addCallback(Protocol)
    1.85 +reactor.run()