Пример #1
0
    def test_meters_bench_chunk_udp_array_via_serialization(self):
        """
        Test
        """

        # For logs format
        SolBase.sleep(100)
        logger.info("Bench now")

        self.per_loop = 1

        self._meters_inject(count=5000)
        ar_json = Meters.meters_to_udp_format(send_pid=True, send_tags=True, send_dtc=False)
        self._bench(1, "udp_chunk_no_dtc", Meters.chunk_udp_array_via_serialization, ar_json, 60000)

        self._meters_inject(count=5000)
        ar_json = Meters.meters_to_udp_format(send_pid=True, send_tags=True, send_dtc=True)
        self._bench(1, "udp_chunk_dtc", Meters.chunk_udp_array_via_serialization, ar_json, 60000)

        # For logs format
        SolBase.sleep(100)
        logger.info("Bench over")
Пример #2
0
    def test_meters_to_udp(self):
        """
        Test
        """

        ai1a = Meters.ai("ai1")
        self.assertIsInstance(ai1a, AtomicIntSafe)
        ai1b = Meters.ai("ai1")
        self.assertEqual(id(ai1a), id(ai1b))

        ai1a = Meters.aii("ai1")
        self.assertEqual(ai1a.get(), 1)
        ai1a = Meters.aii("ai1", 2)
        self.assertEqual(ai1a.get(), 3)
        self.assertEqual(ai1a.get(), Meters.aig("ai1"))

        af1a = Meters.af("af1")
        self.assertIsInstance(af1a, AtomicFloatSafe)
        af1b = Meters.af("af1")
        self.assertEqual(id(af1a), id(af1b))

        af1a = Meters.afi("af1")
        self.assertEqual(af1a.get(), 1.0)
        af1a = Meters.afi("af1", 2.0)
        self.assertEqual(af1a.get(), 3.0)
        self.assertEqual(af1a.get(), Meters.afg("af1"))

        dtc1a = Meters.dtc("dtc1")
        self.assertIsInstance(dtc1a, DelayToCountSafe)
        dtc1b = Meters.dtc("dtc1")
        self.assertEqual(id(dtc1a), id(dtc1b))

        Meters.dtci("dtc1", 0)
        Meters.dtci("dtc1", 50)
        Meters.dtci("dtc1", 100)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[0].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[50].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[100].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[500].get(), 0)

        # Write
        Meters.write_to_logger()

        # Serialize
        ar_json = Meters.meters_to_udp_format(send_pid=True, send_dtc=True)
        logger.info("Got ar_json=%s", ar_json)
        for cur_ar in ar_json:
            logger.info("Got cur_ar=%s", cur_ar)

        # Serialize, no dtc
        ar_json = Meters.meters_to_udp_format(send_pid=True, send_dtc=False)
        logger.info("Got ar_json=%s", ar_json)
        for cur_ar in ar_json:
            logger.info("Got cur_ar=%s", cur_ar)

        # Send to daemon (assuming its up locally)
        Meters.send_udp_to_knockdaemon()
        Meters.send_udp_to_knockdaemon(send_dtc=True)
        Meters.send_udp_to_knockdaemon(send_dtc=False)

        # ------------------------
        # UDP Scheduler test
        # ------------------------

        # Check
        self.assertIsNone(Meters.UDP_SCHEDULER_GREENLET)
        self.assertFalse(Meters.UDP_SCHEDULER_STARTED)

        # Start
        Meters.udp_scheduler_start(send_interval_ms=500)

        # Check
        self.assertIsNotNone(Meters.UDP_SCHEDULER_GREENLET)
        self.assertTrue(Meters.UDP_SCHEDULER_STARTED)

        # Start again
        Meters.udp_scheduler_start(send_interval_ms=500)

        # Check again
        self.assertIsNotNone(Meters.UDP_SCHEDULER_GREENLET)
        self.assertTrue(Meters.UDP_SCHEDULER_STARTED)

        # Interval is 500 => we sleep 3.250 sec, we assume we must have at least 500, 1000, 1500, 2000, 2500, 3000 run => 6 runs
        SolBase.sleep(3250)

        # Check
        self.assertGreaterEqual(Meters.aig("k.meters.udp.run.ok"), 6)
        self.assertEqual(Meters.aig("k.meters.udp.run.ex"), 0)
        self.assertIsNotNone(Meters.UDP_SCHEDULER_GREENLET)
        self.assertTrue(Meters.UDP_SCHEDULER_STARTED)

        # We stop
        Meters.udp_scheduler_stop()
        self.assertIsNone(Meters.UDP_SCHEDULER_GREENLET)
        self.assertFalse(Meters.UDP_SCHEDULER_STARTED)

        # Sleep again and check no more running
        cur_run = Meters.aig("k.meters.udp.run.ok")
        SolBase.sleep(2000)
        self.assertEqual(cur_run, Meters.aig("k.meters.udp.run.ok"))
