pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 524 forks source link

After building the FSM and HFSM, how to benchmark the performance , like converage speed or Traverse the FSM #524

Closed WillianXu117 closed 3 years ago

aleneum commented 3 years ago

Hello @WillianXu117,

I am not entirely sure what you are referring to. If you want to measure how long it took to process a trigger, you can just get the timestamps before and after the trigger call:

from transitions import Machine
import time

def heavy_processing():
  time.sleep(0.1)

m = Machine(states=['A', 'B'], initial='A', after_state_change=heavy_processing)
start_time = time.time()
m.to_B()
print(f"Trigger processing took {round((time.time() - start_time) * 1000.0)} ms")
>>> Trigger processing took 107 ms

You can do the timekeeping in callbacks as well. Check the callback execution order in the README for more details.

from transitions import Machine
import time

start_time = 0

def heavy_processing():
    time.sleep(0.1)

def set_time():
    global start_time
    start_time = time.time()

def log_time():
    global start_time
    print(f"Trigger processing took {round((time.time() - start_time) * 1000.0)} ms")

m = Machine(states=['A', 'B'], initial='A', prepare_event=set_time, after_state_change=heavy_processing,
            finalize_event=log_time)

m.to_B()

Questions about how transitions can be used should be posted on Stack Overflow. Your question gains higher visibility since most developers look for help there. The targeted community is larger; Some people will even help you to formulate a good question. People get 'rewarded' with 'reputation' to help you. You also gain reputation in case this questions pops up more frequently. It's a win-win situation. Make sure to tag your question with the pytransitions tag.

aleneum commented 3 years ago

Closing this since there has been no feedback for over 2 weeks. Feel free to comment if the issue still persists. I will reopen the issue if necessary.