Best Python code snippet using localstack_python
test_collectors.py
Source:test_collectors.py
...8 link_type = {(1, 2): 'internal', (2, 3): 'external',9 (2, 1): 'internal', (3, 2): 'external'}10 view = type('MockNetworkView', (), {'link_type': lambda s, u, v: link_type[(u, v)]})()11 c = collectors.LinkLoadCollector(view, req_size=req_size, content_size=cont_size)12 c.start_session(3.0, 1, 4)13 c.request_hop(1, 2)14 c.content_hop(2, 1)15 c.end_session()16 c.start_session(5.0, 1, 4)17 c.request_hop(1, 2)18 c.request_hop(2, 3)19 c.content_hop(3, 2)20 c.content_hop(2, 1)21 c.end_session()22 res = c.results()23 int_load = res['PER_LINK_INTERNAL']24 ext_load = res['PER_LINK_EXTERNAL']25 self.assertEqual(2 * req_size / 2, int_load[(1, 2)])26 self.assertEqual(2 * cont_size / 2, int_load[(2, 1)])27 self.assertEqual(req_size / 2, ext_load[(2, 3)])28 self.assertEqual(cont_size / 2, ext_load[(3, 2)])29 mean_int = res['MEAN_INTERNAL']30 mean_ext = res['MEAN_EXTERNAL']31 self.assertEqual((req_size + cont_size) / 2, mean_int)32 self.assertEqual((req_size + cont_size) / 4, mean_ext)33 def test_internal_links_only(self):34 req_size = 50035 cont_size = 70036 link_type = {(1, 2): 'internal', (2, 3): 'internal',37 (2, 1): 'internal', (3, 2): 'internal'}38 view = type('MockNetworkView', (), {'link_type': lambda s, u, v: link_type[(u, v)]})()39 c = collectors.LinkLoadCollector(view, req_size=req_size, content_size=cont_size)40 c.start_session(3.0, 1, 4)41 c.request_hop(1, 2)42 c.content_hop(2, 1)43 c.end_session()44 c.start_session(5.0, 1, 4)45 c.request_hop(1, 2)46 c.request_hop(2, 3)47 c.content_hop(3, 2)48 c.content_hop(2, 1)49 c.end_session()50 res = c.results()51 mean_ext = res['MEAN_EXTERNAL']52 ext_load = res['PER_LINK_EXTERNAL']53 self.assertEqual(0, mean_ext)54 self.assertEqual(0, len(ext_load))55 def test_external_links_only(self):56 req_size = 50057 cont_size = 70058 link_type = {(1, 2): 'external', (2, 3): 'external',59 (2, 1): 'external', (3, 2): 'external'}60 view = type('MockNetworkView', (), {'link_type': lambda s, u, v: link_type[(u, v)]})()61 c = collectors.LinkLoadCollector(view, req_size=req_size, content_size=cont_size)62 c.start_session(3.0, 1, 'CONTENT')63 c.request_hop(1, 2)64 c.content_hop(2, 1)65 c.end_session()66 c.start_session(5.0, 1, 'CONTENT')67 c.request_hop(1, 2)68 c.request_hop(2, 3)69 c.content_hop(3, 2)70 c.content_hop(2, 1)71 c.end_session()72 res = c.results()73 mean_ext = res['MEAN_INTERNAL']74 ext_load = res['PER_LINK_INTERNAL']75 self.assertEqual(0, mean_ext)76 self.assertEqual(0, len(ext_load))77class TestLatencyCollector(unittest.TestCase):78 def test_base(self):79 link_delay = {(1, 2): 2, (2, 3): 10,80 (2, 1): 4, (3, 2): 20}81 view = type('MockNetworkView', (), {'link_delay': lambda s, u, v: link_delay[(u, v)]})()82 c = collectors.LatencyCollector(view)83 c.start_session(3.0, 1, 'CONTENT')84 c.request_hop(1, 2)85 c.content_hop(2, 1)86 c.end_session()87 c.start_session(5.0, 1, 'CONTENT')88 c.request_hop(1, 2)89 c.request_hop(2, 3)90 c.content_hop(3, 2)91 c.content_hop(2, 1)92 c.end_session()93 res = c.results()94 self.assertEqual((10 + 20 + 2 * (2 + 4)) / 2, res['MEAN'])95 def test_main_path(self):96 link_delay = {(1, 2): 2, (2, 3): 10,97 (2, 1): 4, (3, 2): 20}98 view = type('MockNetworkView', (), {'link_delay': lambda s, u, v: link_delay[(u, v)]})()99 c = collectors.LatencyCollector(view)100 c.start_session(3.0, 1, 'CONTENT')101 c.request_hop(1, 2)102 c.content_hop(2, 1)103 c.end_session()104 c.start_session(5.0, 1, 'CONTENT')105 c.request_hop(1, 2)106 c.request_hop(2, 3)107 c.request_hop(2, 1, main_path=False)108 c.content_hop(3, 2)109 c.content_hop(2, 1)110 c.content_hop(2, 3, main_path=False)111 c.end_session()112 res = c.results()113 self.assertEqual((10 + 20 + 2 * (2 + 4)) / 2, res['MEAN'])114class TestCacheHitRatioCollector(unittest.TestCase):115 def test_base(self):116 view = type('MockNetworkView', (), {})()117 c = collectors.CacheHitRatioCollector(view)118 c.start_session(3.0, 1, 'CONTENT')119 c.cache_hit(1)120 c.end_session()121 c.start_session(4.0, 1, 'CONTENT')122 c.server_hit(2)123 c.end_session()124 res = c.results()125 self.assertEqual(0.5, res['MEAN'])126 def test_per_node(self):127 view = type('MockNetworkView', (), {})()128 c = collectors.CacheHitRatioCollector(view, per_node=True)129 c.start_session(3.0, 1, 'CONTENT')130 c.cache_hit(1)131 c.end_session()132 c.start_session(4.0, 1, 'CONTENT')133 c.cache_hit(1)134 c.end_session()135 c.start_session(5.0, 1, 'CONTENT')136 c.cache_hit(2)137 c.end_session()138 c.start_session(6.0, 1, 'CONTENT')139 c.cache_hit(3)140 c.end_session()141 c.start_session(7.0, 1, 'CONTENT')142 c.server_hit(4)143 c.end_session()144 res = c.results()145 self.assertEqual({1: 0.4, 2: 0.2, 3: 0.2}, res['PER_NODE_CACHE_HIT_RATIO'])146 self.assertEqual({4: 0.2}, res['PER_NODE_SERVER_HIT_RATIO'])147 def test_per_content(self):148 view = type('MockNetworkView', (), {})()149 c = collectors.CacheHitRatioCollector(view, content_hits=True)150 c.start_session(3.0, 'RECV', 1)151 c.cache_hit(1)152 c.end_session()153 c.start_session(4.0, 'RECV', 1)154 c.server_hit(2)155 c.end_session()156 c.start_session(5.0, 'RECV', 2)157 c.cache_hit(3)158 c.end_session()159 c.start_session(6.0, 'RECV', 2)160 c.server_hit(4)161 c.end_session()162 c.start_session(7.0, 'RECV', 2)163 c.server_hit(5)164 c.end_session()165 c.start_session(8.0, 'RECV', 2)166 c.server_hit(5)167 c.end_session()168 res = c.results()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!