• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6
7from autotest_lib.server.cros.network import netperf_runner
8
9class NetperfSession(object):
10    """Abstracts a network performance measurement to reduce variance."""
11
12    MAX_DEVIATION_FRACTION = 0.03
13    MEASUREMENT_MAX_SAMPLES = 10
14    MEASUREMENT_MIN_SAMPLES = 3
15    WARMUP_SAMPLE_TIME_SECONDS = 2
16    WARMUP_WINDOW_SIZE = 2
17    WARMUP_MAX_SAMPLES = 10
18
19
20    @staticmethod
21    def _from_samples(samples):
22        """Construct a NetperfResult that averages previous results.
23
24        This makes typing this considerably easier.
25
26        @param samples: list of NetperfResult
27        @return NetperfResult average of samples.
28
29        """
30        return netperf_runner.NetperfResult.from_samples(samples)
31
32
33    def __init__(self, client_proxy, server_proxy, ignore_failures=False):
34        """Construct a NetperfSession.
35
36        @param client_proxy: WiFiClient object.
37        @param server_proxy: LinuxSystem object.
38
39        """
40        self._client_proxy = client_proxy
41        self._server_proxy = server_proxy
42        self._ignore_failures = ignore_failures
43
44
45    def warmup_wifi_part(self, warmup_client=True):
46        """Warm up a rate controller on the client or server.
47
48        WiFi "warms up" in that rate controllers dynamically adjust to
49        environmental conditions by increasing symbol rates until loss is
50        observed.  This manifests as initially slow data transfer rates that
51        get better over time.
52
53        We'll say that a rate controller is warmed up if a small sample of
54        WARMUP_WINDOW_SIZE throughput measurements has an average throughput
55        within a standard deviation of the previous WARMUP_WINDOW_SIZE samples.
56
57        @param warmup_client: bool True iff we should warmup the client rate
58                controller.  Otherwise we warm up the server rate controller.
59
60        """
61        if warmup_client:
62            # We say a station is warm if the TX throughput is maximized.
63            # Each station only controls its own transmission TX rate.
64            logging.info('Warming up the client WiFi rate controller.')
65            test_type = netperf_runner.NetperfConfig.TEST_TYPE_TCP_STREAM
66        else:
67            logging.info('Warming up the server WiFi rate controller.')
68            test_type = netperf_runner.NetperfConfig.TEST_TYPE_TCP_MAERTS
69        config = netperf_runner.NetperfConfig(
70                test_type, test_time=self.WARMUP_SAMPLE_TIME_SECONDS)
71        warmup_history = []
72        with netperf_runner.NetperfRunner(
73                self._client_proxy, self._server_proxy, config) as runner:
74            while len(warmup_history) < self.WARMUP_MAX_SAMPLES:
75                warmup_history.append(runner.run())
76                if len(warmup_history) > 2 * self.WARMUP_WINDOW_SIZE:
77                    # Grab 2 * WARMUP_WINDOW_SIZE samples, divided into the most
78                    # recent chunk and the chunk before that.
79                    start = -2 * self.WARMUP_WINDOW_SIZE
80                    middle = -self.WARMUP_WINDOW_SIZE
81                    past_result = self._from_samples(
82                            warmup_history[start:middle])
83                    recent_result = self._from_samples(warmup_history[middle:])
84                    if recent_result.throughput < (past_result.throughput +
85                                                   past_result.throughput_dev):
86                        logging.info('Rate controller is warmed.')
87                        return
88            else:
89                logging.warning('Did not completely warmup the WiFi part.')
90
91
92    def warmup_stations(self):
93        """Warms up both the client and server stations."""
94        self.warmup_wifi_part(warmup_client=True)
95        self.warmup_wifi_part(warmup_client=False)
96
97
98    def run(self, config):
99        """Measure the average and standard deviation of a netperf test.
100
101        @param config: NetperfConfig object.
102
103        """
104        logging.info('Performing %s measurements in netperf session.',
105                     config.human_readable_tag)
106        history = []
107        none_count = 0
108        final_result = None
109        with netperf_runner.NetperfRunner(
110                self._client_proxy, self._server_proxy, config) as runner:
111            while len(history) + none_count < self.MEASUREMENT_MAX_SAMPLES:
112                result = runner.run(ignore_failures=self._ignore_failures)
113                if result is None:
114                    none_count += 1
115                    continue
116
117                history.append(result)
118                if len(history) < self.MEASUREMENT_MIN_SAMPLES:
119                    continue
120
121                final_result = self._from_samples(history)
122                if final_result.all_deviations_less_than_fraction(
123                        self.MAX_DEVIATION_FRACTION):
124                    break
125
126        if final_result is None:
127            final_result = self._from_samples(history)
128        logging.info('Took averaged measurement %r.', final_result)
129        return history or None
130