3 # NEPI, a framework to manage network experiments
4 # Copyright (C) 2013 INRIA
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 2 as
8 # published by the Free Software Foundation;
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.ec import ExperimentController
21 from nepi.execution.resource import ResourceManager, ResourceState, \
22 clsinit_copy, ResourceAction, ResourceFactory
23 from nepi.execution.runner import ExperimentRunner
32 reschedule_delay = "0.5s"
36 class Link(ResourceManager):
37 _rtype = "dummy::Link"
39 time.sleep(deploy_time)
40 super(Link, self).do_deploy()
41 self.logger.debug(" -------- DEPLOYED ------- ")
43 class Interface(ResourceManager):
44 _rtype = "dummy::Interface"
47 node = self.get_connected(Node.get_rtype())[0]
48 link = self.get_connected(Link.get_rtype())[0]
50 if node.state < ResourceState.READY or \
51 link.state < ResourceState.READY:
52 self.ec.schedule(reschedule_delay, self.deploy)
53 self.logger.debug(" -------- RESCHEDULING ------- ")
55 time.sleep(deploy_time)
56 super(Interface, self).do_deploy()
57 self.logger.debug(" -------- DEPLOYED ------- ")
59 class Node(ResourceManager):
60 _rtype = "dummy::Node"
63 self.logger.debug(" -------- DO_DEPLOY ------- ")
64 time.sleep(deploy_time)
65 super(Node, self).do_deploy()
66 self.logger.debug(" -------- DEPLOYED ------- ")
68 class Application(ResourceManager):
69 _rtype = "dummy::Application"
72 node = self.get_connected(Node.get_rtype())[0]
74 if node.state < ResourceState.READY:
75 self.ec.schedule(reschedule_delay, self.deploy)
76 self.logger.debug(" -------- RESCHEDULING ------- ")
78 time.sleep(deploy_time)
79 super(Application, self).do_deploy()
80 self.logger.debug(" -------- DEPLOYED ------- ")
83 super(Application, self).do_start()
85 self.ec.schedule("0s", self.stop)
87 ResourceFactory.register_type(Application)
88 ResourceFactory.register_type(Node)
89 ResourceFactory.register_type(Interface)
90 ResourceFactory.register_type(Link)
92 class RunnerTestCase(unittest.TestCase):
93 def test_runner_max_runs(self):
97 ec = ExperimentController(exp_id = "max-runs-test")
99 # Add simulated nodes and applications
104 for i in range(node_count):
105 node = ec.register_resource("dummy::Node")
108 iface = ec.register_resource("dummy::Interface")
109 ec.register_connection(node, iface)
112 for i in range(app_count):
113 app = ec.register_resource("dummy::Application")
114 ec.register_connection(node, app)
117 link = ec.register_resource("dummy::Link")
120 ec.register_connection(link, iface)
122 rnr = ExperimentRunner()
123 runs = rnr.run(ec, min_runs = 5, max_runs = 10, wait_guids = apps,
126 self.assertEqual(runs, 10)
128 def test_runner_convergence(self):
132 ec = ExperimentController(exp_id = "convergence-test")
134 # Add simulated nodes and applications
139 for i in range(node_count):
140 node = ec.register_resource("dummy::Node")
143 iface = ec.register_resource("dummy::Interface")
144 ec.register_connection(node, iface)
147 for i in range(app_count):
148 app = ec.register_resource("dummy::Application")
149 ec.register_connection(node, app)
152 link = ec.register_resource("dummy::Link")
155 ec.register_connection(link, iface)
157 samples = [10, 10, 10, 10, 12, 10, 12, 10, 10, 11]
159 def compute_metric_callback(samples, ec, run):
160 return samples[run-1]
162 metric_callback = functools.partial(compute_metric_callback, samples)
164 rnr = ExperimentRunner()
165 runs = rnr.run(ec, min_runs = 5,
166 compute_metric_callback = metric_callback,
170 self.assertEqual(runs, 10)
172 if __name__ == '__main__':