Christopher Wiley | 2f48d95 | 2013-02-22 09:51:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | import logging |
| 8 | import logging.handlers |
| 9 | import os |
| 10 | import subprocess |
| 11 | import sys |
| 12 | import time |
| 13 | import urllib2 |
| 14 | |
| 15 | import_path = os.environ.get("SYSROOT", "") + "/usr/lib/flimflam/test" |
| 16 | sys.path.append(import_path) |
| 17 | import flimflam |
| 18 | |
| 19 | class SpeedTester(object): |
| 20 | @staticmethod |
| 21 | def CommandOutput(command): |
| 22 | p = subprocess.Popen(command, stdout=subprocess.PIPE) |
| 23 | return p.communicate()[0] |
| 24 | |
| 25 | def __init__(self): |
| 26 | self.keyvals = [] |
| 27 | self.flim = None |
| 28 | |
| 29 | def write_perf_keyval(self, d): |
| 30 | self.keyvals.append(d) |
| 31 | logging.info(str(d).replace('%', '%%')) |
| 32 | |
| 33 | def ExtractPerfField(self, field): |
| 34 | return [kv[field] |
| 35 | for kv in self.keyvals |
| 36 | if field in kv] |
| 37 | |
| 38 | def FetchUrl(self, url_pattern= |
| 39 | 'http://testing-chargen.appspot.com/download?size=%d', |
| 40 | size=10, |
| 41 | label=None): |
| 42 | """Fetch the URL, wirte a dictionary of performance data.""" |
| 43 | |
| 44 | if not label: |
| 45 | raise Exception('no label supplied') |
| 46 | |
| 47 | url = url_pattern % size |
| 48 | logging.info('Fetching: %s', url) |
| 49 | start_time = time.time() |
| 50 | result = urllib2.urlopen(url) |
| 51 | bytes_received = len(result.read()) |
| 52 | fetch_time = time.time() - start_time |
| 53 | if not fetch_time: |
| 54 | raise Exception('Fetch took 0 time') |
| 55 | |
| 56 | if bytes_received != size: |
| 57 | raise Exception('asked for %d bytes, got %d' % |
| 58 | (size, bytes_received)) |
| 59 | |
| 60 | self.write_perf_keyval( |
| 61 | {'seconds_%s_fetch_time' % label: fetch_time, |
| 62 | 'bytes_%s_bytes_received' % label: bytes_received, |
| 63 | 'bits_second_%s_speed' % label: 8 * bytes_received / fetch_time} |
| 64 | ) |
| 65 | |
| 66 | def run_once_internal(self, connect_count): |
| 67 | logging.info('%s', SpeedTester.CommandOutput(['/sbin/route', '-n'])) |
| 68 | |
| 69 | for _ in xrange(connect_count): |
| 70 | self.FetchUrl(label='3G', size=1 << 19) |
| 71 | |
| 72 | def run_once(self, connect_count): |
| 73 | self.flim = flimflam.FlimFlam() |
| 74 | self.run_once_internal(connect_count) |
| 75 | speeds = self.ExtractPerfField("bits_second_3G_speed") |
| 76 | logging.info('Speeds: %s', speeds) |
| 77 | mean = sum(speeds) / len(speeds) |
| 78 | |
| 79 | print "==============================" |
| 80 | print "Mean speed in kbit/second:", mean / 1000 |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | logger = logging.getLogger() |
| 85 | logger.setLevel(logging.INFO) |
| 86 | logging.info('Beginning') |
| 87 | tester = SpeedTester() |
| 88 | tester.run_once(4) |