From b5b7131f4e19b427d75aac333b106bfbdd4e978c Mon Sep 17 00:00:00 2001 From: Antoni Segura Puimedon Date: Tue, 10 Oct 2017 12:01:58 +0200 Subject: [PATCH] net_utils: Speed up mac address generation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using getrandbits supposes a huge speedup: Py2 In [2]: %timeit random.randint(0, 0xff) The slowest run took 28.53 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 769 ns per loop In [3]: %timeit random.getrandbits(8) The slowest run took 54.81 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 126 ns per loop Py3 In [3]: %timeit random.randint(0, 0xff) The slowest run took 288.80 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 1.15 µs per loop In [4]: %timeit random.getrandbits(8) The slowest run took 29.15 times longer than the fastest. This could mean that an intermediate result is being cached. 10000000 loops, best of 3: 109 ns per loop Change-Id: Id6b86c7d2cf88feb81b79862d93f8d06aeb8fa63 Signed-off-by: Antoni Segura Puimedon --- neutron_lib/tests/unit/utils/test_net.py | 8 ++++---- neutron_lib/utils/net.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/neutron_lib/tests/unit/utils/test_net.py b/neutron_lib/tests/unit/utils/test_net.py index 8a2bac064..6c210da81 100644 --- a/neutron_lib/tests/unit/utils/test_net.py +++ b/neutron_lib/tests/unit/utils/test_net.py @@ -33,17 +33,17 @@ class TestGetHostname(base.BaseTestCase): class TestGetRandomMac(base.BaseTestCase): - @mock.patch.object(random, 'randint', return_value=0xa2) + @mock.patch.object(random, 'getrandbits', return_value=0xa2) def test_first_4_octets_unchanged(self, mock_rnd): mac = net.get_random_mac(['aa', 'bb', '00', 'dd', 'ee', 'ff']) self.assertEqual('aa:bb:00:dd:a2:a2', mac) - mock_rnd.assert_called_with(0x00, 0xff) + mock_rnd.assert_called_with(8) - @mock.patch.object(random, 'randint', return_value=0xa2) + @mock.patch.object(random, 'getrandbits', return_value=0xa2) def test_first_4th_octet_generated(self, mock_rnd): mac = net.get_random_mac(['aa', 'bb', 'cc', '00', 'ee', 'ff']) self.assertEqual('aa:bb:cc:a2:a2:a2', mac) - mock_rnd.assert_called_with(0x00, 0xff) + mock_rnd.assert_called_with(8) class TestPortDeviceOwner(base.BaseTestCase): diff --git a/neutron_lib/utils/net.py b/neutron_lib/utils/net.py index 3e2c80ff7..07121ae2f 100644 --- a/neutron_lib/utils/net.py +++ b/neutron_lib/utils/net.py @@ -36,8 +36,8 @@ def get_random_mac(base_mac): """ mac = [int(base_mac[0], 16), int(base_mac[1], 16), - int(base_mac[2], 16), random.randint(0x00, 0xff), - random.randint(0x00, 0xff), random.randint(0x00, 0xff)] + int(base_mac[2], 16), random.getrandbits(8), + random.getrandbits(8), random.getrandbits(8)] if base_mac[3] != '00': mac[3] = int(base_mac[3], 16) return ':'.join(["%02x" % x for x in mac])