aboutsummaryrefslogtreecommitdiff
path: root/src/histogram.cc
blob: 8d1eb77b1bc88e7347bc893ce6707a0ddb8256f0 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "histogram.h"  // NOLINT(build/include_inline)
#include "histogram-inl.h"
#include "memory_tracker-inl.h"

namespace node {

using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Local;
using v8::Map;
using v8::Number;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;

Histogram::Histogram(int64_t lowest, int64_t highest, int figures) {
  hdr_histogram* histogram;
  CHECK_EQ(0, hdr_init(lowest, highest, figures, &histogram));
  histogram_.reset(histogram);
}

HistogramBase::HistogramBase(
    Environment* env,
    v8::Local<v8::Object> wrap,
    int64_t lowest,
    int64_t highest,
    int figures)
    : BaseObject(env, wrap),
      Histogram(lowest, highest, figures) {
  MakeWeak();
}

void HistogramBase::MemoryInfo(MemoryTracker* tracker) const {
  tracker->TrackFieldWithSize("histogram", GetMemorySize());
}

void HistogramBase::GetMin(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  double value = static_cast<double>(histogram->Min());
  args.GetReturnValue().Set(value);
}

void HistogramBase::GetMax(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  double value = static_cast<double>(histogram->Max());
  args.GetReturnValue().Set(value);
}

void HistogramBase::GetMean(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  args.GetReturnValue().Set(histogram->Mean());
}

void HistogramBase::GetExceeds(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  double value = static_cast<double>(histogram->Exceeds());
  args.GetReturnValue().Set(value);
}

void HistogramBase::GetStddev(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  args.GetReturnValue().Set(histogram->Stddev());
}

void HistogramBase::GetPercentile(
    const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  CHECK(args[0]->IsNumber());
  double percentile = args[0].As<Number>()->Value();
  args.GetReturnValue().Set(histogram->Percentile(percentile));
}

void HistogramBase::GetPercentiles(
    const FunctionCallbackInfo<Value>& args) {
  Environment* env = Environment::GetCurrent(args);
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  CHECK(args[0]->IsMap());
  Local<Map> map = args[0].As<Map>();
  histogram->Percentiles([map, env](double key, double value) {
    map->Set(
        env->context(),
        Number::New(env->isolate(), key),
        Number::New(env->isolate(), value)).IsEmpty();
  });
}

void HistogramBase::DoReset(const FunctionCallbackInfo<Value>& args) {
  HistogramBase* histogram;
  ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder());
  histogram->ResetState();
}

BaseObjectPtr<HistogramBase> HistogramBase::New(
    Environment* env,
    int64_t lowest,
    int64_t highest,
    int figures) {
  CHECK_LE(lowest, highest);
  CHECK_GT(figures, 0);
  v8::Local<v8::Object> obj;
  auto tmpl = env->histogram_instance_template();
  if (!tmpl->NewInstance(env->context()).ToLocal(&obj))
    return {};

  return MakeDetachedBaseObject<HistogramBase>(
      env, obj, lowest, highest, figures);
}

void HistogramBase::Initialize(Environment* env) {
  // Guard against multiple initializations
  if (!env->histogram_instance_template().IsEmpty())
    return;

  Local<FunctionTemplate> histogram = FunctionTemplate::New(env->isolate());
  Local<String> classname = FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram");
  histogram->SetClassName(classname);

  Local<ObjectTemplate> histogramt =
    histogram->InstanceTemplate();

  histogramt->SetInternalFieldCount(1);
  env->SetProtoMethod(histogram, "exceeds", HistogramBase::GetExceeds);
  env->SetProtoMethod(histogram, "min", HistogramBase::GetMin);
  env->SetProtoMethod(histogram, "max", HistogramBase::GetMax);
  env->SetProtoMethod(histogram, "mean", HistogramBase::GetMean);
  env->SetProtoMethod(histogram, "stddev", HistogramBase::GetStddev);
  env->SetProtoMethod(histogram, "percentile", HistogramBase::GetPercentile);
  env->SetProtoMethod(histogram, "percentiles", HistogramBase::GetPercentiles);
  env->SetProtoMethod(histogram, "reset", HistogramBase::DoReset);

  env->set_histogram_instance_template(histogramt);
}

}  // namespace node