def test_statetransition_immediate(self): pta = PTA.from_json(example_json_1) pta.set_random_energy_model() pta.state["IDLE"].power.value = 9 cg = get_simulated_accountingmethod( "static_statetransition_immediate")(pta, 1000000, "uint8_t", "uint8_t", "uint8_t", "uint8_t") cg.current_state = pta.state["IDLE"] cg.sleep(7) self.assertEqual(cg.get_energy(), 9 * 7) pta.transitions[1].energy.value = 123 cg.pass_transition(pta.transitions[1]) self.assertEqual(cg.get_energy(), 9 * 7 + 123) cg.pass_transition(pta.transitions[1]) self.assertEqual(cg.get_energy(), (9 * 7 + 123 + 123) % 256) cg = get_simulated_accountingmethod( "static_statetransition_immediate")(pta, 100000, "uint8_t", "uint8_t", "uint8_t", "uint8_t") cg.current_state = pta.state["IDLE"] cg.sleep(7) self.assertEqual(cg.get_energy(), 0) cg.sleep(15) self.assertEqual(cg.get_energy(), 90) cg.sleep(90) self.assertEqual(cg.get_energy(), 900 % 256) cg = get_simulated_accountingmethod( "static_statetransition_immediate")(pta, 100000, "uint8_t", "uint8_t", "uint8_t", "uint16_t") cg.current_state = pta.state["IDLE"] cg.sleep(7) self.assertEqual(cg.get_energy(), 0) cg.sleep(15) self.assertEqual(cg.get_energy(), 90) cg.sleep(90) self.assertEqual(cg.get_energy(), 900) pta.state["IDLE"].power.value = 9 # -> 90 uW pta.transitions[1].energy.value = 1 # -> 100 pJ cg = get_simulated_accountingmethod( "static_statetransition_immediate")(pta, 1000000, "uint8_t", "uint8_t", "uint8_t", "uint8_t", 1e-5, 1e-5, 1e-10) cg.current_state = pta.state["IDLE"] cg.sleep(10) # 10 us self.assertEqual(cg.get_energy(), 90 * 10) cg.pass_transition(pta.transitions[1]) self.assertAlmostEqual(cg.get_energy(), 90 * 10 + 100, places=0) cg.pass_transition(pta.transitions[1]) self.assertAlmostEqual(cg.get_energy(), 90 * 10 + 100 + 100, places=0)
def test_from_json(self): pta = PTA.from_json(example_json_1) self.assertEqual(pta.parameters, ["datarate", "txbytes", "txpower"]) self.assertEqual(pta.state["UNINITIALIZED"].name, "UNINITIALIZED") self.assertEqual(pta.state["IDLE"].name, "IDLE") self.assertEqual(pta.state["TX"].name, "TX") self.assertEqual(len(pta.transitions), 5) self.assertEqual(pta.transitions[0].name, "init") self.assertEqual(pta.transitions[1].name, "init") self.assertEqual(pta.transitions[2].name, "setTxPower") self.assertEqual(pta.transitions[3].name, "send") self.assertEqual(pta.transitions[4].name, "txComplete")
def test_state_immediate(self): pta = PTA.from_json(example_json_1) pta.set_random_energy_model() pta.state["IDLE"].power.value = 9 cg = get_simulated_accountingmethod("static_state_immediate")( pta, 1000000, "uint8_t", "uint8_t", "uint8_t", "uint8_t") cg.current_state = pta.state["IDLE"] cg.sleep(7) self.assertEqual(cg.get_energy(), 9 * 7) pta.transitions[1].energy.value = 123 cg.pass_transition(pta.transitions[1]) self.assertEqual(cg.get_energy(), 9 * 7) cg.pass_transition(pta.transitions[1]) self.assertEqual(cg.get_energy(), 9 * 7)
def test_get_X_expensive_state(self): pta = PTA.from_json(example_json_1) self.assertEqual(pta.get_least_expensive_state(), pta.state["IDLE"]) self.assertEqual(pta.get_most_expensive_state(), pta.state["TX"]) # self.assertAlmostEqual(pta.min_duration_until_energy_overflow(), (2**32 - 1) * 1e-12 / 10e-3, places=9) # self.assertAlmostEqual(pta.min_duration_until_energy_overflow(energy_granularity=1e-9), (2**32 - 1) * 1e-9 / 10e-3, places=9) self.assertAlmostEqual( pta.max_duration_until_energy_overflow(), (2**32 - 1) * 1e-12 / 5e-6, places=9, ) self.assertAlmostEqual( pta.max_duration_until_energy_overflow(energy_granularity=1e-9), (2**32 - 1) * 1e-9 / 5e-6, places=9, )
def test_from_json_dfs_arg(self): pta = PTA.from_json(example_json_1) self.assertEqual( sorted(dfs_tran_to_name(pta.dfs(1), False)), [["init", "init"], ["init", "send"], ["init", "setTxPower"]], ) self.assertEqual( sorted(dfs_tran_to_name(pta.dfs(1, with_arguments=True), True)), [ [("init", ()), ("init", ())], [("init", ()), ("send", ('"foo"', 3))], [("init", ()), ("send", ('"hodor"', 5))], [("init", ()), ("setTxPower", (10, ))], [("init", ()), ("setTxPower", (20, ))], [("init", ()), ("setTxPower", (30, ))], ], )
def test_from_json_function(self): pta = PTA.from_json(example_json_1) self.assertEqual( pta.state["TX"].get_energy(1000, { "datarate": 10, "txbytes": 6, "txpower": 10 }), 1000 * (10000 + 2 * 10), ) self.assertEqual( pta.transitions[4].get_timeout({ "datarate": 10, "txbytes": 6, "txpower": 10 }), 500 + 16 * 6, )
def test_from_json_dfs_param(self): pta = PTA.from_json(example_json_1) no_param = {"datarate": None, "txbytes": None, "txpower": 10} param_tx3 = {"datarate": None, "txbytes": 3, "txpower": 10} param_tx5 = {"datarate": None, "txbytes": 5, "txpower": 10} param_txp10 = {"datarate": None, "txbytes": None, "txpower": 10} param_txp20 = {"datarate": None, "txbytes": None, "txpower": 20} param_txp30 = {"datarate": None, "txbytes": None, "txpower": 30} self.assertEqual( sorted( dfs_tran_to_name( pta.dfs(1, with_arguments=True, with_parameters=True), True, True)), [ [("init", (), no_param), ("init", (), no_param)], [("init", (), no_param), ("send", ('"foo"', 3), param_tx3)], [("init", (), no_param), ("send", ('"hodor"', 5), param_tx5)], [("init", (), no_param), ("setTxPower", (10, ), param_txp10)], [("init", (), no_param), ("setTxPower", (20, ), param_txp20)], [("init", (), no_param), ("setTxPower", (30, ), param_txp30)], ], )
k, v = kv.split("=") try: arg_dict[k] = float(v) except ValueError: arg_dict[k] = v args.with_substates = arg_dict if args.plot_traces: plot_traces(preprocessed_data, args.plot_traces) if raw_data.preprocessing_stats["num_valid"] == 0: print("No valid data available. Abort.", file=sys.stderr) sys.exit(2) if pta is None and raw_data.pta is not None: pta = PTA.from_json(raw_data.pta) by_name, parameters, arg_count = pta_trace_to_aggregate( preprocessed_data, ignored_trace_indexes ) filter_aggregate_by_param(by_name, parameters, args.filter_param) if args.param_shift: param_shift = dfatool.cli.parse_param_shift(args.param_shift) shift_param_in_aggregate(by_name, parameters, param_shift) detect_outliers_in_aggregate( by_name, z_limit=args.z_score, remove_outliers=args.remove_outliers )