C++ prometheus性能分析

18 篇文章 0 订阅
测试

#include <time.h>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>

#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
// #include "opencensus/exporters/stats/prometheus/prometheus_exporter.h"
#include "opencensus/exporters/stats/stdout/stdout_exporter.h"
#include "opencensus/exporters/trace/stdout/stdout_exporter.h"
#include "opencensus/stats/stats.h"
#include "opencensus/trace/sampler.h"
#include "opencensus/trace/span.h"

#include <iostream>

#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
// #include "opencensus/exporters/stats/prometheus/prometheus_exporter.h"
#include "opencensus/stats/stats.h"
#include "opencensus/tags/tag_key.h"
//#include "prometheus/exposer.h"

namespace {

ABSL_CONST_INIT const absl::string_view kVideoSizeViewName =
    "my.org/views/video_size";
ABSL_CONST_INIT const absl::string_view kVideoSizeMeasureName =
    "my.org/measure/video_size";

// The resource owner defines and registers a measure. A function exposing a
// function-local static is the recommended style, ensuring that the measure is
// only registered once.
opencensus::stats::MeasureInt64 VideoSizeMeasure() {
  static const opencensus::stats::MeasureInt64 video_size =
      opencensus::stats::MeasureInt64::Register(
          kVideoSizeMeasureName, "size of processed videos", "By");
  return video_size;
}

opencensus::tags::TagKey FrontendKey() {
  static const auto frontend_key =
      opencensus::tags::TagKey::Register("my.org/keys/frontend");
  return frontend_key;
}

}  // namespace
constexpr int64_t kMiB = 1 << 20;

ABSL_CONST_INIT const absl::string_view kLatencyMeasureName     = "repl/latency";
ABSL_CONST_INIT const absl::string_view kLineLengthsMeasureName = "repl/line_lengths";

// Treat Measures and TagKeys as singletons and initialize on
// demand in order to avoid initialization order issues.

opencensus::stats::MeasureDouble LatencyMsMeasure() {
  static const auto measure = opencensus::stats::MeasureDouble::Register(
      kLatencyMeasureName, "The latency in milliseconds", "ms");
  return measure;
}

opencensus::stats::MeasureInt64 LineLengthsMeasure() {
  static const auto measure = opencensus::stats::MeasureInt64::Register(
      kLineLengthsMeasureName, "The distributions of line lengths", "By");
  return measure;
}

opencensus::tags::TagKey MethodKey() {
  static const auto key = opencensus::tags::TagKey::Register("method");
  return key;
}

opencensus::stats::ViewDescriptor RegisterViews() {
  // 1. Latency view
  // We need to register the measure before registering the view.
  LatencyMsMeasure();
  opencensus::stats::ViewDescriptor()
      .set_name("ocquickstart.io/latency")
      .set_description("The various methods' latencies in milliseconds")
      .set_measure(kLatencyMeasureName)
      .set_aggregation(opencensus::stats::Aggregation::Distribution(
          opencensus::stats::BucketBoundaries::Explicit(
              {0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000,
               6000})))
      .add_column(MethodKey())
      .RegisterForExport();

  // 2. Line lengths
  LineLengthsMeasure();
  opencensus::stats::ViewDescriptor()
      .set_name("ocquickstart.io/line_lengths")
      .set_description("The length of the lines read in")
      .set_measure(kLineLengthsMeasureName)
      .set_aggregation(opencensus::stats::Aggregation::Distribution(
          opencensus::stats::BucketBoundaries::Explicit(
              {0, 5, 10, 15, 20, 40, 60, 80, 100, 200, 400, 600, 800,
               1000})))
      .add_column(MethodKey())
      .RegisterForExport();

  // 3. Lines count: just a count aggregation on the line lengths measure
  opencensus::stats::ViewDescriptor()
      .set_name("ocquickstart.io/lines_in")
      .set_description("The number of lines read in")
      .set_measure(kLineLengthsMeasureName)
      .set_aggregation(opencensus::stats::Aggregation::Count())
      .add_column(MethodKey())
      .RegisterForExport();


  // Register stdout exporters.
  opencensus::exporters::stats::StdoutExporter::Register();
  opencensus::exporters::trace::StdoutExporter::Register();

  // Call measure so that it is initialized.
  VideoSizeMeasure();

  // Create view to see the processed video size distribution broken down by
  // frontend. The view has bucket boundaries (0, 16 * kMiB, 65536 * kMiB) that
  // will group measure values into histogram buckets.
  const opencensus::stats::ViewDescriptor video_size_view =
      opencensus::stats::ViewDescriptor()
          .set_name(kVideoSizeViewName)
          .set_description("processed video size over time")
          .set_measure(kVideoSizeMeasureName)
          .set_aggregation(opencensus::stats::Aggregation::Distribution(
              opencensus::stats::BucketBoundaries::Explicit(
                  {0, 16 * kMiB, 256 * kMiB})))
          .add_column(FrontendKey());
  return video_size_view;
}

