1#!/usr/bin/python
2
3import optparse
4import sys
5import time
6
7import adbutil
8from devices import DEVICES
9
10def parse_options(argv):
11    usage = 'Usage: %prog [options]'
12    desc = 'Example: %prog'
13    parser = optparse.OptionParser(usage=usage, description=desc)
14    parser.add_option("-c", dest='clear', action="store_true")
15    parser.add_option("-d", dest='device', action="store",)
16    parser.add_option("-t", dest='trace', action="store_true")
17    options, categories = parser.parse_args(argv[1:])
18    return (options, categories)
19
20def clear_data(device = None):
21    if device != None:
22        dev = DEVICES[device]
23        adbutil.root(dev)
24        adbutil.pm(dev, "clear", "com.android.benchmark")
25    else:
26        for name, dev in DEVICES.iteritems():
27            print("Clearing " + name)
28            adbutil.root(dev)
29            adbutil.pm(dev, "clear", "com.android.benchmark")
30
31def start_device(name, dev):
32    print("Go " + name + "!")
33    try:
34        adbutil.am(dev, "force-stop", "com.android.benchmark")
35        adbutil.wake(dev)
36        adbutil.am(dev, "start",
37            ["-n", "\"com.android.benchmark/.app.RunLocalBenchmarksActivity\"",
38            "--eia", "\"com.android.benchmark.EXTRA_ENABLED_BENCHMARK_IDS\"", "\"0,1,2,3,4,5,6\"",
39            "--ei", "\"com.android.benchmark.EXTRA_RUN_COUNT\"", "\"5\""])
40    except adbutil.AdbError:
41        print "Couldn't launch " + name + "."
42
43def start_benchmark(device, trace):
44    if device != None:
45        start_device(device, DEVICES[device])
46        if trace:
47            time.sleep(3)
48            adbutil.trace(DEVICES[device])
49    else:
50        if trace:
51            print("Note: -t only valid with -d, can't trace")
52        for name, dev in DEVICES.iteritems():
53            start_device(name, dev)
54
55def main():
56    options, categories = parse_options(sys.argv)
57    if options.clear:
58        print options.device
59        clear_data(options.device)
60    else:
61        start_benchmark(options.device, options.trace)
62
63
64if __name__ == "__main__":
65    main()
66