Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bettery testing scripts #42

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

MarcLeclair
Copy link
Contributor

Added scripts for testing battery usage locally

@MarcLeclair MarcLeclair requested a review from kycn May 20, 2024 21:55
Copy link

@kycn kycn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments. Looks good overall! Thanks a lot for bringing these in-tree :)

Comment on lines +27 to +54
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/search_box_text']/@bounds)" "$XML_FILE")
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/tab_switcher_button']/@bounds)" "$XML_FILE")
sleep 1

# Extract and calculate center coordinates in a single line
TOOLBAR_X_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TOOLBAR_Y_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))
TABS_TRAY_BUTTON_X_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TABS_TRAY_BUTTON_Y_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

# tap to open tabs tray
adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

# take ss
adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/new_tab_view_button']/@bounds)" "$XML_FILE")
sleep 1

ADD_TAB_BUTTON_X_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
ADD_TAB_BUTTON_Y_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

rm window_dump.xml
adb shell input keyevent KEYCODE_BACK
sleep 1

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a more readable version of this, we can use that here. This is for Firefox, you'd need to adjust the node ids to make it work for chrome.

# calculate toolbar coordinates
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/toolbar']/@bounds)" "$XML_FILE")
sleep 1

toolbar_x1=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
toolbar_x2=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_toolbar_x=$(($toolbar_x1+$toolbar_x2))

toolbar_y1=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
toolbar_y2=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_toolbar_y=$(($toolbar_y1+$toolbar_y2))

TOOLBAR_X_COORDINATE=$(($sum_toolbar_x/2))
TOOLBAR_Y_COORDINATE=$(($sum_toolbar_y/2))

# calculate tabs tray coordinates
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/counter_box']/@bounds)" "$XML_FILE")
sleep 1

tabs_tray_x1=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
tabs_tray_x2=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_tabs_tray_x=$(($tabs_tray_x1+$tabs_tray_x2))

tabs_tray_y1=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
tabs_tray_y2=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_tabs_tray_y=$(($tabs_tray_y1+$tabs_tray_y2))

TABS_TRAY_BUTTON_X_COORDINATE=$(($sum_tabs_tray_x/2))
TABS_TRAY_BUTTON_Y_COORDINATE=$(($sum_tabs_tray_y/2))

adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

# calculate new tab button coordinates
ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@content-desc='Add tab']/@bounds)" "$XML_FILE")
sleep 1

add_tab_x1=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
add_tab_x2=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_add_tab_x=$(($add_tab_x1+$add_tab_x2))

add_tab_y1=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
add_tab_y2=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_add_tab_y=$(($add_tab_y1+$add_tab_y2))

ADD_TAB_BUTTON_X_COORDINATE=$(($sum_add_tab_x/2))
ADD_TAB_BUTTON_Y_COORDINATE=$(($sum_add_tab_y/2))

rm window_dump.xml

# go back to main page to start testing
adb shell input keyevent KEYCODE_BACK
sleep 1

Comment on lines +7 to +16
URL_REDDIT="https://www.reddit.com/r/all"
URL_INSTAGRAM="https://www.instagram.com/"
URL_INSTAGRAM_EXPLORE="https://www.instagram.com/explore"
URL_FACEBOOK="https://www.facebook.com"
URL_TMZ="https://tmz.com"
URL_PEREZ="https://perezhilton.com"
URL_WIKI="https://wikipedia.org/wiki/Student%27s_t-test",
URL_SEARCH_FOX="https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Histograms.json",
URL_BUZZFEED="https://buzzfeed.com"
URL_CNN="https://cnn.com"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these urls do not behave well with the script due to its prompts for cookie banners, ads, etc. I'd suggest to test them out and remove the ones that do not behave well.

Comment on lines +7 to +16
URL_REDDIT="https://www.reddit.com/r/all"
URL_INSTAGRAM="https://www.instagram.com/"
URL_INSTAGRAM_EXPLORE="https://www.instagram.com/explore"
URL_FACEBOOK="https://www.facebook.com"
URL_TMZ="https://tmz.com"
URL_PEREZ="https://perezhilton.com"
URL_WIKI="https://wikipedia.org/wiki/Student%27s_t-test",
URL_SEARCH_FOX="https://searchfox.org/mozilla-central/source/toolkit/components/telemetry/Histograms.json",
URL_BUZZFEED="https://buzzfeed.com"
URL_CNN="https://cnn.com"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above for Chrome, better to test the urls and delete the problematic ones.

Comment on lines +44 to +70
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/toolbar']/@bounds)" "$XML_FILE")
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/counter_box']/@bounds)" "$XML_FILE")
sleep 1

# Extract and calculate center coordinates in a single line
TOOLBAR_X_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TOOLBAR_Y_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))
TABS_TRAY_BUTTON_X_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TABS_TRAY_BUTTON_Y_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

# tap to open tabs tray
adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

# take ss
adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@content-desc='Add tab']/@bounds)" "$XML_FILE")
sleep 1

ADD_TAB_BUTTON_X_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
ADD_TAB_BUTTON_Y_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

rm window_dump.xml
adb shell input keyevent KEYCODE_BACK
sleep 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a rewrite as the above comment for chrome would be better here.

# calculate toolbar coordinates
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/toolbar']/@bounds)" "$XML_FILE")
sleep 1

toolbar_x1=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
toolbar_x2=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_toolbar_x=$(($toolbar_x1+$toolbar_x2))

