def test_complex_gate_optimization(complex_gate, complex_gate_opt): """Make sure the optimized version runs identically to the unoptimized.""" sg, sg_opt = complex_gate, complex_gate_opt sim1 = SensorGraphSimulator(sg) sim1.stop_condition("run_time 10 minutes") sg.load_constants() sim1.record_trace() sim1.run() sim2 = SensorGraphSimulator(sg_opt) sim2.stop_condition("run_time 10 minutes") sg_opt.load_constants() sim2.record_trace() sim2.run() assert len(sim1.trace) == 0 assert len(sim2.trace) == 0 sim1.step(DataStream.FromString("system input 1034"), 1) sim2.step(DataStream.FromString("system input 1034"), 1) sim1.run() sim2.run() print("Unoptimized Output") for x in sim1.trace: print("%08d %s: %d" % (x.raw_time, DataStream.FromEncoded(x.stream), x.value)) print("\nOptimized Output") for x in sim2.trace: print("%08d %s: %d" % (x.raw_time, DataStream.FromEncoded(x.stream), x.value)) assert len(sim1.trace) == 4 assert len(sim2.trace) == 4 assert sim1.trace == sim2.trace #Check that number of trigger streamer commands is same for optimized and unoptimized trigger_nodes = [ node for node in complex_gate.nodes if node.func_name == 'trigger_streamer' ] trigger_nodes_opt = [ node for node in complex_gate_opt.nodes if node.func_name == 'trigger_streamer' ] assert len(trigger_nodes) == len(trigger_nodes_opt)
def test_user_tick_optimization(usertick_gate, usertick_gate_opt): """Make sure the optimized version runs identically to the unoptimized.""" sg, sg_opt = usertick_gate, usertick_gate_opt for node in sg_opt.nodes: print(node) sim1 = SensorGraphSimulator(sg) sim1.stop_condition("run_time 10 minutes") sg.load_constants() sim1.record_trace() sim1.run() sim2 = SensorGraphSimulator(sg_opt) sim2.stop_condition("run_time 10 minutes") sg_opt.load_constants() sim2.record_trace() sim2.run() assert len(sim1.trace) == 0 assert len(sim2.trace) == 0 sim1.step(DataStream.FromString("system input 1034"), 1) sim2.step(DataStream.FromString("system input 1034"), 1) sim1.run() sim2.run() print("Unoptimized Output") for x in sim1.trace: print("%08d %s: %d" % (x.raw_time, DataStream.FromEncoded(x.stream), x.value)) print("\nOptimized Output") for x in sim2.trace: print("%08d %s: %d" % (x.raw_time, DataStream.FromEncoded(x.stream), x.value)) assert len(sim1.trace) == 4 assert len(sim2.trace) == 4 assert sim1.trace == sim2.trace
def main(argv=None): """Main entry point for iotile sensorgraph simulator. This is the iotile-sgrun command line program. It takes an optional set of command line parameters to allow for testing. Args: argv (list of str): An optional set of command line parameters. If not passed, these are taken from sys.argv. """ if argv is None: argv = sys.argv[1:] try: executor = None parser = build_args() args = parser.parse_args(args=argv) model = DeviceModel() parser = SensorGraphFileParser() parser.parse_file(args.sensor_graph) parser.compile(model) if not args.disable_optimizer: opt = SensorGraphOptimizer() opt.optimize(parser.sensor_graph, model=model) graph = parser.sensor_graph sim = SensorGraphSimulator(graph) for stop in args.stop: sim.stop_condition(stop) for watch in args.watch: watch_sel = DataStreamSelector.FromString(watch) graph.sensor_log.watch(watch_sel, watch_printer) # If we are semihosting, create the appropriate executor connected to the device if args.semihost_device is not None: executor = SemihostedRPCExecutor(args.port, args.semihost_device) sim.rpc_executor = executor for mock in args.mock_rpc: slot, rpc_id, value = process_mock_rpc(mock) sim.rpc_executor.mock(slot, rpc_id, value) for stim in args.stimulus: sim.stimulus(stim) graph.load_constants() if args.trace is not None: sim.record_trace() try: if args.connected: sim.step(user_connected, 8) sim.run(accelerated=not args.realtime) except KeyboardInterrupt: pass if args.trace is not None: sim.trace.save(args.trace) finally: if executor is not None: executor.hw.close() return 0