summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/value/histogram.h
blob: ff99d3311fe4226da250105e5d8d74c6ccda81da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "tracing/tracing/proto/histogram.pb.h"

namespace catapult {

class RunningStatistics;

// This class continuously saves results from a performance tests.
// You can later convert the results to a proto and upload to a Catapult
// dashboard.
//
// This class provides a subset of the API in histogram.py and is meant to work
// exactly the same way.
class HistogramBuilder {
 public:
  HistogramBuilder(const std::string& name,
                   tracing::tracing::proto::UnitAndDirection unit);
  ~HistogramBuilder();

  void set_description(const std::string& description) {
    description_ = description;
  }

  void AddDiagnostic(const std::string& key,
                     tracing::tracing::proto::Diagnostic diagnostic);

  void AddSample(double value);

  void SetSummaryOptions(tracing::tracing::proto::SummaryOptions options);

  std::unique_ptr<tracing::tracing::proto::Histogram> toProto() const;

 private:
  class Resampler;

  std::unique_ptr<Resampler> resampler_;
  std::unique_ptr<RunningStatistics> running_statistics_;
  int max_num_sample_values_;
  std::string name_;
  std::string description_;
  tracing::tracing::proto::SummaryOptions options_;
  tracing::tracing::proto::UnitAndDirection unit_;
  std::vector<double> sample_values_;
  std::unordered_map<std::string, tracing::tracing::proto::Diagnostic>
      diagnostics_;
  int num_nans_;
};

// Returns the corresponding proto unit if a C++ unit test still uses the old
// unit strings (see docs/histogram-set-json-format.md for the spec). Note,
// any _biggerIsBetter or _smallerIsBetter suffixes will be ignored.
tracing::tracing::proto::Unit UnitFromJsonUnit(std::string unit);

}  // namespace catapult