From f021a84c6724eb2e230c0e2fdbb093a4177f8eef Mon Sep 17 00:00:00 2001 From: eakuefner Date: Wed, 8 Jul 2015 14:52:49 -0700 Subject: [PATCH] [Telemetry] Refactor Find*Values* methods in PageTestResults This CL introduces FindValues, a generic helper method for searching PageTestResults, reimplements the existing helpers in terms of it, and adds a new helper to search for values matching interaction record labels which I plan to use in a forthcoming CL. BUG=461101 R=aiolos,nednguyen Review URL: https://codereview.chromium.org/1219143003 Cr-Commit-Position: refs/heads/master@{#337918} --- .../internal/results/page_test_results.py | 30 +++++++++++++--------- .../internal/results/page_test_results_unittest.py | 24 +++++++++++++++++ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/tools/telemetry/telemetry/internal/results/page_test_results.py b/tools/telemetry/telemetry/internal/results/page_test_results.py index 104f73c7a019..9e382bbb2d31 100644 --- a/tools/telemetry/telemetry/internal/results/page_test_results.py +++ b/tools/telemetry/telemetry/internal/results/page_test_results.py @@ -208,26 +208,32 @@ class PageTestResults(object): for output_formatter in self._output_formatters: output_formatter.Format(self) - def FindPageSpecificValuesForPage(self, page, value_name): + def FindValues(self, predicate): + """Finds all values matching the specified predicate. + + Args: + predicate: A function that takes a Value and returns a bool. + Returns: + A list of values matching |predicate|. + """ values = [] for value in self.all_page_specific_values: - if value.page == page and value.name == value_name: + if predicate(value): values.append(value) return values + def FindPageSpecificValuesForPage(self, page, value_name): + return self.FindValues(lambda v: v.page == page and v.name == value_name) + def FindAllPageSpecificValuesNamed(self, value_name): - values = [] - for value in self.all_page_specific_values: - if value.name == value_name: - values.append(value) - return values + return self.FindValues(lambda v: v.name == value_name) + + def FindAllPageSpecificValuesFromIRNamed(self, tir_label, value_name): + return self.FindValues(lambda v: v.name == value_name + and v.tir_label == tir_label) def FindAllTraceValues(self): - values = [] - for value in self.all_page_specific_values: - if isinstance(value, trace.TraceValue): - values.append(value) - return values + return self.FindValues(lambda v: isinstance(v, trace.TraceValue)) def _SerializeTracesToDirPath(self, dir_path): """ Serialize all trace values to files in dir_path and return a list of diff --git a/tools/telemetry/telemetry/internal/results/page_test_results_unittest.py b/tools/telemetry/telemetry/internal/results/page_test_results_unittest.py index bb1d5fcca907..62c0f1839e48 100644 --- a/tools/telemetry/telemetry/internal/results/page_test_results_unittest.py +++ b/tools/telemetry/telemetry/internal/results/page_test_results_unittest.py @@ -187,6 +187,30 @@ class PageTestResultsTest(base_test_results_unittest.BaseTestResultsUnittest): self.assertEquals( [value1, value2, value3], results.all_page_specific_values) + def testFindValues(self): + results = page_test_results.PageTestResults() + results.WillRunPage(self.pages[0]) + v0 = scalar.ScalarValue(self.pages[0], 'a', 'seconds', 3) + results.AddValue(v0) + v1 = scalar.ScalarValue(self.pages[0], 'a', 'seconds', 4) + results.AddValue(v1) + results.DidRunPage(self.pages[1]) + + values = results.FindValues(lambda v: v.value == 3) + self.assertEquals([v0], values) + + def testValueWithTIRLabel(self): + results = page_test_results.PageTestResults() + results.WillRunPage(self.pages[0]) + v0 = scalar.ScalarValue(self.pages[0], 'a', 'seconds', 3, tir_label='foo') + results.AddValue(v0) + v1 = scalar.ScalarValue(self.pages[0], 'a', 'seconds', 3, tir_label='bar') + results.AddValue(v1) + results.DidRunPage(self.pages[0]) + + values = results.FindAllPageSpecificValuesFromIRNamed('foo', 'a') + self.assertEquals([v0], values) + def testTraceValue(self): results = page_test_results.PageTestResults() results.WillRunPage(self.pages[0]) -- 2.11.4.GIT