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.
5 from telemetry
.internal
.results
import page_test_results
6 from telemetry
.page
import page
as page_module
7 from telemetry
.testing
import options_for_unittests
8 from telemetry
.testing
import page_test_test_case
9 from telemetry
.util
import wpr_modes
11 from measurements
import v8_detached_context_age_in_gc
14 class FakePage(object):
15 def __init__(self
, url
):
17 self
.is_file
= url
.startswith('file://')
20 class FakeTab(object):
21 def __init__(self
, histograms
):
22 self
.histograms
= histograms
23 self
.current_histogram_index
= 0
25 def EvaluateJavaScript(self
, script
):
26 if 'V8.DetachedContextAgeInGC' in script
:
27 self
.current_histogram_index
+= 1
28 return self
.histograms
[self
.current_histogram_index
- 1]
31 def CollectGarbage(self
):
35 def _MeasureFakePage(histograms
):
36 results
= page_test_results
.PageTestResults()
37 page
= FakePage('file://blank.html')
38 tab
= FakeTab(histograms
)
39 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
40 results
.WillRunPage(page
)
41 metric
.DidNavigateToPage(page
, tab
)
42 metric
.ValidateAndMeasurePage(page
, tab
, results
)
43 results
.DidRunPage(page
)
47 def _ActualValues(results
):
48 return dict((v
.name
, v
) for v
in results
.all_page_specific_values
)
51 class SimplePage(page_module
.Page
):
52 def __init__(self
, page_set
):
53 super(SimplePage
, self
).__init
__(
54 'file://host.html', page_set
, page_set
.base_dir
)
55 def RunPageInteractions(self
, action_runner
):
56 # Reload the page to detach the previous context.
57 action_runner
.ReloadPage()
60 class V8DetachedContextAgeInGCTests(page_test_test_case
.PageTestTestCase
):
63 self
._options
= options_for_unittests
.GetCopy()
64 self
._options
.browser_options
.wpr_mode
= wpr_modes
.WPR_OFF
66 def testWithNoData(self
):
68 """{"count": 0, "buckets": []}""",
69 """{"count": 0, "buckets": []}"""
71 results
= _MeasureFakePage(histograms
)
72 actual
= _ActualValues(results
)
73 self
.assertTrue('skip' in actual
)
74 self
.assertFalse('V8_DetachedContextAgeInGC' in actual
)
76 def testWithData(self
):
78 """{"count": 3, "buckets": [{"low": 1, "high": 2, "count": 1},
79 {"low": 2, "high": 3, "count": 2}]}""",
80 """{"count": 4, "buckets": [{"low": 1, "high": 2, "count": 2},
81 {"low": 2, "high": 3, "count": 2}]}""",
83 results
= _MeasureFakePage(histograms
)
84 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
85 self
.assertEqual(2, actual
.value
)
86 self
.assertEqual('garbage_collections', actual
.units
)
88 def testWithSimplePage(self
):
89 page_set
= self
.CreateEmptyPageSet()
90 page
= SimplePage(page_set
)
91 page_set
.AddStory(page
)
92 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
93 results
= self
.RunMeasurement(metric
, page_set
, options
=self
._options
)
94 self
.assertEquals(0, len(results
.failures
))
95 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
96 self
.assertLessEqual(0, actual
.value
)
97 self
.assertEqual('garbage_collections', actual
.units
)