#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 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& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); double value = static_cast(histogram->Min()); args.GetReturnValue().Set(value); } void HistogramBase::GetMax(const FunctionCallbackInfo& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); double value = static_cast(histogram->Max()); args.GetReturnValue().Set(value); } void HistogramBase::GetMean(const FunctionCallbackInfo& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); args.GetReturnValue().Set(histogram->Mean()); } void HistogramBase::GetExceeds(const FunctionCallbackInfo& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); double value = static_cast(histogram->Exceeds()); args.GetReturnValue().Set(value); } void HistogramBase::GetStddev(const FunctionCallbackInfo& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); args.GetReturnValue().Set(histogram->Stddev()); } void HistogramBase::GetPercentile( const FunctionCallbackInfo& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); CHECK(args[0]->IsNumber()); double percentile = args[0].As()->Value(); args.GetReturnValue().Set(histogram->Percentile(percentile)); } void HistogramBase::GetPercentiles( const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); CHECK(args[0]->IsMap()); Local map = args[0].As(); 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& args) { HistogramBase* histogram; ASSIGN_OR_RETURN_UNWRAP(&histogram, args.Holder()); histogram->ResetState(); } BaseObjectPtr HistogramBase::New( Environment* env, int64_t lowest, int64_t highest, int figures) { CHECK_LE(lowest, highest); CHECK_GT(figures, 0); v8::Local obj; auto tmpl = env->histogram_instance_template(); if (!tmpl->NewInstance(env->context()).ToLocal(&obj)) return {}; return MakeDetachedBaseObject( env, obj, lowest, highest, figures); } void HistogramBase::Initialize(Environment* env) { // Guard against multiple initializations if (!env->histogram_instance_template().IsEmpty()) return; Local histogram = FunctionTemplate::New(env->isolate()); Local classname = FIXED_ONE_BYTE_STRING(env->isolate(), "Histogram"); histogram->SetClassName(classname); Local 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