Пример #3
0
    def test_meters_chunk_udp_array_via_serialization(self):
        """
        Test
        """

        # ----------------------
        # Udp, no DTC
        # ----------------------
        self._meters_inject(count=5000)
        ar_json = Meters.meters_to_udp_format(
            send_pid=True,
            send_tags=True,
            send_dtc=False,
        )
        logger.info("Got ar_json.len=%s", len(ar_json))

        for max_size, _ in [
            (60000, 0.0),
            (30000, 0.0),
        ]:
            logger.info("*** CHECK, NO DTC, max_size=%s", max_size)
            ar_bin_chunk, ar_json_chunk = Meters.chunk_udp_array_via_serialization(ar_json, max_size_bytes=max_size)
            logger.info("Got chunk size=%s/%s", len(ar_bin_chunk), len(ar_json_chunk))
            for bin_buf in ar_bin_chunk:
                logger.debug("Got chunk, NO DTC, len=%s, max=%s", len(bin_buf), max_size)
                self.assertTrue(len(bin_buf) < max_size)

            logger.info("Check now")
            c = 0
            for cur_ar in ar_json_chunk:
                c += len(cur_ar)
                for cur_item in cur_ar:
                    self.assertIn(cur_item, ar_json)
            self.assertEquals(c, len(ar_json))

        # ----------------------
        # Udp, DTC
        # ----------------------
        self._meters_inject(count=500)
        ar_json = Meters.meters_to_udp_format(
            send_pid=True,
            send_tags=True,
            send_dtc=True,
        )
        logger.info("Got ar_json.len=%s", len(ar_json))

        for max_size, _ in [
            (60000, 0.0),
            (30000, 0.0),
        ]:
            logger.info("*** CHECK, DTC, max_size=%s", max_size)
            ar_bin_chunk, ar_json_chunk = Meters.chunk_udp_array_via_serialization(ar_json, max_size_bytes=max_size)
            logger.info("Got chunk size=%s/%s", len(ar_bin_chunk), len(ar_json_chunk))
            for bin_buf in ar_bin_chunk:
                logger.debug("Got chunk, DTC, len=%s, max=%s", len(bin_buf), max_size)
                self.assertTrue(len(bin_buf) <= max_size)

            logger.info("Check now")
            c = 0
            for cur_ar in ar_json_chunk:
                c += len(cur_ar)
                for cur_item in cur_ar:
                    self.assertIn(cur_item, ar_json)
            self.assertEquals(c, len(ar_json))
Пример #4
0
    def test_meters_with_tags_a_with_udp_check(self):
        """
        Test
        """

        hca = Meters._tags_hash_compute_and_store({"flag": "FA"})
        hcb = Meters._tags_hash_compute_and_store({"flag": "FB"})

        Meters.aii("ai1")
        Meters.aii("ai1", tags={"flag": "FA"})
        Meters.aii("ai1", tags={"flag": "FA"})
        Meters.aii("ai1", tags={"flag": "FB"})
        Meters.aii("ai1", tags={"flag": "FB"})
        Meters.aii("ai1", tags={"flag": "FB"})

        Meters.dtci("dtc1", 0)
        Meters.dtci("dtc1", 50)
        Meters.dtci("dtc1", 100)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[0].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[50].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[100].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#"]._sorted_dict[500].get(), 0)

        Meters.dtci("dtc1", 0, tags={"flag": "FA"})
        Meters.dtci("dtc1", 50, tags={"flag": "FA"})
        Meters.dtci("dtc1", 100, tags={"flag": "FA"})

        Meters.dtci("dtc1", 0, tags={"flag": "FB"})
        Meters.dtci("dtc1", 50, tags={"flag": "FB"})
        Meters.dtci("dtc1", 100, tags={"flag": "FB"})
        Meters.dtci("dtc1", 0, tags={"flag": "FB"})
        Meters.dtci("dtc1", 50, tags={"flag": "FB"})
        Meters.dtci("dtc1", 100, tags={"flag": "FB"})

        self.assertEquals(Meters._hash_meter["a_int"]["ai1#"].get(), 1)
        self.assertEquals(Meters._hash_meter["a_int"]["ai1#" + hca].get(), 2)
        self.assertEquals(Meters._hash_meter["a_int"]["ai1#" + hcb].get(), 3)

        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hca]._sorted_dict[0].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hca]._sorted_dict[50].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hca]._sorted_dict[100].get(), 1)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hca]._sorted_dict[500].get(), 0)

        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hcb]._sorted_dict[0].get(), 2)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hcb]._sorted_dict[50].get(), 2)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hcb]._sorted_dict[100].get(), 2)
        self.assertEquals(Meters._hash_meter["dtc"]["dtc1#" + hcb]._sorted_dict[500].get(), 0)

        # Write
        Meters.write_to_logger()

        # Upd check
        ar_udp = Meters.meters_to_udp_format(send_pid=True, send_tags=True, send_dtc=True)

        total_check = 0
        total_ok = 0
        total_ko_not_found = 0
        total_ko_multiple_found = 0
        for s_key, d_tag, v in [
            ("ai1", {}, 1),
            ("ai1", {"flag": "FA", }, 2),
            ("ai1", {"flag": "FB", }, 3),

            ("dtc1_0-50", {}, 1),
            ("dtc1_50-100", {}, 1),
            ("dtc1_100-500", {}, 1),
            ("dtc1_500-1000", {}, 0),
            ("dtc1_1000-2500", {}, 0),
            ("dtc1_2500-5000", {}, 0),
            ("dtc1_5000-10000", {}, 0),
            ("dtc1_10000-30000", {}, 0),
            ("dtc1_30000-60000", {}, 0),
            ("dtc1_60000-MAX", {}, 0),

            ("dtc1_0-50", {"flag": "FA", }, 1),
            ("dtc1_50-100", {"flag": "FA", }, 1),
            ("dtc1_100-500", {"flag": "FA", }, 1),
            ("dtc1_500-1000", {"flag": "FA", }, 0),
            ("dtc1_1000-2500", {"flag": "FA", }, 0),
            ("dtc1_2500-5000", {"flag": "FA", }, 0),
            ("dtc1_5000-10000", {"flag": "FA", }, 0),
            ("dtc1_10000-30000", {"flag": "FA", }, 0),
            ("dtc1_30000-60000", {"flag": "FA", }, 0),
            ("dtc1_60000-MAX", {"flag": "FA", }, 0),

            ("dtc1_0-50", {"flag": "FB", }, 2),
            ("dtc1_50-100", {"flag": "FB", }, 2),
            ("dtc1_100-500", {"flag": "FB", }, 2),
            ("dtc1_500-1000", {"flag": "FB", }, 0),
            ("dtc1_1000-2500", {"flag": "FB", }, 0),
            ("dtc1_2500-5000", {"flag": "FB", }, 0),
            ("dtc1_5000-10000", {"flag": "FB", }, 0),
            ("dtc1_10000-30000", {"flag": "FB", }, 0),
            ("dtc1_30000-60000", {"flag": "FB", }, 0),
            ("dtc1_60000-MAX", {"flag": "FB", }, 0),

        ]:
            total_check += 1

            # Locate it
            found = 0
            for check_key, check_tag, check_v, _, _ in ar_udp:
                if check_key == s_key and check_tag == d_tag and check_v == v:
                    found += 1

            # Check
            if found == 0:
                total_ko_not_found += 1
            elif found > 1:
                total_ko_multiple_found += 1
            else:
                total_ok += 1

        # Final
        self.assertEquals(total_ko_multiple_found, 0)
        self.assertEquals(total_ko_multiple_found, 0)
        self.assertEquals(total_ok, total_check)
        self.assertEquals(len(ar_udp), total_check)