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
.core
import wpr_modes
6 from telemetry
.page
import page
as page_module
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 from telemetry
.value
import skip
13 from measurements
import v8_detached_context_age_in_gc
16 class FakePage(object):
17 def __init__(self
, url
):
19 self
.is_file
= url
.startswith('file://')
22 class FakeTab(object):
23 def __init__(self
, histograms
):
24 self
.histograms
= histograms
25 self
.current_histogram_index
= 0
27 def EvaluateJavaScript(self
, script
):
28 if 'V8.DetachedContextAgeInGC' in script
:
29 self
.current_histogram_index
+= 1
30 return self
.histograms
[self
.current_histogram_index
- 1]
33 def CollectGarbage(self
):
37 def _MeasureFakePage(histograms
):
38 results
= page_test_results
.PageTestResults()
39 page
= FakePage('file://blank.html')
40 tab
= FakeTab(histograms
)
41 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
42 results
.WillRunPage(page
)
43 metric
.DidNavigateToPage(page
, tab
)
44 metric
.ValidateAndMeasurePage(page
, tab
, results
)
45 results
.DidRunPage(page
)
49 def _ActualValues(results
):
50 return dict((v
.name
, v
) for v
in results
.all_page_specific_values
)
53 class SimplePage(page_module
.Page
):
54 def __init__(self
, page_set
):
55 super(SimplePage
, self
).__init
__(
56 'file://host.html', page_set
, page_set
.base_dir
)
57 def RunPageInteractions(self
, action_runner
):
58 # Reload the page to detach the previous context.
59 action_runner
.ReloadPage()
62 class V8DetachedContextAgeInGCTests(page_test_test_case
.PageTestTestCase
):
65 self
._options
= options_for_unittests
.GetCopy()
66 self
._options
.browser_options
.wpr_mode
= wpr_modes
.WPR_OFF
68 def testWithNoData(self
):
70 """{"count": 0, "buckets": []}""",
71 """{"count": 0, "buckets": []}"""
73 results
= _MeasureFakePage(histograms
)
74 actual
= _ActualValues(results
)
75 self
.assertTrue('skip' in actual
)
76 self
.assertFalse('V8_DetachedContextAgeInGC' in actual
)
78 def testWithData(self
):
80 """{"count": 3, "buckets": [{"low": 1, "high": 2, "count": 1},
81 {"low": 2, "high": 3, "count": 2}]}""",
82 """{"count": 4, "buckets": [{"low": 1, "high": 2, "count": 2},
83 {"low": 2, "high": 3, "count": 2}]}""",
85 results
= _MeasureFakePage(histograms
)
86 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
87 self
.assertEqual(2, actual
.value
)
88 self
.assertEqual('garbage_collections', actual
.units
)
90 def testWithSimplePage(self
):
91 page_set
= self
.CreateEmptyPageSet()
92 page
= SimplePage(page_set
)
93 page_set
.AddUserStory(page
)
94 metric
= v8_detached_context_age_in_gc
.V8DetachedContextAgeInGC()
95 results
= self
.RunMeasurement(metric
, page_set
, options
=self
._options
)
96 self
.assertEquals(0, len(results
.failures
))
97 actual
= _ActualValues(results
)['V8_DetachedContextAgeInGC']
98 self
.assertLessEqual(0, actual
.value
)
99 self
.assertEqual('garbage_collections', actual
.units
)