autotest: Add logic to log netperf results as keyvals

Write a delegate object that knows how to log netperf results as
keyvals.  This allows us to reuse the same logging format between tests.

BUG=chromium:279395
TEST=A test consuming this logic runs successfully.

Change-Id: I7bf64f3f9fa1771c5faa14315036056d81ae837d
Reviewed-on: https://chromium-review.googlesource.com/66973
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Tested-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/server/cros/network/netperf_wifi_perf_logger.py b/server/cros/network/netperf_wifi_perf_logger.py
new file mode 100644
index 0000000..a4c77a0
--- /dev/null
+++ b/server/cros/network/netperf_wifi_perf_logger.py
@@ -0,0 +1,58 @@
+# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+
+
+class NetperfWiFiPerfLogger(object):
+    """Delegate object to write netperf keyvals in a standard format."""
+
+    @property
+    def channel_label(self):
+        """@return string like 'ch011' corresponding to configured channel."""
+        return 'ch%03d' % self._ap_config.channel
+
+
+    def __init__(self, ap_config, wifi_client, keyval_recorder):
+        """Construct a NetperfWiFiPerfLogger.
+
+        @param ap_config a HostapConfig object representing the state
+                of the AP that netperf is being run against.
+        @param wifi_client a WiFiClient representing the DUT in the test.
+        @param keyval_recorder a function that takes a single argument which
+                is a dict of keyvals.  For instance, this could be the
+                |write_perf_keyvals| function from a test object.
+
+        """
+        self._ap_config = ap_config
+        self._wifi_client = wifi_client
+        self.write_perf_keyval = keyval_recorder
+
+
+    def record_signal_keyval(self):
+        """Records the current WiFi signal level as a keyval."""
+        signal_level_key = '_'.join(
+                [self._wifi_client.machine_id, 'signal', self.channel_label])
+        signal_level = self._wifi_client.wifi_signal_level
+        self.write_perf_keyval({signal_level_key: signal_level})
+        logging.debug('Signal level for channel %d is %d dBm',
+                      self._ap_config.channel, signal_level)
+
+
+    def record_keyvals_for_result(self, result, descriptive_tag=None):
+        """Records result data keyvals.
+
+        @param result NetperfResult object.
+        @param descriptive_tag string concise whitespace free string to be
+                embedded in keyval keys.
+
+        """
+        suffix = '%s_mode%s_%s_%s' % (
+                self.channel_label,
+                self._ap_config.printable_mode,
+                self._ap_config.security_config.security,
+                descriptive_tag or result.tag)
+        keyvals = result.get_keyval(prefix=self._wifi_client.machine_id,
+                                    suffix=suffix)
+        self.write_perf_keyval(keyvals)