Bug 1457127 - Add basic gtest coverage for histogram persistence I/O. r?chutten,janerik draft
authorAlessio Placitelli <alessio.placitelli@gmail.com>
Wed, 16 May 2018 13:09:34 +0200
changeset 797042 430dd90bbfa1c424645100b1909f8d908a83c209
parent 797041 3f9aff25fc5a3fc6821982973a03bab2152b569e
push id110415
push userbmo:alessio.placitelli@gmail.com
push dateFri, 18 May 2018 17:17:25 +0000
reviewerschutten, janerik
bugs1457127
milestone62.0a1
Bug 1457127 - Add basic gtest coverage for histogram persistence I/O. r?chutten,janerik MozReview-Commit-ID: 75MQ1oDXLj0
toolkit/components/telemetry/geckoview/gtest/TestGeckoView.cpp
--- a/toolkit/components/telemetry/geckoview/gtest/TestGeckoView.cpp
+++ b/toolkit/components/telemetry/geckoview/gtest/TestGeckoView.cpp
@@ -27,16 +27,38 @@ const char kSampleData[] = R"({
     }
   },
   "keyedScalars": {
     "parent": {
       "telemetry.test.keyed_unsigned_int": {
         "testKey": 73
       }
     }
+  },
+  "histograms": {
+    "parent": {
+      "TELEMETRY_TEST_MULTIPRODUCT": {
+        "sum": 6,
+        "counts": [
+          3, 5, 7
+        ]
+      }
+    }
+  },
+  "keyedHistograms": {
+    "content": {
+      "TELEMETRY_TEST_MULTIPRODUCT_KEYED": {
+        "niceKey": {
+          "sum": 10,
+          "counts": [
+            1, 2, 3
+          ]
+        }
+      }
+    }
   }
 })";
 
 const char16_t kPersistedFilename[] = u"gv_measurements.json";
 
 namespace {
 
 /**
@@ -220,16 +242,90 @@ TestDeserializePersistedKeyedScalars(JSC
   JS::RootedObject sampleObj(aCx, &sampleData.toObject());
   JS::RootedValue keyedScalarData(aCx);
   ASSERT_TRUE(JS_GetProperty(aCx, sampleObj, "keyedScalars", &keyedScalarData)
               && keyedScalarData.isObject()) << "Failed to get sampleData['keyedScalars']";
 
   CheckJSONEqual(aCx, aData, keyedScalarData);
 }
 
+void
+TestSerializeHistograms(JSONWriter& aWriter)
+{
+  // Report the same data that's in kSampleData for histograms.
+  // We only want to make sure that I/O and parsing works, as telemetry
+  // measurement updates is taken care of by xpcshell tests.
+  aWriter.StartObjectProperty("parent");
+  aWriter.StartObjectProperty("TELEMETRY_TEST_MULTIPRODUCT");
+  aWriter.IntProperty("sum", 6);
+  aWriter.StartArrayProperty("counts");
+  aWriter.IntElement(3);
+  aWriter.IntElement(5);
+  aWriter.IntElement(7);
+  aWriter.EndArray();
+  aWriter.EndObject();
+  aWriter.EndObject();
+}
+
+void
+TestSerializeKeyedHistograms(JSONWriter& aWriter)
+{
+  // Report the same data that's in kSampleData for keyed histograms.
+  // We only want to make sure that I/O and parsing works, as telemetry
+  // measurement updates is taken care of by xpcshell tests.
+  aWriter.StartObjectProperty("content");
+  aWriter.StartObjectProperty("TELEMETRY_TEST_MULTIPRODUCT_KEYED");
+  aWriter.StartObjectProperty("niceKey");
+  aWriter.IntProperty("sum", 10);
+  aWriter.StartArrayProperty("counts");
+  aWriter.IntElement(1);
+  aWriter.IntElement(2);
+  aWriter.IntElement(3);
+  aWriter.EndArray();
+  aWriter.EndObject();
+  aWriter.EndObject();
+  aWriter.EndObject();
+}
+
+void
+TestDeserializeHistograms(JSContext* aCx, JS::HandleValue aData)
+{
+  // Get a JS object out of the JSON sample.
+  JS::RootedValue sampleData(aCx);
+  NS_ConvertUTF8toUTF16 utf16Content(kSampleData);
+  ASSERT_TRUE(JS_ParseJSON(aCx, utf16Content.BeginReading(), utf16Content.Length(), &sampleData))
+    << "Failed to create a JS object from the JSON sample";
+
+  // Get sampleData["histograms"].
+  JS::RootedObject sampleObj(aCx, &sampleData.toObject());
+  JS::RootedValue histogramData(aCx);
+  ASSERT_TRUE(JS_GetProperty(aCx, sampleObj, "histograms", &histogramData) && histogramData.isObject())
+    << "Failed to get sampleData['histograms']";
+
+  CheckJSONEqual(aCx, aData, histogramData);
+}
+
+void
+TestDeserializeKeyedHistograms(JSContext* aCx, JS::HandleValue aData)
+{
+  // Get a JS object out of the JSON sample.
+  JS::RootedValue sampleData(aCx);
+  NS_ConvertUTF8toUTF16 utf16Content(kSampleData);
+  ASSERT_TRUE(JS_ParseJSON(aCx, utf16Content.BeginReading(), utf16Content.Length(), &sampleData))
+    << "Failed to create a JS object from the JSON sample";
+
+  // Get sampleData["keyedHistograms"].
+  JS::RootedObject sampleObj(aCx, &sampleData.toObject());
+  JS::RootedValue keyedHistogramData(aCx);
+  ASSERT_TRUE(JS_GetProperty(aCx, sampleObj, "keyedHistograms", &keyedHistogramData)
+              && keyedHistogramData.isObject()) << "Failed to get sampleData['keyedHistograms']";
+
+  CheckJSONEqual(aCx, aData, keyedHistogramData);
+}
+
 } // Anonymous
 
 /**
  * A GeckoView specific test fixture. Please note that this
  * can't live in the above anonymous namespace.
  */
 class TelemetryGeckoViewFixture : public TelemetryTestFixture {
 protected:
@@ -247,16 +343,25 @@ namespace TelemetryScalar {
 
 nsresult SerializeScalars(JSONWriter& aWriter) { TestSerializeScalars(aWriter); return NS_OK; }
 nsresult SerializeKeyedScalars(JSONWriter& aWriter) { TestSerializeKeyedScalars(aWriter); return NS_OK; }
 nsresult DeserializePersistedScalars(JSContext* aCx, JS::HandleValue aData) { TestDeserializePersistedScalars(aCx, aData); return NS_OK; }
 nsresult DeserializePersistedKeyedScalars(JSContext* aCx, JS::HandleValue aData) { TestDeserializePersistedKeyedScalars(aCx, aData); return NS_OK; }
 
 } // TelemetryScalar
 
+namespace TelemetryHistogram {
+
+nsresult SerializeHistograms(mozilla::JSONWriter &aWriter) { TestSerializeHistograms(aWriter); return NS_OK; }
+nsresult SerializeKeyedHistograms(mozilla::JSONWriter &aWriter) { TestSerializeKeyedHistograms(aWriter); return NS_OK; }
+nsresult DeserializeHistograms(JSContext* aCx, JS::HandleValue aData) { TestDeserializeHistograms(aCx, aData); return NS_OK; }
+nsresult DeserializeKeyedHistograms(JSContext* aCx, JS::HandleValue aData) { TestDeserializeKeyedHistograms(aCx, aData); return NS_OK; }
+
+} // TelemetryHistogram
+
 namespace TelemetryGeckoViewTesting {
 
 void TestDispatchPersist();
 
 } // TelemetryGeckoViewTesting
 
 /**
  * Test that corrupted JSON files don't crash the Telemetry core.