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