def bench(): # order of magnitude # of routes on the internet == normal use case data = generate_random_data(100000) keys = generate_random_ips(10000) results = {} c = CidrClassifier() n = NaiveCidrClassifier() c.add_mapping('0.0.0.0', 0, 'Base') n.add_mapping('0.0.0.0', 0, 'Base') with time_simple('insert CidrClassifier', results): bench_insert(c, data) with time_simple('insert NaiveCidrClassifier', results): bench_insert(n, data) with time_simple('lookup CidrClassifier', results): r1 = bench_lookup(c, keys) with time_simple('lookup NaiveCidrClassifier', results): r2 = bench_lookup(n, keys) assert r1 == r2 for k, v in sorted(results.iteritems()): print '%s %.2fs' % (k, v)
def test_basic(self): c = CidrClassifier() c.add_mapping('0.0.0.0', 0, 'NONE') c.add_mapping('192.168.0.0', 16, 'RFC1918') c.add_mapping('104.36.192.0', 21, 'Uber') c.add_mapping('104.36.192.0', 22, 'Uber1') self.assertEqual(c.lookup('104.36.192.1'), ('104.36.192.0', 22, 'Uber1')) self.assertEqual(c.lookup('104.36.196.1'), ('104.36.192.0', 21, 'Uber')) self.assertEqual(c.lookup('192.168.0.0'), ('192.168.0.0', 16, 'RFC1918')) self.assertEqual(c.lookup('192.1.0.0'), ('0.0.0.0', 0, 'NONE'))
def test_basic(self): c = CidrClassifier() # ECMP path c.add_mapping('192.168.0.0', 24, 'HopA') c.add_mapping('192.168.0.0', 24, 'HopB') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 24, ['HopA', 'HopB'])) # non-ECMP path because of different mask c.add_mapping('192.168.0.0', 26, 'HopA') c.add_mapping('192.168.0.0', 27, 'HopC') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 27, ['HopC'])) c.remove_mapping('192.168.0.0', 27, 'HopC') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 26, ['HopA'])) c.remove_mapping('192.168.0.0', 26, 'HopA') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 24, ['HopA', 'HopB'])) c.remove_mapping('192.168.0.0', 24, 'HopB') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 24, ['HopA'])) c.add_mapping('192.168.0.0', 24, 'HopC') self.assertEqual(c.lookup('192.168.0.1'), ('192.168.0.0', 24, ['HopA', 'HopC'])) c.remove_mapping('192.168.0.0', 24, 'HopA') c.remove_mapping('192.168.0.0', 24, 'HopC') self.assertRaises(KeyError, lambda: c.lookup('192.168.0.0')) c.add_mapping('10.0.0.0', 8, 'HopA') c.add_mapping('10.0.0.0', 12, 'HopA') c.add_mapping('10.0.0.0', 12, 'HopB') c.add_mapping('192.168.0.0', 16, 'HopA') # self.assertEqual(c.show_mapping(), ) print(c.show_all_mappings()) # vim: set textwidth=120: