Hi guys,

I thought this might be useful for some of you, although I'm sure it could
be made a bit cleaner. Here is how I profile my Wave bot:



def OnWaveletSelfAdded(event, wavelet):
   blah

def OnBlipSubmitted(event, wavelet):
   blah

def OnDocumentChanged(event, wavelet):
   blah

def OnGadgetStateChanged(event, wavelet):
   blah

def profileEvent(event, wavelet):
   """Run OnDocumentChanged with profiling"""
   import cProfile, pstats, StringIO
   prof = cProfile.Profile()
   if event.type == "WAVELET_SELF_ADDED":
     prof = prof.runctx("OnWaveletSelfAdded(event, wavelet)", globals(),
locals())
   elif event.type == "BLIP_SUBMITTED":
     prof = prof.runctx("OnBlipSubmitted(event, wavelet)", globals(),
locals())
   elif event.type == "DOCUMENT_CHANGED":
     prof = prof.runctx("OnDocumentChanged(event, wavelet)", globals(),
locals())
   elif event.type == "GADGET_STATE_CHANGED":
     prof = prof.runctx("OnGadgetStateChanged(event, wavelet)", globals(),
locals())
   stream = StringIO.StringIO()
   stats = pstats.Stats(prof, stream=stream)
   stats.sort_stats("time")  # Or cumulative
   stats.print_stats(80)  # 80 = how many to print
   # The rest is optional.
   # stats.print_callees()
   # stats.print_callers()
   logging.info("Profile data:\n%s", stream.getvalue())


if __name__ == '__main__':
  myRobot = robot.Robot(BOT_NAME, image_url=IMAGE_URL,
profile_url=PROFILE_URL)

  # Set this variable to enable/disable profiling
  profiling = True

  if profiling:
    myRobot.register_handler(events.WaveletSelfAdded, profileEvent)
    myRobot.register_handler(events.BlipSubmitted, profileEvent)
    myRobot.register_handler(events.DocumentChanged, profileEvent)
    myRobot.register_handler(events.GadgetStateChanged, profileEvent)
  else:
    myRobot.register_handler(events.WaveletSelfAdded, OnWaveletSelfAdded)
    myRobot.register_handler(events.BlipSubmitted, OnBlipSubmitted)
    myRobot.register_handler(events.DocumentChanged, OnDocumentChanged)
    myRobot.register_handler(events.GadgetStateChanged,
OnGadgetStateChanged)

  # Run robot
  appengine_robot_runner.run(myRobot)



The downside of this method is that you have to register the callbacks
twice, but at least you can turn profiling on and off very easily. The
results of the profiling are shown in the logs.



Raphaël

-- 
You received this message because you are subscribed to the Google Groups 
"Google Wave API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-wave-api?hl=en.

Reply via email to