1 from builtins
import range
2 import maya.OpenMaya
as om
6 __all__ = [
'profilerToJSON',
'profilerToCSV',
'profilerFormatJSON']
19 def profilerToJSON(fileName, useIndex, durationMin):
21 fileName : name of file to write to disk
22 useIndex : write events using index lookup to category and name lists
23 durationMin : only write out events which have at least this minimum time duration
26 Sample code to extract profiler information and write to file in JSON format
29 > profilerToJSON('profiler_indexed.json', True, 0.0) # Index without a duration clamp
30 > profilerToJSON('profiler_nonIndexed.json', False, 10.0) # Non-Indexed with duration clamp
102 stripped =
lambda s:
"".join(i
for i
in s
if 31 < ord(i) < 127)
104 eventCount = om.MProfiler.getEventCount()
108 file = open(fileName,
"w")
116 file.write(
"\t\"version\": 1,\n")
118 file.write(
"\t\"eventCount\": " + str(eventCount) +
",\n")
120 file.write(
"\t\"cpuCount\": " + str(om.MProfiler.getNumberOfCPUs()) +
",\n")
124 om.MProfiler.getAllCategories(categories)
125 asciiString = json.dumps(categories,
True,
True)
127 file.write(
"\t\"categories\": " + asciiString +
",\n")
131 for i
in range(0, eventCount, 1):
132 eventName = om.MProfiler.getEventName(i)
133 eventName = eventName.decode(
'ascii',
'replace')
134 eventName = stripped(eventName)
135 if eventName
not in nameDict:
136 nameDict[eventName] = len(nameDict)
138 nameString = json.dumps(list(nameDict.keys()),
True,
True)
139 file.write(
'\t\"eventNames\" : ' + nameString +
",\n")
143 file.write(
'\t\"events\": [\n')
146 for i
in range(0, eventCount):
148 duration = om.MProfiler.getEventDuration(i)
149 if duration > durationMin:
150 eventsWritten = eventsWritten + 1
152 eventTime = om.MProfiler.getEventTime(i)
153 eventName = om.MProfiler.getEventName(i)
154 eventName = eventName.decode(
'ascii',
'replace')
155 eventName = stripped(eventName)
157 eventNameIndex = list(nameDict.keys()).index(eventName)
160 if om.MProfiler.getDescription(i):
161 description = om.MProfiler.getDescription(i)
163 eventCategory = om.MProfiler.getEventCategory(i)
164 eventCategoryName = om.MProfiler.getCategoryName(eventCategory)
166 eventCatagoryIndex = categories.index(eventCategoryName)
168 threadDuration = om.MProfiler.getThreadDuration(i)
170 threadId = om.MProfiler.getThreadId(i)
172 cpuId = om.MProfiler.getCPUId(i)
174 colorId = om.MProfiler.getColor(i)
182 file.write(
'\"time\" : ' + str(eventTime) +
', ')
184 file.write(
'\"nameIdx\" : ' + str(eventNameIndex) +
', ')
186 file.write(
'\"name\" : \"' + eventName +
'\", ')
187 file.write(
'\"desc\" : \"' + str(description) +
'\", ')
189 file.write(
'\"catIdx\" : ' + str(eventCatagoryIndex) +
', ')
191 file.write(
'\"category\" : \"' + eventCategoryName +
'\", ')
192 file.write(
'\"duration\" : ' + str(duration) +
', ')
193 file.write(
'\"tDuration\" : ' + str(threadDuration) +
', ')
194 file.write(
'\"tId\" : ' + str(threadId) +
', ')
195 file.write(
'\"cpuId\" : ' + str(cpuId) +
', ')
196 file.write(
'\"colorId\" : ' + str(colorId) +
'')
200 file.write(
"\t\"eventsWritten\": " + str(eventsWritten) +
"\n")
204 def profilerFormatJSON(fileName, fileName2):
206 fileName : name of file to read
207 fileName2 : name of file to write to
210 Simple utility code to read a JSON file sort and format it before
211 writing to a secondary file.
214 > profilerFormatJSON('profilerIn.json', 'profilerFormatted.json')
217 file = open(fileName,
"r")
221 result = json.load(file)
224 dump = json.dumps(result, sort_keys=
True, indent=4, separators=(
',',
': '))
226 file2 = open(fileName2,
"w")
239 def profilerToCSV(fileName, durationMin):
241 fileName : name of file to write to disk
242 useIndex : write events using index lookup to category and name lists
243 durationMin : only write out events which have at least this minimum time duration
246 Sample to output profiler event information only to CSV format.
248 > profilerToCSV('profiler.csv', 0.0)
262 stripped =
lambda s:
"".join(i
for i
in s
if 31 < ord(i) < 127)
264 eventCount = om.MProfiler.getEventCount()
268 file = open(fileName,
"w")
272 csvWriter = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC)
277 head = (
'Event Time',
'Event Name',
'Description',
'Event Category',
'Duration',
'Thread Duration',
'Thread Id',
'CPU Id',
'Color Id' )
278 csvWriter.writerow(head)
280 for i
in range(0, eventCount):
282 duration = om.MProfiler.getEventDuration(i)
283 if duration > durationMin:
285 eventTime = om.MProfiler.getEventTime(i)
286 eventName = om.MProfiler.getEventName(i)
287 eventName = eventName.decode(
'ascii',
'replace')
288 eventName = stripped(eventName)
291 if om.MProfiler.getDescription(i):
292 description = om.MProfiler.getDescription(i)
294 eventCategory = om.MProfiler.getEventCategory(i)
295 eventCategoryName = om.MProfiler.getCategoryName(eventCategory)
297 threadDuration = om.MProfiler.getThreadDuration(i)
299 threadId = om.MProfiler.getThreadId(i)
301 cpuId = om.MProfiler.getCPUId(i)
303 colorId = om.MProfiler.getColor(i)
305 row = ( eventTime, eventName, description, eventCategoryName, duration, threadDuration, threadId, cpuId, colorId )
307 csvWriter.writerow(row)
313 def initializePlugin(obj):
316 def uninitializePlugin(obj):