std::string getLine() {
  absl::Time start = absl::Now();

  std::string input;

  // Get the line
  std::getline(std::cin, input);

  absl::Time end = absl::Now();
  double latency_ms = absl::ToDoubleMilliseconds(end - start);

  // Record both measures at once.
  opencensus::stats::Record({{LatencyMsMeasure(), latency_ms},
                             {LineLengthsMeasure(), input.length()}},
                            {{MethodKey(), "getLine"}});
  return input;
}

std::string processLine(const std::string& in) {
  absl::Time start = absl::Now();
  std::string out(in);

  for (auto it = out.begin(); it != out.end(); it++) {
    *it = std::toupper(*it);
  }

  absl::Time end = absl::Now();
  double latency_ms = absl::ToDoubleMilliseconds(end - start);

  opencensus::stats::Record({{LatencyMsMeasure(), latency_ms}},
                            {{MethodKey(), "processLine"}});
  return out;
}

// Simple program that collects data for video size.
int main(int argc, char **argv) {
  if (argc != 1) {
    std::cerr << "Usage: " << argv[0] << "\n";
    return 1;
  }
  srand(time(NULL));

//  // Firstly enable the Prometheus exporter
//  auto exporter =
//      std::make_shared<opencensus::exporters::stats::PrometheusExporter>();
//  // Expose Prometheus on :8888
//  prometheus::Exposer exposer("127.0.0.1:8888");
//  exposer.RegisterCollectable(exporter);

  auto video_size_view = RegisterViews();  // trace
  opencensus::stats::View view(video_size_view);
  video_size_view.RegisterForExport();

  // Samplers are potentially expensive to construct. Use one long-lived sampler
  // instead of constructing one per Span.
  static opencensus::trace::AlwaysSampler sampler;

  // Done initializing. Video processing starts here:
  auto span = opencensus::trace::Span::StartSpan("my.org/ProcessVideo", nullptr,
                                                 {&sampler});
  span.AddAnnotation("Start processing video.");
  // Sleep for [1,10] milliseconds to fake work.
  absl::SleepFor(absl::Milliseconds(rand() % 10 + 1));
  // Record the processed video size.
  opencensus::stats::Record({{VideoSizeMeasure(), 25 * kMiB}},
                            {{FrontendKey(), "video size"}});
  span.AddAnnotation("Finished processing video.");
  span.End();

  std::cout << "video_size_view definitions:" << video_size_view.DebugString()
            << "\n\n";

  std::cout << "Waiting 10.1s for exporters to run...\n\n";
  absl::SleepFor(absl::Milliseconds(10100));

  std::cout << "View data:\n";
  const auto data = view.GetData();
  assert(data.type() == opencensus::stats::ViewData::Type::kDistribution);
  for (auto &it : data.distribution_data()) {
    std::cout << "  ";
    for (auto &name : it.first) std::cout << name << " : ";
    std::cout << it.second.DebugString() << "\n";
  }
}
输出 

video_size_view definitions:
  name: "my.org/views/video_size"
  measure: name: "my.org/measure/video_size"; units: "By"; description: "size of processed videos"; type: int64
  aggregation: Distribution with Buckets: 0,1.67772e+07,2.68435e+08
  aggregation window: Cumulative
  columns: my.org/keys/frontend
  description: "processed video size over time"
  expiry duration: 0

Waiting 10.1s for exporters to run...

Name: my.org/ProcessVideo
TraceId-SpanId-Options: 8ad59ba5bb062620a8b1b82d330e4447-f815861ab2819a74-01
Parent SpanId: 0000000000000000 (remote: false)
Start time: 2024-08-28T06:18:33.2866047+00:00
End time: 2024-08-28T06:18:33.305601+00:00
Attributes: (0 dropped)
Annotations: (0 dropped)
  2024-08-28T06:18:33.2866536+00:00: Start processing video.
  2024-08-28T06:18:33.3055938+00:00: Finished processing video.
Message events: (0 dropped)
Links: (0 dropped)
Span ended: true
Status: OK

No data for view "ocquickstart.io/latency".
No data for view "ocquickstart.io/line_lengths".
No data for view "ocquickstart.io/lines_in".
Data for view "my.org/views/video_size":
Row data from 2024-08-28T06:18:38.2963482+00:00 to 2024-08-28T06:18:43.296423969+00:00:
  my.org/keys/frontend=video size
    count: 1 mean: 2.62144e+07 sum of squared deviation: 0 min: 2.62144e+07 max: 2.62144e+07
    histogram counts: 0, 0, 1, 0

View data:
  video size : count: 1 mean: 2.62144e+07 sum of squared deviation: 0 min: 2.62144e+07 max: 2.62144e+07
histogram counts: 0, 0, 1, 0

参考

C++ tracy性能分析-CSDN博客

gprof性能分析_gprof分析-CSDN博客

C++性能调优工具_c++调优工具-CSDN博客

C++性能调优-CSDN博客

GitHub - census-instrumentation/opencensus-cpp: A stats collection and distributed tracing framework

https://github.com/prometheus/prometheus 

https://github.com/jupp0r/prometheus-cpp

Overview | Prometheus


创作不易,小小的支持一下吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值