Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility functions to obtain elapsed time and communication cost #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions mpyc/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ async def start(self):
server.close()
self.start_time = time.time()

def elapsed_time(self):
"""Return the elapsed time since the MPyC runtime started."""
return time.time() - self.start_time

def communication_cost(self, per_user=False):
"""Return the number of bytes exchanged since the MPyC runtime started.

If the per_user parameter is True, the function returns a list with the cost per user.
Otherwise, all costs are aggregated.
"""
per_user_cost = [peer.protocol.nbytes_sent if peer.pid != self.pid else 0 for peer in self.parties]

if per_user:
return per_user_cost
else:
return sum(per_user_cost)



async def shutdown(self):
"""Shutdown the MPyC runtime.

Expand All @@ -267,8 +286,8 @@ async def shutdown(self):
# Wait for all parties behind a barrier.
while self._pc_level > self._program_counter[1]:
await asyncio.sleep(0)
elapsed = time.time() - self.start_time
nbytes = [peer.protocol.nbytes_sent if peer.pid != self.pid else 0 for peer in self.parties]
elapsed = self.elapsed_time()
nbytes = self.communication_cost(per_user=True)
elapsed = datetime.timedelta(seconds=round(elapsed*1000)/1000) # round to milliseconds
logging.info(f'Stop MPyC -- elapsed time: {str(elapsed)[:-3]}|bytes sent: {sum(nbytes)}')
logging.debug(f'Bytes sent per party: {" ".join(map(str, nbytes))}')
Expand Down
4 changes: 4 additions & 0 deletions tests/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,10 @@ def test_misc(self):
cs_f = lambda b, i: [b * (2*i+1) + i**2, (b*2+1) * 3**i]
self.assertEqual(mpc.run(mpc.output(mpc.find(x, 2, bits=False, cs_f=cs_f))), [4, 9])

def test_utils(self):
self.assertEqual(mpc.communication_cost(), 0)
self.assertEqual(mpc.communication_cost(per_user=True), [0])
self.assertGreater(mpc.elapsed_time(), 0)

if __name__ == "__main__":
unittest.main()