Samples/Profiling/CreateProfilingEventsLog.py

Samples/Profiling/CreateProfilingEventsLog.py
1 # Copyright 2012 Autodesk, Inc. All rights reserved.
2 # Use of this software is subject to the terms of the Autodesk license agreement
3 # provided at the time of installation or download, or which otherwise accompanies
4 # this software in either electronic or hard copy form.
5 #
6 # Script description:
7 # Demonstrates usage of FBProfiler to collect time events information.
8 #
9 # Topic: FBProfiler, FBPlayerControl
10 #
11 from pyfbsdk import *
12 import os
13 import tempfile
14 import time
15 
16 gApp = FBApplication()
17 gProfiler = FBProfiler()
18 gPlayer = FBPlayerControl()
19 
20 # Activate collection of time events information
21 gProfiler.ActiveSampling = True
22 
23 # Play for 50 frames
24 gPlayer.Goto( FBTime(0,0,0,0) )
25 gPlayer.Play()
26 while FBSystem().LocalTime.GetFrame() < 50:
27  gApp.FlushEventQueue()
28 
29 # Wait for playing to stop
30 gPlayer.Stop()
31 while gPlayer.IsPlaying:
32  gApp.FlushEventQueue()
33 
34 # Deactivate collection of time events information
35 gProfiler.ActiveSampling = False
36 
37 lSamplesCount = gProfiler.GetEventSampleCount()
38 print "Number of samples collected: %d\n" % lSamplesCount
39 
40 if lSamplesCount > 0:
41  logPath = os.path.join( tempfile.gettempdir(), "EventsLog.txt" )
42  f = open(logPath, 'wt')
43 
44  for i in range(lSamplesCount):
45  lEvent = gProfiler.GetEventSample(i)
46  lEndEvent = gProfiler.GetEndEventSample(i)
47  lColor = lEvent.GetColor()
48 
49  if lEvent.IsSingleEvent():
50  lString = "%s;%s;%f %f %f %d %d\n" % (lEvent.GetComment(),lEvent.GetTypeName(),lColor[0],lColor[1],lColor[2],lEvent.GetThreadID(), lEvent.GetTime().Get())
51  f.write(lString)
52  elif lEndEvent:
53  lString = "%s;%s;%f %f %f %d %d %d\n" % (lEvent.GetComment(),lEvent.GetTypeName(),lColor[0],lColor[1],lColor[2],lEvent.GetThreadID(), lEvent.GetTime().Get(), lEndEvent.GetTime().Get())
54  f.write(lString)
55  f.close()
56 
57  print "File successfully saved to %s" % logPath