1#!/bin/sh
2#
3# Script that measures statsd's PSS under an increasing number of metrics.
4
5# Globals.
6pss=""
7pid=""
8
9# Starts the loadtest.
10start_loadtest() {
11    echo "Starting loadtest"
12    adb shell am start -n com.android.statsd.loadtest/.LoadtestActivity --es "type" "start"
13}
14
15# Stops the loadtest.
16stop_loadtest() {
17    echo "Stopping loadtest"
18    adb shell am start -n com.android.statsd.loadtest/.LoadtestActivity --es "type" "stop"
19}
20
21# Sets the metrics replication.
22# Arguments:
23#   $1: The replication factor.
24set_replication() {
25    adb shell am start -n com.android.statsd.loadtest/.LoadtestActivity --es "type" "set_replication" --ei "replication" "${1}"
26    echo "Replication set to ${1}"
27}
28
29# Reads statsd's pid and PSS.
30update_pid_and_pss() {
31    # Command that reads the PSS for statsd. This also gives us its pid.
32    get_mem=$(adb shell dumpsys meminfo |grep statsd)
33    # Looks for statsd's pid.
34    regex="([0-9,]+)K: statsd \(pid ([0-9]+)\).*"
35    if [[ $get_mem =~ $regex ]]; then
36        pss=$(echo "${BASH_REMATCH[1]}" | tr -d , | sed 's/\.//g')
37        pid=$(echo "${BASH_REMATCH[2]}")
38    else
39        echo $cmd doesnt match $regex
40    fi
41}
42
43# Kills statsd.
44# Assumes the pid has been set.
45kill_statsd() {
46    echo "Killing statsd (pid ${pid})"
47    adb shell kill -9 "${pid}"
48}
49
50# Main loop.
51main() {
52    start_time=$(date +%s)
53    values=()
54    stop_loadtest
55
56    echo ""
57    echo "********************* NEW LOADTEST ************************"
58    update_pid_and_pss
59    for replication in 1 2 4 8 16 32 64 128 256 512 1024 2048 4096
60    do
61        echo "**** Starting test at replication ${replication} ****"
62
63        # (1) Restart statsd. This will ensure its state is empty.
64        kill_statsd
65        sleep 3 # wait a bit for it to restart
66        update_pid_and_pss
67        echo "Before the test, statsd's PSS is ${pss}"
68
69        # (2) Set the replication.
70        set_replication "${replication}"
71        sleep 1 # wait a bit
72
73        # (3) Start the loadtest.
74        start_loadtest
75
76        # (4) Wait several seconds, then read the PSS.
77        sleep 100 && update_pid_and_pss
78        echo "During the test, statsd's PSS is ${pss}"
79        values+=(${pss})
80
81        echo "Values: ${values[@]}"
82
83        # (5) Stop loadtest.
84        stop_loadtest
85        sleep 2
86
87        echo ""
88    done
89
90    end_time=$(date +%s)
91    echo "Completed loadtest in $((${end_time} - ${start_time})) seconds."
92
93    values_as_str=$(IFS=$'\n'; echo "${values[*]}")
94    echo "The PSS values are:"
95    echo "${values_as_str}"
96    echo ""
97}
98
99main
100