class MultipleEndsTest(unittest.TestCase):
    def __init__(self, *args):
        super(MultipleEndsTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_multiple_sources_single_sink(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=5.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test2 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=10.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test3 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=15.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test1.start()
        test2.start()
        test3.start()

        time.sleep(3.0)

        bwsum = test1.bandwidth.movavg(5) + test2.bandwidth.movavg(
            5) + test3.bandwidth.movavg(5)
        self.assertTrue(
            bwsum > 0.8e6 and bwsum < 1.2e6,
            "Expected the combined bandwidth of three tests to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (bwsum))

        time.sleep(2.5)

        self.assertTrue(test1.done,
                        "Expected test1 to have finished at time = 5.5s")
        self.assertFalse(test2.done,
                         "Expected test2 to be still running at time = 5.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 5.5s")

        time.sleep(2.5)

        bwsum = test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(
            bwsum > 0.8e6 and bwsum < 1.2e6,
            "Expected the combined bandwidth of two tests to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (bwsum))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 10.5s")

        time.sleep(2.5)

        self.assertTrue(
            test3.bandwidth.movavg(5) > 0.80e6
            and test3.bandwidth.movavg(5) < 1.20e6,
            "Expected test3 bandwidth to be ~1Mbit/s (with 1 test running concurrently)"
            + ", instead it was %.2fMbit/s" %
            (test3.bandwidth.movavg(5) / 1e6))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")

        self.assertTrue(
            test3.latency.duration() > test2.latency.duration()
            and test2.latency.duration() > test1.latency.duration(),
            "Expected test3 duration (%.2fs) to be greater than test2 (%.2fs)"
            " and still greater than test1 (%.2fs)" %
            (test3.latency.duration(), test2.latency.duration(),
             test1.latency.duration()))

    def test_multiple_sinks(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw=5.0 * 10**6,
                                         pktsize=1500,
                                         duration=3.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test2 = self.srcnode.create_test(bw=5.0 * 10**6,
                                         pktsize=1500,
                                         duration=3.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12346,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test1.start()
        test2.start()
        time.sleep(3.5)

        self.assertTrue(test1.done,
                        "Expected test2 to have finished at time = 3.5s")
        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 3.5s")

        self.assertTrue(
            test2.overall_bandwidth > 4 * test1.overall_bandwidth,
            "Expected test2 bw (%.2fMbit/s) to be at least 4x larger"
            " than test1 bw (%.2fMbit/s)" %
            (test2.overall_bandwidth / 1e6, test1.overall_bandwidth / 1e6))
 def __init__(self, *args):
     super(MultipleEndsTest, self).__init__(*args)
     rospy.init_node('network_monitor_udp_test')
     self.srcnode = UdpmonsourceHandle('performance_test')
     self.tc = TcPortControl(self)
     self.tc.reset()
class ParameterTest(unittest.TestCase):
    def __init__(self, *args):
        super(ParameterTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_tos(self):
        time.sleep(1.0)

        test1 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 1.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2, tos = 0x10)
        test2 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 1.0,
                                         sink_ip = "127.0.0.1", sink_port = 12346,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2, tos = 0x11)
        
        test1.start()
        test2.start()

        s = scapy.L2Socket(iface='lo', filter='udp and port 12345')

        while True:
            pkt = s.recv(2048)
            if pkt is None:
                continue
            if pkt.type != 0x800:
                print "Unexpected protocol 0x%04x"%pkt.type
                continue
            if pkt.dport == 12345:
                tos = pkt.payload.tos
                break

        self.assertEqual(tos, 0x10,
                         "Unexpected TOS value for test1" + str(pkt))

        s = scapy.L2Socket(iface='lo', filter='udp and port 12346')

        while True:
            pkt = s.recv(2048)
            if pkt is None:
                continue
            if pkt.type != 0x800:
                print "Unexpected protocol 0x%04x"%pkt.type
                continue
            if pkt.dport == 12346:
                tos = pkt.payload.tos
                break

        self.assertEqual(tos, 0x11,
                         "Unexpected TOS value for test2: " + str(pkt))

    def test_latencybins(self):
        self.tc.set_latency_loss(latency = 0.15)
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 500, duration = 1.5,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test.start()
        time.sleep(2.0)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.overall_loss > 90.0,
                        "Expected 100%% loss, instead it was %.2f%%"%(test.overall_loss))
        self.assertTrue(test.latency_histogram[-1] > 0.95 and sum(test.latency_histogram[:-1]) < 0.05,
                        "Expected all packets to be in the last bin (100ms, infinity)")
                    
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 500, duration = 1.5,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.2, latencybins = [ 0.01, 0.1, 0.5 ])
        test.start()
        time.sleep(2.0)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue( test.overall_loss < 2.0,
                         "Expected 0%% loss, instead it was %.2f%%"%
                         (test.overall_loss))

    def test_max_return_time(self):
        self.tc.set_latency_loss(latency = 0.075)
        self.tc.set_latency_loss_udp_returnpath(latency = 0.175)

        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 500, duration = 1.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.05, ros_returnpath = False) 
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertEqual(test.latency.min(), 0.0,
                        "Expected min latency to be 0.0ms meaning that for at least one interval "
                        "(in fact, this should hold for all intervals) no packets were restricted [1]")
                   
        
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, max_return_time = 0.01)
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertEqual(test.latency.min(), 0.0,
                        "Expected min latency to be 0.0ms meaning that for at least one interval "
                        "(in fact, this should hold for all intervals) no packets were restricted [1]")

        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, max_return_time = 0.225)
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.latency.min() > 0.05,
                        "Expected min latency to be ~75ms (meaning that there was no interval which had "
                        " no restricted packets), instead it was %.2fms"%
                        (test.latency.min()))
        self.assertTrue(test.loss.min() < 2.0,
                        "Expected 0%% loss during all intervals, instead for one interval it was %.2f%%"%
                        (test.loss.min()) )

    def test_udp_return(self):
        self.tc.set_latency_loss_udp_returnpath(loss = 20.0)

        test = self.srcnode.create_test(bw = 2.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, ros_returnpath = False) 
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.loss.movavg() > 10.0,
                        "Expected loss to be ~20%% on UDP return path instead it was %.2f%%"%
                        (test.loss.movavg()))

        test = self.srcnode.create_test(bw = 2.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, ros_returnpath = True) 
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.loss.movavg() < 2.0,
                        "Expected loss to be ~0%% with ROS return path instead it was %.2f%%"%
                        (test.loss.movavg()))

    def test_roundtrip(self):
        self.tc.set_latency_loss_udp_returnpath(latency = 0.05)
        
        test = self.srcnode.create_test(bw = 2.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, ros_returnpath = False,
                                        roundtrip = False) 
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.latency.movavg() < 0.01,
                        "Expected latency to be < 1ms, instead it was %.2fms"%
                        (test.latency.movavg() * 1e3))

        test = self.srcnode.create_test(bw = 2.0*10**6, pktsize = 500, duration = 1.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, 
                                        update_interval = 0.05, ros_returnpath = False,
                                        roundtrip = True) 
        test.start()
        time.sleep(1.5)
        self.assertTrue(test.done, "Expected test to have ended")
        self.assertTrue(test.latency.movavg() > 0.03,
                        "Expected latency to be ~50ms, instead it was %.2fms"%
                        (test.latency.movavg() * 1e3))
Example #4
0
 def __init__(self, *args):
     super(ParameterTest, self).__init__(*args)
     rospy.init_node('network_monitor_udp_test')
     self.srcnode = UdpmonsourceHandle('performance_test')
     self.tc = TcPortControl(self)
     self.tc.reset()
Example #5
0
class ConstantBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(ConstantBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_basic(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 3.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(test.overall_latency > 0.0 and test.overall_latency < 0.005,
                        "Expected latency on loopback interface to be positive and under 5ms, instead it was %.2fms"%
                        (test.overall_latency * 1000))
        self.assertAlmostEqual(test.overall_loss, 0.0, 2,
                               "Expected packet loss on loopback interface to be zero, instead it was %.2f%%"%
                               (test.overall_loss))
        self.assertTrue(test.overall_bandwidth > 0.9 * 10**6,
                        "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"%
                        (test.overall_bandwidth/1e6))
                    

    def test_bw_measurement(self):
        test = self.srcnode.create_test(bw = 3.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(test.overall_bandwidth < 1e6 and test.overall_bandwidth > 0.9e6,
                        "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"%
                        (test.overall_bandwidth/1e6))

    def test_bw_measurement_variable_bw(self):   
        test = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.15)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 1.15e6 and test.bandwidth.movavg(5) > 0.85e6,
                        "Expected useful bandwidth on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(4e6)
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 4.2e6 and test.bandwidth.movavg(5) > 3.8e6,
                        "Expected useful bandwidth on loopback interface to be ~4Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(0.25e6)
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 0.35e6 and test.bandwidth.movavg(5) > 0.15e6,
                        "Expected useful bandwidth on loopback interface to be ~0.25Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))

    def test_loss_measurement(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 12.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.15)
        self.tc.set_latency_loss(loss = 5)
        test.start()
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 8.5 and test.loss.movavg() > 1.5,
                        "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"%
                        (test.loss.movavg()))
        self.tc.set_latency_loss(loss = 20)
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 25.0 and test.loss.movavg() > 15.0,
                        "Expected packet loss on loopback interface to be ~20%%, instead it was %.2f%%"%
                        (test.loss.movavg()))
        self.tc.set_latency_loss(loss = 0)
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 1.0,
                        "Expected packet loss on loopback interface to be ~0%%, instead it was %.2f%%"%
                        (test.loss.movavg()))

    def test_latency_measurement(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 6.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.2)
        test.start()
        time.sleep(1.5)
        lo_latency = test.latency.movavg(3)        
        self.tc.set_latency_loss(latency = 0.02)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency > 0.015 and added_latency < 0.025,
                        "Expected added latency on loopback interface to be ~20ms, instead it was %.2fms"%
                        (added_latency * 1000))
        self.tc.set_latency_loss(latency = 0.06)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency > 0.052 and added_latency < 0.068,
                        "Expected added latency on loopback interface to be ~60ms, instead it was %.2fms"%
                        (added_latency * 1000))
        self.tc.set_latency_loss(latency = 0)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency < 0.002,
                        "Expected added latency on loopback interface to be ~0ms, instead it was %.2fms"%
                        (added_latency * 1000))

    def test_latency_loss(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 3.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.2)
        self.tc.set_latency_loss(loss = 5, latency = 0.05)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.loss.movavg(10) < 7.5 and test.loss.movavg(10) > 2.5,
                        "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"%
                        (test.loss.movavg(10)))
        self.assertTrue(test.latency.movavg(10) < 0.07 and test.latency.movavg(10) > 0.05,
                        "Expected latency on loopback interface to be ~60ms, instead it was %.2fms"%
                        (test.latency.movavg(10)* 1000))
class AdaptiveBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(AdaptiveBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_ramp_up(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(
            test.bandwidth.movavg(5) > 4.3e6
            and test.bandwidth.movavg(5) < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_ramp_down(self):
        test = self.srcnode.create_test(bw=5.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(2.0)
        self.tc.set_rate_limit(1e6)
        time.sleep(3.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 0.6e6
            and test.bandwidth.movavg(5) < 1.4e6,
            "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_varying_capacity(self):
        test = self.srcnode.create_test(bw=2 * 10**6,
                                        pktsize=1500,
                                        duration=15.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(10e6)
        test.start()
        time.sleep(4.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 9e6 and test.bandwidth.movavg(5) < 11e6,
            "Expected capacity on loopback interface to be ~10Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(1e6)
        time.sleep(4.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 0.6e6
            and test.bandwidth.movavg(5) < 1.4e6,
            "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(5e6)
        time.sleep(6.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 3.5e6
            and test.bandwidth.movavg(5) < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_get_capacity(self):
        self.tc.set_rate_limit(5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 4.3e6 and capacity < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
        self.tc.set_rate_limit(20e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 15e6 and capacity < 25e6,
            "Expected capacity on loopback interface to be ~20Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
        self.tc.set_rate_limit(0.5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 0.3e6 and capacity < 0.7e6,
            "Expected capacity on loopback interface to be ~0.5Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
Example #7
0
class AdaptiveBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(AdaptiveBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_ramp_up(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.bandwidth.movavg(5) > 4.3e6 and test.bandwidth.movavg(5) < 5.7e6,
                   "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                   (test.bandwidth.movavg(5)/1e6))

    def test_ramp_down(self):
        test = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(2.0)
        self.tc.set_rate_limit(1e6)
        time.sleep(3.0)
        self.assertTrue(test.bandwidth.movavg(5) > 0.6e6 and test.bandwidth.movavg(5) < 1.4e6,
                   "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                   (test.bandwidth.movavg(5)/1e6))

    def test_varying_capacity(self):
        test = self.srcnode.create_test(bw = 2*10**6, pktsize = 1500, duration = 15.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(10e6)
        test.start()
        time.sleep(4.0)
        self.assertTrue(test.bandwidth.movavg(5) > 9e6 and test.bandwidth.movavg(5) < 11e6,
                        "Expected capacity on loopback interface to be ~10Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(1e6)
        time.sleep(4.0)
        self.assertTrue(test.bandwidth.movavg(5) > 0.6e6 and test.bandwidth.movavg(5) < 1.4e6,
                        "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(5e6)
        time.sleep(6.0)
        self.assertTrue(test.bandwidth.movavg(5) > 3.5e6 and test.bandwidth.movavg(5) < 5.7e6,
                        "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))

    def test_get_capacity(self):
        self.tc.set_rate_limit(5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 4.3e6 and capacity < 5.7e6,
                        "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
        self.tc.set_rate_limit(20e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 15e6 and capacity < 25e6,
                        "Expected capacity on loopback interface to be ~20Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
        self.tc.set_rate_limit(0.5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 0.3e6 and capacity < 0.7e6,
                        "Expected capacity on loopback interface to be ~0.5Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
class ConstantBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(ConstantBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_basic(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=1500,
                                        duration=3.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(
            test.overall_latency > 0.0 and test.overall_latency < 0.005,
            "Expected latency on loopback interface to be positive and under 5ms, instead it was %.2fms"
            % (test.overall_latency * 1000))
        self.assertAlmostEqual(
            test.overall_loss, 0.0, 2,
            "Expected packet loss on loopback interface to be zero, instead it was %.2f%%"
            % (test.overall_loss))
        self.assertTrue(
            test.overall_bandwidth > 0.9 * 10**6,
            "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"
            % (test.overall_bandwidth / 1e6))

    def test_bw_measurement(self):
        test = self.srcnode.create_test(bw=3.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(
            test.overall_bandwidth < 1e6 and test.overall_bandwidth > 0.9e6,
            "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"
            % (test.overall_bandwidth / 1e6))

    def test_bw_measurement_variable_bw(self):
        test = self.srcnode.create_test(bw=5.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.15)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 1.15e6
            and test.bandwidth.movavg(5) > 0.85e6,
            "Expected useful bandwidth on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(4e6)
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 4.2e6
            and test.bandwidth.movavg(5) > 3.8e6,
            "Expected useful bandwidth on loopback interface to be ~4Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(0.25e6)
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 0.35e6
            and test.bandwidth.movavg(5) > 0.15e6,
            "Expected useful bandwidth on loopback interface to be ~0.25Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_loss_measurement(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=12.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.15)
        self.tc.set_latency_loss(loss=5)
        test.start()
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 8.5 and test.loss.movavg() > 1.5,
            "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"
            % (test.loss.movavg()))
        self.tc.set_latency_loss(loss=20)
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 25.0 and test.loss.movavg() > 15.0,
            "Expected packet loss on loopback interface to be ~20%%, instead it was %.2f%%"
            % (test.loss.movavg()))
        self.tc.set_latency_loss(loss=0)
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 1.0,
            "Expected packet loss on loopback interface to be ~0%%, instead it was %.2f%%"
            % (test.loss.movavg()))

    def test_latency_measurement(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=6.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.2)
        test.start()
        time.sleep(1.5)
        lo_latency = test.latency.movavg(3)
        self.tc.set_latency_loss(latency=0.02)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency > 0.015 and added_latency < 0.025,
            "Expected added latency on loopback interface to be ~20ms, instead it was %.2fms"
            % (added_latency * 1000))
        self.tc.set_latency_loss(latency=0.06)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency > 0.052 and added_latency < 0.068,
            "Expected added latency on loopback interface to be ~60ms, instead it was %.2fms"
            % (added_latency * 1000))
        self.tc.set_latency_loss(latency=0)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency < 0.002,
            "Expected added latency on loopback interface to be ~0ms, instead it was %.2fms"
            % (added_latency * 1000))

    def test_latency_loss(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=3.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.2)
        self.tc.set_latency_loss(loss=5, latency=0.05)
        test.start()
        time.sleep(3.5)
        self.assertTrue(
            test.loss.movavg(10) < 7.5 and test.loss.movavg(10) > 2.5,
            "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"
            % (test.loss.movavg(10)))
        self.assertTrue(
            test.latency.movavg(10) < 0.07 and test.latency.movavg(10) > 0.05,
            "Expected latency on loopback interface to be ~60ms, instead it was %.2fms"
            % (test.latency.movavg(10) * 1000))
class MultipleEndsTest(unittest.TestCase):
    def __init__(self, *args):
        super(MultipleEndsTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_multiple_sources_single_sink(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 5.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test2 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 10.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test3 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 15.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test1.start()
        test2.start()
        test3.start()

        time.sleep(3.0)

        bwsum = test1.bandwidth.movavg(5) + test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(bwsum > 0.8e6 and bwsum < 1.2e6,
                        "Expected the combined bandwidth of three tests to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (bwsum))

        time.sleep(2.5)
        
        self.assertTrue(test1.done,
                        "Expected test1 to have finished at time = 5.5s")
        self.assertFalse(test2.done,
                         "Expected test2 to be still running at time = 5.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 5.5s")

        time.sleep(2.5)
        
        bwsum = test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(bwsum > 0.8e6 and bwsum < 1.2e6,
                        "Expected the combined bandwidth of two tests to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (bwsum))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 10.5s")

        time.sleep(2.5)

        self.assertTrue(test3.bandwidth.movavg(5) > 0.80e6 and test3.bandwidth.movavg(5) < 1.20e6,
                        "Expected test3 bandwidth to be ~1Mbit/s (with 1 test running concurrently)" + 
                        ", instead it was %.2fMbit/s"%
                        (test3.bandwidth.movavg(5)/1e6))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")

        self.assertTrue(test3.latency.duration() > test2.latency.duration() and 
                        test2.latency.duration() > test1.latency.duration(),
                        "Expected test3 duration (%.2fs) to be greater than test2 (%.2fs)"
                        " and still greater than test1 (%.2fs)"%
                        (test3.latency.duration(), test2.latency.duration(), test1.latency.duration()))
        
    def test_multiple_sinks(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 3.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test2 = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 3.0,
                                         sink_ip = "127.0.0.1", sink_port = 12346,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test1.start()
        test2.start()
        time.sleep(3.5)
        
        self.assertTrue(test1.done, "Expected test2 to have finished at time = 3.5s")
        self.assertTrue(test2.done, "Expected test2 to have finished at time = 3.5s")
        
        self.assertTrue(test2.overall_bandwidth > 4 * test1.overall_bandwidth,
                        "Expected test2 bw (%.2fMbit/s) to be at least 4x larger"
                        " than test1 bw (%.2fMbit/s)"%
                        (test2.overall_bandwidth/1e6, test1.overall_bandwidth/1e6))