|
| 1 | +# Copyright (c) 2016-2019, Nefeli Networks, Inc. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# * Redistributions of source code must retain the above copyright notice, this |
| 8 | +# list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# * Neither the names of the copyright holders nor the names of their |
| 15 | +# contributors may be used to endorse or promote products derived from this |
| 16 | +# software without specific prior written permission. |
| 17 | +# |
| 18 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +# POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +from test_utils import * |
| 31 | + |
| 32 | + |
| 33 | +class BessQueueOccupancyTest(BessModuleTestCase): |
| 34 | + def _send_packets(self, q): |
| 35 | + eth = scapy.Ether(src='02:1e:67:9f:4d:ae', dst='06:16:3e:1b:72:32') |
| 36 | + ip = scapy.IP(src='172.16.0.2', dst='8.8.8.8') |
| 37 | + tcp = scapy.TCP(sport=52428, dport=80) |
| 38 | + l7 = 'helloworld' |
| 39 | + pkt = eth / ip / tcp / l7 |
| 40 | + |
| 41 | + pkts = [pkt] * 100 |
| 42 | + _ = self.run_module(q, 0, pkts, [0]) |
| 43 | + return len(pkts) |
| 44 | + |
| 45 | + def test_hist_enable(self): |
| 46 | + q = Queue(size=1024, track_occupancy=True) |
| 47 | + sent = self._send_packets(q) |
| 48 | + resp = q.get_status() |
| 49 | + self.assertEqual(resp.enqueued, sent) |
| 50 | + self.assertEqual(resp.dequeued, sent) |
| 51 | + self.assertEqual(resp.occupancy_summary.count, sent) |
| 52 | + |
| 53 | + def test_hist_disable(self): |
| 54 | + q = Queue(size=1024, track_occupancy=False) |
| 55 | + sent = self._send_packets(q) |
| 56 | + resp = q.get_status() |
| 57 | + self.assertEqual(resp.enqueued, sent) |
| 58 | + self.assertEqual(resp.dequeued, sent) |
| 59 | + self.assertEqual(resp.occupancy_summary.count, 0) |
| 60 | + |
| 61 | + def test_hist_size(self): |
| 62 | + q = Queue(size=1024, track_occupancy=True) |
| 63 | + resp = q.get_status() |
| 64 | + self.assertEqual(resp.size, 1024) |
| 65 | + self.assertEqual(resp.occupancy_summary.num_buckets, 32) |
| 66 | + self.assertEqual(resp.occupancy_summary.bucket_width, 32) |
| 67 | + |
| 68 | + q.set_size(size=2048) |
| 69 | + resp = q.get_status() |
| 70 | + self.assertEqual(resp.size, 2048) |
| 71 | + self.assertEqual(resp.occupancy_summary.num_buckets, 32) |
| 72 | + self.assertEqual(resp.occupancy_summary.bucket_width, 64) |
| 73 | + |
| 74 | + q = Queue(size=1024, track_occupancy=True, occupancy_hist_buckets=64) |
| 75 | + resp = q.get_status() |
| 76 | + self.assertEqual(resp.size, 1024) |
| 77 | + self.assertEqual(resp.occupancy_summary.num_buckets, 64) |
| 78 | + self.assertEqual(resp.occupancy_summary.bucket_width, 16) |
| 79 | + |
| 80 | + def test_hist_summary(self): |
| 81 | + q = Queue(size=1024, track_occupancy=True) |
| 82 | + sent = self._send_packets(q) |
| 83 | + |
| 84 | + resp = q.get_status(occupancy_percentiles=[0.5, 0.9, 0.99]) |
| 85 | + self.assertEqual(resp.occupancy_summary.count, 100) |
| 86 | + self.assertEqual(len(resp.occupancy_summary.percentile_values), 3) |
| 87 | + |
| 88 | + resp = q.get_status(occupancy_percentiles=[0, 0.5, 0.9, 0.99]) |
| 89 | + self.assertEqual(resp.occupancy_summary.count, 100) |
| 90 | + self.assertEqual(len(resp.occupancy_summary.percentile_values), 4) |
| 91 | + |
| 92 | + resp = q.get_status(clear_hist=True) |
| 93 | + self.assertEqual(resp.occupancy_summary.count, 100) |
| 94 | + |
| 95 | + resp = q.get_status() |
| 96 | + self.assertEqual(resp.occupancy_summary.count, 0) |
| 97 | + |
| 98 | + |
| 99 | +suite = unittest.TestLoader().loadTestsFromTestCase(BessQueueOccupancyTest) |
| 100 | +results = unittest.TextTestRunner(verbosity=2, stream=sys.stdout).run(suite) |
| 101 | + |
| 102 | +if results.failures or results.errors: |
| 103 | + sys.exit(1) |
0 commit comments