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