histogram_delta_serialization_unittest.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2013 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.
  4. #include "base/metrics/histogram_delta_serialization.h"
  5. #include <vector>
  6. #include "base/metrics/histogram.h"
  7. #include "base/metrics/histogram_base.h"
  8. #include "base/metrics/statistics_recorder.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace base {
  11. TEST(HistogramDeltaSerializationTest, DeserializeHistogramAndAddSamples) {
  12. std::unique_ptr<StatisticsRecorder> statistic_recorder(
  13. StatisticsRecorder::CreateTemporaryForTesting());
  14. HistogramDeltaSerialization serializer("HistogramDeltaSerializationTest");
  15. std::vector<std::string> deltas;
  16. // Nothing was changed yet.
  17. serializer.PrepareAndSerializeDeltas(&deltas, true);
  18. EXPECT_TRUE(deltas.empty());
  19. HistogramBase* histogram = Histogram::FactoryGet(
  20. "TestHistogram", 1, 1000, 10, HistogramBase::kIPCSerializationSourceFlag);
  21. histogram->Add(1);
  22. histogram->Add(10);
  23. histogram->Add(100);
  24. histogram->Add(1000);
  25. serializer.PrepareAndSerializeDeltas(&deltas, true);
  26. EXPECT_FALSE(deltas.empty());
  27. HistogramDeltaSerialization::DeserializeAndAddSamples(deltas);
  28. // The histogram has kIPCSerializationSourceFlag. So samples will be ignored.
  29. std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
  30. EXPECT_EQ(1, snapshot->GetCount(1));
  31. EXPECT_EQ(1, snapshot->GetCount(10));
  32. EXPECT_EQ(1, snapshot->GetCount(100));
  33. EXPECT_EQ(1, snapshot->GetCount(1000));
  34. // Clear kIPCSerializationSourceFlag to emulate multi-process usage.
  35. histogram->ClearFlags(HistogramBase::kIPCSerializationSourceFlag);
  36. HistogramDeltaSerialization::DeserializeAndAddSamples(deltas);
  37. std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
  38. EXPECT_EQ(2, snapshot2->GetCount(1));
  39. EXPECT_EQ(2, snapshot2->GetCount(10));
  40. EXPECT_EQ(2, snapshot2->GetCount(100));
  41. EXPECT_EQ(2, snapshot2->GetCount(1000));
  42. }
  43. } // namespace base