1 /* vim:set ts=2 sw=2 sts=0 et: */
2 /* Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
5
6 #include "gtest/gtest.h"
7 #include "js/Conversions.h"
8 #include "mozilla/Telemetry.h"
9 #include "TelemetryFixture.h"
10 #include "TelemetryTestHelpers.h"
11
12 using namespace mozilla;
13 using namespace TelemetryTestHelpers;
14
TEST_F(TelemetryTestFixture,AutoCounter)15 TEST_F(TelemetryTestFixture, AutoCounter) {
16 const uint32_t kExpectedValue = 100;
17 AutoJSContextWithGlobal cx(mCleanGlobal);
18
19 const char* telemetryTestCountName =
20 Telemetry::GetHistogramName(Telemetry::TELEMETRY_TEST_COUNT);
21
22 GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
23 false);
24
25 // Accumulate in the histogram
26 {
27 Telemetry::AutoCounter<Telemetry::TELEMETRY_TEST_COUNT> autoCounter;
28 autoCounter += kExpectedValue / 2;
29 }
30 // This counter should not accumulate since it does not go out of scope
31 Telemetry::AutoCounter<Telemetry::TELEMETRY_TEST_COUNT> autoCounter;
32 autoCounter += kExpectedValue;
33 // Accumulate a second time in the histogram
34 {
35 Telemetry::AutoCounter<Telemetry::TELEMETRY_TEST_COUNT> autoCounter;
36 autoCounter += kExpectedValue / 2;
37 }
38
39 // Get a snapshot for all the histograms
40 JS::RootedValue snapshot(cx.GetJSContext());
41 GetSnapshots(cx.GetJSContext(), mTelemetry, telemetryTestCountName, &snapshot,
42 false);
43
44 // Get the histogram from the snapshot
45 JS::RootedValue histogram(cx.GetJSContext());
46 GetProperty(cx.GetJSContext(), telemetryTestCountName, snapshot, &histogram);
47
48 // Get "sum" property from histogram
49 JS::RootedValue sum(cx.GetJSContext());
50 GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
51
52 // Check that the "sum" stored in the histogram matches with |kExpectedValue|
53 uint32_t uSum = 0;
54 JS::ToUint32(cx.GetJSContext(), sum, &uSum);
55 ASSERT_EQ(uSum, kExpectedValue)
56 << "The histogram is not returning expected value";
57 }
58
TEST_F(TelemetryTestFixture,AutoCounterUnderflow)59 TEST_F(TelemetryTestFixture, AutoCounterUnderflow) {
60 const uint32_t kExpectedValue = 0;
61 AutoJSContextWithGlobal cx(mCleanGlobal);
62
63 const char* telemetryTestCountName =
64 Telemetry::GetHistogramName(Telemetry::TELEMETRY_TEST_COUNT);
65
66 GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
67 false);
68
69 // Accumulate in the histogram
70 {
71 Telemetry::AutoCounter<Telemetry::TELEMETRY_TEST_COUNT> autoCounter;
72 autoCounter += -1;
73 }
74
75 // Get a snapshot for all the histograms
76 JS::RootedValue snapshot(cx.GetJSContext());
77 GetSnapshots(cx.GetJSContext(), mTelemetry, telemetryTestCountName, &snapshot,
78 false);
79
80 // Get the histogram from the snapshot
81 JS::RootedValue histogram(cx.GetJSContext());
82 GetProperty(cx.GetJSContext(), telemetryTestCountName, snapshot, &histogram);
83
84 // Get "sum" property from histogram
85 JS::RootedValue sum(cx.GetJSContext());
86 GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
87
88 // Check that the "sum" stored in the histogram matches with |kExpectedValue|
89 uint32_t uSum = 42;
90 JS::ToUint32(cx.GetJSContext(), sum, &uSum);
91 ASSERT_EQ(uSum, kExpectedValue)
92 << "The histogram is supposed to return 0 when an underflow occurs.";
93 }
94
TEST_F(TelemetryTestFixture,RuntimeAutoCounter)95 TEST_F(TelemetryTestFixture, RuntimeAutoCounter) {
96 const uint32_t kExpectedValue = 100;
97 AutoJSContextWithGlobal cx(mCleanGlobal);
98
99 const char* telemetryTestCountName =
100 Telemetry::GetHistogramName(Telemetry::TELEMETRY_TEST_COUNT);
101
102 GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
103 false);
104
105 // Accumulate in the histogram
106 {
107 Telemetry::RuntimeAutoCounter autoCounter(Telemetry::TELEMETRY_TEST_COUNT);
108 autoCounter += kExpectedValue / 2;
109 }
110 // This counter should not accumulate since it does not go out of scope
111 Telemetry::RuntimeAutoCounter autoCounter(Telemetry::TELEMETRY_TEST_COUNT);
112 autoCounter += kExpectedValue;
113 // Accumulate a second time in the histogram
114 {
115 Telemetry::RuntimeAutoCounter autoCounter(Telemetry::TELEMETRY_TEST_COUNT);
116 autoCounter += kExpectedValue / 2;
117 }
118 // Get a snapshot for all the histograms
119 JS::RootedValue snapshot(cx.GetJSContext());
120 GetSnapshots(cx.GetJSContext(), mTelemetry, telemetryTestCountName, &snapshot,
121 false);
122
123 // Get the histogram from the snapshot
124 JS::RootedValue histogram(cx.GetJSContext());
125 GetProperty(cx.GetJSContext(), telemetryTestCountName, snapshot, &histogram);
126
127 // Get "sum" property from histogram
128 JS::RootedValue sum(cx.GetJSContext());
129 GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
130
131 // Check that the "sum" stored in the histogram matches with |kExpectedValue|
132 uint32_t uSum = 0;
133 JS::ToUint32(cx.GetJSContext(), sum, &uSum);
134 ASSERT_EQ(uSum, kExpectedValue)
135 << "The histogram is not returning expected value";
136 }
137
TEST_F(TelemetryTestFixture,RuntimeAutoCounterUnderflow)138 TEST_F(TelemetryTestFixture, RuntimeAutoCounterUnderflow) {
139 const uint32_t kExpectedValue = 0;
140 AutoJSContextWithGlobal cx(mCleanGlobal);
141
142 const char* telemetryTestCountName =
143 Telemetry::GetHistogramName(Telemetry::TELEMETRY_TEST_COUNT);
144
145 GetAndClearHistogram(cx.GetJSContext(), mTelemetry, "TELEMETRY_TEST_COUNT"_ns,
146 false);
147
148 // Accumulate in the histogram
149 {
150 Telemetry::RuntimeAutoCounter autoCounter(Telemetry::TELEMETRY_TEST_COUNT,
151 kExpectedValue);
152 autoCounter += -1;
153 }
154
155 // Get a snapshot for all the histograms
156 JS::RootedValue snapshot(cx.GetJSContext());
157 GetSnapshots(cx.GetJSContext(), mTelemetry, telemetryTestCountName, &snapshot,
158 false);
159
160 // Get the histogram from the snapshot
161 JS::RootedValue histogram(cx.GetJSContext());
162 GetProperty(cx.GetJSContext(), telemetryTestCountName, snapshot, &histogram);
163
164 // Get "sum" property from histogram
165 JS::RootedValue sum(cx.GetJSContext());
166 GetProperty(cx.GetJSContext(), "sum", histogram, &sum);
167
168 // Check that the "sum" stored in the histogram matches with |kExpectedValue|
169 uint32_t uSum = 42;
170 JS::ToUint32(cx.GetJSContext(), sum, &uSum);
171 ASSERT_EQ(uSum, kExpectedValue)
172 << "The histogram is supposed to return 0 when an underflow occurs.";
173 }
174