Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
analyse.py
#!/usr/bin/env python3
from cProfile import label
from datetime import timedelta
import sys
import pandas
import glob
from perfetto.batch_trace_processor.api import BatchTraceProcessor
import matplotlib.pyplot as plt
import numpy as np
# trendline https://www.statology.org/matplotlib-trendline/
def drawTrendLine(dataFrame, xKey, yKey, color, label):
ts = pandas.to_numeric(dataFrame[xKey])
mem = pandas.to_numeric(dataFrame[yKey])
coefficients = np.polyfit(ts, mem, 1)
b = coefficients[0]
print(f'{label} has coefficient {b:.20f}')
p = np.poly1d(coefficients)
plt.plot(ts, p(ts), color=color, label=label)
plt.legend()
tracesFolder = sys.argv[1]
packageName = sys.argv[2] if len(sys.argv) > 3 else 'com.mapbox.navigation.examples'
files = glob.glob(f'{tracesFolder}/*.perfetto-trace')
if (len(files) == 0):
print(f"no trace files found in {tracesFolder}")
exit()
print("loading:" + ' '.join(files))
with BatchTraceProcessor(files) as btp:
rssMemorySets = btp.query(f"select c.ts / 1000000 as timestamp, c.value / 1000 as rss from counter as c left join process_counter_track as t on c.track_id = t.id left join process as p using (upid) where t.name like 'mem.rss' and p.name like '{packageName}' order by c.ts")
rssMemory = pandas.concat(rssMemorySets)
rssMemory.sort_values(by=['timestamp'], inplace=True)
print(rssMemory.head())
rssMemory.plot(x='timestamp', y='rss')
drawTrendLine(rssMemory, xKey='timestamp', yKey='rss', color="red", label="rss trendline")
startOfTheTrace = rssMemory['timestamp'].iloc[0]
endOfTheTrace = rssMemory['timestamp'].iloc[-1]
traceDurationMilliseconds = (endOfTheTrace - startOfTheTrace)
delta = timedelta(milliseconds=traceDurationMilliseconds)
print("trace duration " + str(delta))
# heapSizeMemorySets = btp.query("select c.ts / 1000000 as timestamp, c.value / 1000 as heap from counter as c left join process_counter_track as t on c.track_id = t.id left join process as p using (upid) where t.name like 'Heap size (KB)' and p.name like 'com.mapbox.navigation.examples' order by c.ts")
# heapSize = pandas.concat(heapSizeMemorySets)
# heapSize.sort_values(by=['timestamp'], inplace=True)
# print(heapSize.head())
# heapSize.plot(x='timestamp', y='heap')
# drawTrendLine(heapSize, xKey='timestamp', yKey='heap', color="yellow", label="heap trendline")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment