add download synchronization to base static experiment

This commit is contained in:
gmega 2024-11-03 08:39:19 -03:00
parent 221cb4955c
commit a5127da5f2
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
2 changed files with 28 additions and 8 deletions

View File

@ -19,17 +19,25 @@ class StaticDisseminationExperiment(Generic[TNetworkHandle, TInitialMetadata]):
def run(self):
sample = self.sampler(len(self.network.nodes))
seeder_idx = [next(sample) for _ in range(0, self.seeders)]
seeder_indexes = [next(sample) for _ in range(0, self.seeders)]
seeders, leechers = (
[self.network.nodes[i] for i in seeder_idx],
[self.network.nodes[i] for i in range(0, len(self.network.nodes)) if i not in seeder_idx]
[
self.network.nodes[i]
for i in seeder_indexes
],
[
self.network.nodes[i]
for i in range(0, len(self.network.nodes))
if i not in seeder_indexes
]
)
meta, data = self.generate_data()
handle = meta
handle = meta
for node in seeders:
handle = node.seed(data, handle)
for node in leechers:
node.leech(handle)
handles = [node.leech(handle) for node in leechers]
for handle in handles:
handle.await_for_completion()

View File

@ -2,7 +2,7 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Optional, List, Tuple, Union, Sequence
from benchmarks.core.network import FileSharingNetwork, Node
from benchmarks.core.network import FileSharingNetwork, Node, DownloadHandle
from benchmarks.core.utils import Sampler
from benchmarks.experiments.static_experiment import StaticDisseminationExperiment
@ -22,6 +22,7 @@ class MockNode(Node[MockHandle, str]):
def __init__(self) -> None:
self.seeding: Optional[Tuple[MockHandle, Path]] = None
self.leeching: Optional[MockHandle] = None
self.download_was_awaited = False
def seed(
self,
@ -37,6 +38,16 @@ class MockNode(Node[MockHandle, str]):
def leech(self, handle: MockHandle):
self.leeching = handle
return MockDownloadHandle(self)
class MockDownloadHandle(DownloadHandle):
def __init__(self, parent: MockNode) -> None:
self.parent = parent
def await_for_completion(self, timeout: float = 0) -> bool:
self.parent.download_was_awaited = True
return True
class MockFileSharingNetwork(FileSharingNetwork[MockHandle, str]):
@ -72,7 +83,7 @@ def test_should_place_seeders():
assert actual_seeders == set(seeder_indexes)
def test_should_place_leechers():
def test_should_download_at_remaining_nodes():
network = MockFileSharingNetwork(n=13)
file = Path('/path/to/data')
seeder_indexes = [9, 6, 3]
@ -92,6 +103,7 @@ def test_should_place_leechers():
assert node.leeching.path == file
assert node.leeching.name == 'data'
assert node.seeding is None
assert node.download_was_awaited
actual_leechers.add(index)
assert actual_leechers == set(range(13)) - set(seeder_indexes)