feat add microprile examples
Some checks failed
sm-rpc / build (Release, host.gcc) (push) Failing after 11m15s
sm-rpc / build (Release, arm-linux-gnueabihf) (push) Failing after 11m28s
sm-rpc / build (Release, aarch64-linux-gnu) (push) Failing after 11m50s
sm-rpc / build (Debug, mipsel-linux-gnu) (push) Failing after 12m3s
sm-rpc / build (Debug, host.gcc) (push) Failing after 12m13s
sm-rpc / build (Debug, arm-linux-gnueabihf) (push) Failing after 12m30s
sm-rpc / build (Debug, aarch64-linux-gnu) (push) Failing after 12m42s
sm-rpc / build (Release, mipsel-linux-gnu) (push) Failing after 14m53s

This commit is contained in:
tqcq
2025-08-25 12:01:16 +08:00
parent c8b9782baa
commit a58517497b
4 changed files with 42 additions and 76 deletions

View File

@@ -41,7 +41,10 @@ check_cxx_source_compiles(
HAVE_PTHREAD)
add_subdirectory(third_party/breakpad)
add_subdirectory(third_party/microprofile)
if(WITH_MICROPROFILE)
add_subdirectory(third_party/microprofile)
set(COMM_LINK_MICROPROFILE ON)
endif()
CPMAddPackage(
NAME oatpp
@@ -58,6 +61,10 @@ CPMAddPackage(
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog
FORCE)
if(WITH_EXAMPLES)
cc_executable(microprofile_demo SRCS examples/microprofile_demo.cc)
endif()
enable_testing()
cc_test(
calc_test
@@ -71,14 +78,6 @@ cc_executable(
SRCS src/breakpad_test.cc
DEPS PUBLIC breakpad)
cc_executable(
metric_server
SRCS src/metric_server.cc
DEPS PUBLIC
prometheus-cpp::core
prometheus-cpp::pull
prometheus-cpp::push)
cc_benchmark(
calc_benchmark
SRCS src/calc_benchmark.cc

View File

@@ -5,6 +5,9 @@ function(comm_link TARGET_NAME)
if(COMM_LINK_PTHREAD)
target_link_libraries(${TARGET_NAME} PUBLIC pthread)
endif()
if(COMM_LINK_MICROPROFILE)
target_link_libraries(${TARGET_NAME} PUBLIC microprofile)
endif()
endfunction()
# cc_library(name STATIC SRCS a.c b.c DEPS PUBLIC pub_lib PRIVATE pri_lib)

View File

@@ -0,0 +1,31 @@
#include <microprofile.h>
#include <stdio.h>
#include <chrono>
#include <thread>
MICROPROFILE_DEFINE(MAIN, "MAIN", "Main", MP_AUTO);
void Test()
{
MICROPROFILE_TIMELINE_SCOPE(MP_AUTO, "one");
}
int main()
{
MicroProfileOnThreadCreate("Main");
MicroProfileSetEnableAllGroups(true);
MicroProfileSetForceMetaCounters(true);
printf("port: %d\n", MicroProfileWebServerPort());
MicroProfileStartAutoFlip(30);
while (true)
{
MICROPROFILE_SCOPE(MAIN);
Test();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
MicroProfileStopAutoFlip();
MicroProfileShutdown();
return 0;
}

View File

@@ -1,67 +0,0 @@
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>
#include <array>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>
int main()
{
using namespace prometheus;
// create an http server running on port 8080
Exposer exposer{"127.0.0.1:8080"};
// create a metrics registry
// @note it's the users responsibility to keep the object alive
auto registry = std::make_shared<Registry>();
// add a new counter family to the registry (families combine values with the
// same name, but distinct label dimensions)
//
// @note please follow the metric-naming best-practices:
// https://prometheus.io/docs/practices/naming/
auto &packet_counter =
BuildCounter().Name("observed_packets_total").Help("Number of observed packets").Register(*registry);
// add and remember dimensional data, incrementing those is very cheap
auto &tcp_rx_counter = packet_counter.Add({{"protocol", "tcp"}, {"direction", "rx"}});
auto &tcp_tx_counter = packet_counter.Add({{"protocol", "tcp"}, {"direction", "tx"}});
auto &udp_rx_counter = packet_counter.Add({{"protocol", "udp"}, {"direction", "rx"}});
auto &udp_tx_counter = packet_counter.Add({{"protocol", "udp"}, {"direction", "tx"}});
// add a counter whose dimensional data is not known at compile time
// nevertheless dimensional values should only occur in low cardinality:
// https://prometheus.io/docs/practices/naming/#labels
auto &http_requests_counter =
BuildCounter().Name("http_requests_total").Help("Number of HTTP requests").Register(*registry);
// ask the exposer to scrape the registry on incoming HTTP requests
exposer.RegisterCollectable(registry);
for (;;)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
const auto random_value = std::rand();
if (random_value & 1)
tcp_rx_counter.Increment();
if (random_value & 2)
tcp_tx_counter.Increment();
if (random_value & 4)
udp_rx_counter.Increment();
if (random_value & 8)
udp_tx_counter.Increment();
const std::array<std::string, 4> methods = {"GET", "PUT", "POST", "HEAD"};
auto method = methods.at(random_value % methods.size());
// dynamically calling Family<T>.Add() works but is slow and should be
// avoided
http_requests_counter.Add({{"method", method}}).Increment();
}
return 0;
}