OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium 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 |
| 5 from measurements import v8_detached_context_age_in_gc |
| 6 from telemetry.core import wpr_modes |
| 7 from telemetry.page import page_test |
| 8 from telemetry.results import page_test_results |
| 9 from telemetry.unittest_util import options_for_unittests |
| 10 from telemetry.unittest_util import page_test_test_case |
| 11 |
| 12 |
| 13 class FakePage(object): |
| 14 def __init__(self, url): |
| 15 self.url = url |
| 16 self.is_file = url.startswith('file://') |
| 17 |
| 18 |
| 19 class FakeTab(object): |
| 20 def __init__(self, histograms): |
| 21 self.histograms = histograms |
| 22 self.current_histogram_index = 0 |
| 23 |
| 24 def EvaluateJavaScript(self, script): |
| 25 if 'V8.DetachedContextAgeInGC' in script: |
| 26 self.current_histogram_index += 1 |
| 27 return self.histograms[self.current_histogram_index - 1] |
| 28 return "{}" |
| 29 |
| 30 def CollectGarbage(self): |
| 31 pass |
| 32 |
| 33 |
| 34 def _MeasureFakePage(histograms): |
| 35 results = page_test_results.PageTestResults() |
| 36 page = FakePage("file://blank.html") |
| 37 tab = FakeTab(histograms) |
| 38 metric = v8_detached_context_age_in_gc.V8DetachedContextAgeInGC() |
| 39 results.WillRunPage(page) |
| 40 metric.DidNavigateToPage(page, tab) |
| 41 metric.ValidateAndMeasurePage(page, tab, results) |
| 42 results.DidRunPage(page) |
| 43 return results |
| 44 |
| 45 |
| 46 def _ActualValues(results): |
| 47 return dict(list( |
| 48 (v.name, (v.units, v.value)) |
| 49 for v in results.all_page_specific_values |
| 50 )) |
| 51 |
| 52 |
| 53 class V8DetachedContextAgeInGCTests(page_test_test_case.PageTestTestCase): |
| 54 |
| 55 def setUp(self): |
| 56 self._options = options_for_unittests.GetCopy() |
| 57 self._options.browser_options.wpr_mode = wpr_modes.WPR_OFF |
| 58 |
| 59 def testWithNoData(self): |
| 60 histograms = [ |
| 61 """{"count": 0, "buckets": []}""", |
| 62 """{"count": 0, "buckets": []}""" |
| 63 ] |
| 64 results = _MeasureFakePage(histograms) |
| 65 expected = {'V8_DetachedContextAgeInGC': ('garbage_collections', 0) } |
| 66 self._AssertResultsEqual(expected, _ActualValues(results)) |
| 67 |
| 68 def testWithData(self): |
| 69 histograms = [ |
| 70 """{"count": 3, "buckets": [{"low": 1, "high": 2, "count": 1}, |
| 71 {"low": 2, "high": 3, "count": 2}]}""", |
| 72 """{"count": 4, "buckets": [{"low": 1, "high": 2, "count": 2}, |
| 73 {"low": 2, "high": 3, "count": 2}]}""", |
| 74 ] |
| 75 results = _MeasureFakePage(histograms) |
| 76 expected = {'V8_DetachedContextAgeInGC': ('garbage_collections', 2) } |
| 77 self._AssertResultsEqual(expected, _ActualValues(results)) |
| 78 |
| 79 def _AssertResultsEqual(self, expected, actual): |
| 80 for key in expected.iterkeys(): |
| 81 self.assertIn(key, actual.keys()) |
| 82 self.assertEqual(expected[key], actual[key], |
| 83 'Result for [' + key + '] - expected ' + str(expected[key]) + |
| 84 ' but got ' + str(actual[key])) |
OLD | NEW |