toolbar_y1=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
toolbar_y2=$(($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_toolbar_y=$(($toolbar_y1+$toolbar_y2))

TOOLBAR_X_COORDINATE=$(($sum_toolbar_x/2))
TOOLBAR_Y_COORDINATE=$(($sum_toolbar_y/2))

# calculate tabs tray coordinates
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='org.mozilla.firefox:id/counter_box']/@bounds)" "$XML_FILE")
sleep 1

tabs_tray_x1=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
tabs_tray_x2=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_tabs_tray_x=$(($tabs_tray_x1+$tabs_tray_x2))

tabs_tray_y1=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
tabs_tray_y2=$(($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_tabs_tray_y=$(($tabs_tray_y1+$tabs_tray_y2))

TABS_TRAY_BUTTON_X_COORDINATE=$(($sum_tabs_tray_x/2))
TABS_TRAY_BUTTON_Y_COORDINATE=$(($sum_tabs_tray_y/2))

adb shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

# calculate new tab button coordinates
ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@content-desc='Add tab']/@bounds)" "$XML_FILE")
sleep 1

add_tab_x1=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}')))
add_tab_x2=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')))
sum_add_tab_x=$(($add_tab_x1+$add_tab_x2))

add_tab_y1=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}')))
add_tab_y2=$(($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')))
sum_add_tab_y=$(($add_tab_y1+$add_tab_y2))

ADD_TAB_BUTTON_X_COORDINATE=$(($sum_add_tab_x/2))
ADD_TAB_BUTTON_Y_COORDINATE=$(($sum_add_tab_y/2))

rm window_dump.xml

# go back to main page to start testing
adb shell input keyevent KEYCODE_BACK
sleep 1

Comment on lines +48 to +50
get_battery_status
capture_processes
compare_and_log_changes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can parallelize these to make sure we collect the date simultaneously and get rid of any potential overhead of the callstack:

   get_battery_status &
   capture_processes &
   compare_and_log_changes &

would probably work, making sure to terminate those forked background programs.



echo "\n--- CPU Usage at $(date) ---" >> "$LOG_FILE"
adb shell top -n 1 | grep "$PACKAGE" >> "$LOG_FILE"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can utilize ps command here.

adb shell ps -p [PID] -o time= # cpu time  
adb shell ps -p [PID] -o cpu= # cpu core running on
adb shell ps -p [PID] -o %cpu= #  percentage of cpu time used
adb shell ps -p [PID] -o c= #  total %CPU used since start

or collect all at once if desired

adb shell ps -p "$1" -o cpu=,c=,time=,%cpu=

ps can give you process name as well: just add name= to the params.

process_match = process_regex.match(line)
if process_match:

process_name = process_match.group(1).strip()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

important to mention:
We are spawning an extension process as well that follows the same naming convention with content process, i.e. ...:tabX. Just fyi, in case this impacts any of your analysis.

Comment on lines +7 to +16
URL_ALL_RECIPE="https://www.allrecipes.com/"
URL_AMAZON="https://www.amazon.com/"
URL_BING="https://www.bing.com/"
URL_BING_SEARCH="https://www.bing.com/search?q=restaurants+in+exton+pa+19341"
URL_BOOKING="https://www.booking.com/"
URL_DAILY_MAIL="https://www.dailymail.co.uk/sciencetech/article-9749081/Experts-say-Hubble-repair-despite-NASA-insisting-multiple-options-fix.html"
URL_IMDB="https://m.imdb.com/"
URL_MICROSOFT="https://support.microsoft.com/en-us"
URL_STACKOVERFLOW="https://stackoverflow.com/"
URL_WIKIPEDIA="https://en.m.wikipedia.org/wiki/Main_Page"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above comments.

Comment on lines +27 to +53
TOOLBAR_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/search_box_text']/@bounds)" "$XML_FILE")
TABS_TRAY_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/tab_switcher_button']/@bounds)" "$XML_FILE")
sleep 1

# Extract and calculate center coordinates in a single line
TOOLBAR_X_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TOOLBAR_Y_COORDINATE=$((($(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TOOLBAR_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))
TABS_TRAY_BUTTON_X_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
TABS_TRAY_BUTTON_Y_COORDINATE=$((($(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$TABS_TRAY_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

# tap to open tabs tray
adb -s R5CTB1NTHSF shell input tap $TABS_TRAY_BUTTON_X_COORDINATE $TABS_TRAY_BUTTON_Y_COORDINATE
sleep 2

# take ss
adb -s R5CTB1NTHSF shell uiautomator dump
adb -s R5CTB1NTHSF pull /sdcard/window_dump.xml

ADD_TAB_BUTTON_BOUNDS=$(xmllint --xpath "string(//node[@resource-id='com.android.chrome:id/new_tab_view_button']/@bounds)" "$XML_FILE")
sleep 1

ADD_TAB_BUTTON_X_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $1}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $1}')) / 2))
ADD_TAB_BUTTON_Y_COORDINATE=$((($(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $2}' | awk -F',' '{print $2}') + $(echo "$ADD_TAB_BUTTON_BOUNDS" | awk -F'[][]' '{print $4}' | awk -F',' '{print $2}')) / 2))

rm window_dump.xml
adb -s R5CTB1NTHSF shell input keyevent KEYCODE_BACK
sleep 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above commands

simple_browsing_single_site $URL_WIKIPEDIA

# uncomment this line if you want to stop the app
adb -s R5CTB1NTHSF shell am force-stop $PACKAGE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to either make the device selection part generic or add a warning comment to replace the device id for others' notice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants