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 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
.page
import page
as page_module
9 from telemetry
.results
import page_test_results
10 from telemetry
.unittest_util
import options_for_unittests
11 from telemetry
.unittest_util
import page_test_test_case
12 from telemetry
.value
import skip
15 class FakePage(object):
16 def __init__(self
, url
):
18 self
.is_file
= url
.startswith('file://')
21 class FakeTab(object):
22 def __init__(self
, histograms
):
23 self
.histograms
= histograms
24 self
.current_histogram_index
= 0
26 def EvaluateJavaScript(self
, script
):
27 if 'V8.DetachedContextAgeInGC' in script
:
28 self
.current_histogram_index
+= 1
29 return self
.histograms
[self
.current_histogram_index
- 1]
32 def CollectGarbage(self
):
36 def _MeasureFakePage(histograms
):
37 results
= page_test_results
.PageTestResults()
38 page
= FakePage('file://blank.html')
39 tab
= FakeTab(histograms
)
40 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
41 results
.WillRunPage(page
)
42 metric
.DidNavigateToPage(page
, tab
)
43 metric
.ValidateAndMeasurePage(page
, tab
, results
)
44 results
.DidRunPage(page
)
48 def _ActualValues(results
):
49 return dict((v
.name
, v
) for v
in results
.all_page_specific_values
)
52 class SimplePage(page_module
.Page
):
53 def __init__(self
, page_set
):
54 super(SimplePage
, self
).__init
__(
55 'file://host.html', page_set
, page_set
.base_dir
)
56 def RunPageInteractions(self
, action_runner
):
57 # Reload the page to detach the previous context.
58 action_runner
.ReloadPage()
61 class V8DetachedContextAgeInGCTests(page_test_test_case
.PageTestTestCase
):
64 self
._options
= options_for_unittests
.GetCopy()
65 self
._options
.browser_options
.wpr_mode
= wpr_modes
.WPR_OFF
67 def testWithNoData(self
):
69 """{"count": 0, "buckets": []}""",
70 """{"count": 0, "buckets": []}"""
72 results
= _MeasureFakePage(histograms
)
73 actual
= _ActualValues(results
)
74 self
.assertTrue('skip' in actual
)
75 self
.assertFalse('V8_DetachedContextAgeInGC' in actual
)
77 def testWithData(self
):
79 """{"count": 3, "buckets": [{"low": 1, "high": 2, "count": 1},
80 {"low": 2, "high": 3, "count": 2}]}""",
81 """{"count": 4, "buckets": [{"low": 1, "high": 2, "count": 2},
82 {"low": 2, "high": 3, "count": 2}]}""",
84 results
= _MeasureFakePage(histograms
)
85 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
86 self
.assertEqual(2, actual
.value
)
87 self
.assertEqual('garbage_collections', actual
.units
)
89 def testWithSimplePage(self
):
90 page_set
= self
.CreateEmptyPageSet()
91 page
= SimplePage(page_set
)
92 page_set
.AddUserStory(page
)
93 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
94 results
= self
.RunMeasurement(metric
, page_set
, options
=self
._options
)
95 self
.assertEquals(0, len(results
.failures
))
96 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
97 self
.assertLessEqual(0, actual
.value
)
98 self
.assertEqual('garbage_collections', actual
.units
)