skip to Main Content

I have multiple Android application that require retrieving the CPU and memory average when running on a physical device. All applications can be open within Android Studio but not all are native, there are Flutter and React Native projects as well. Android Studio has a profiler but doesn’t allow any export of usage metrics.

I have experience with the Xcode Instruments which allows to gather any metrics like CPU, RAM, network within a table view that you can copy and paste into Excel. After that, any graphs can be made, alongside calculations for averages, minimums, maximums, etc. This is the idea.

If there isn’t functionality within Android Studio, is there with another program which can hook into the device metrics?

2

Answers


  1. Chosen as BEST ANSWER

    Unfortunately I haven't managed to find a tool to streamline this task. However, as a universal method for any Android app, you can use an sh script to capture and export the information into an .csv file through the use of adb shell. Here is mine:

    # record.sh
    
    file="$1.csv"
    echo "cpu%,mem%" > $file
    pid=`adb shell pidof $1`
    while [ true ]
    do
      res=`adb shell top -b -n 1 -d 0.1 -o %CPU,%MEM -p $pid`
      end=`sed -e 's#.*M()#1#' <<< $res`
      cpu=`cut -d " " -f1 <<< $end`
      mem=`cut -d " " -f2 <<< $end`
      echo "[$(date +"%T")] $pid: $cpu, $mem"
      echo "$cpu,$mem" >> $file
      sleep 1
    done
    

    Above is the implementation where the only argument is the package name (e.g. sh record.sh com.facebook.katana). The script will create a .csv file at the same directory which will be named after the app package name (e.g. com.facebook.katana.csv). CPU and memory usage is written to the .csv every second but that is customisable by changing the sleep argument.

    Below is a result of what the data looks like after opening it in Excel, in addition to some graphs that are possible with this data.

    enter image description here


  2. For CPU usage, you can record a System Trace in Android Studio profiler and export the trace file, which can be analyzed using the Perfetto Trace Processor, a SQL-interface for querying data events and slices. See https://perfetto.dev/docs/data-sources/cpu-freq#sql for the CPU-related SQL tables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search