This is an automated email from the ASF dual-hosted git repository. cmcfarlen pushed a commit to branch NewAPIMetricsPOC in repository https://gitbox.apache.org/repos/asf/trafficserver.git
commit 4e5de6e08e732487d54db5ca5dc3a0a88400cf60 Author: Chris McFarlen <cmcfar...@apple.com> AuthorDate: Thu Jul 6 15:44:14 2023 -0500 add cmake support and a unittest. Fix crasher --- CMakeLists.txt | 1 + include/api/Metrics.h | 3 ++- src/api/CMakeLists.txt | 26 ++++++++++++++++++++++++++ src/api/Metrics.cc | 2 +- src/api/test_Metrics.cc | 40 ++++++++++++++++++++++++++++++++++++++++ src/records/CMakeLists.txt | 1 + 6 files changed, 71 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d17008f78e..fdb08b4bfc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -335,6 +335,7 @@ configure_file(include/ts/apidefs.h.in include/ts/apidefs.h) enable_testing() +add_subdirectory(src/api) add_subdirectory(src/tscpp/util) add_subdirectory(src/tscpp/api) add_subdirectory(src/tscore) diff --git a/include/api/Metrics.h b/include/api/Metrics.h index 0379c7ceb4..f343c0d786 100644 --- a/include/api/Metrics.h +++ b/include/api/Metrics.h @@ -32,6 +32,7 @@ #include <cstdint> #include "records/P_RecDefs.h" +#include "ts/apidefs.h" namespace ts { @@ -63,7 +64,7 @@ public: virtual ~Metrics() = default; - Metrics() { _addBlob(); } + Metrics() { _blobs[0] = new MetricStorage(); } // Singleton static Metrics &getInstance(); diff --git a/src/api/CMakeLists.txt b/src/api/CMakeLists.txt new file mode 100644 index 0000000000..718d9eda5c --- /dev/null +++ b/src/api/CMakeLists.txt @@ -0,0 +1,26 @@ +####################### +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor license +# agreements. See the NOTICE file distributed with this work for additional information regarding +# copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under +# the License. +# +####################### + +add_library(tsapi STATIC Metrics.cc) + +add_executable(test_Metrics + test_Metrics.cc + ) +target_link_libraries(test_Metrics PRIVATE tsapi tscore) +target_include_directories(test_Metrics PRIVATE ${CMAKE_SOURCE_DIR}/include ${CATCH_INCLUDE_DIR}) + +add_test(NAME test_Metrics COMMAND $<TARGET_FILE:test_Metrics>) diff --git a/src/api/Metrics.cc b/src/api/Metrics.cc index a95588c172..b2d04d31df 100644 --- a/src/api/Metrics.cc +++ b/src/api/Metrics.cc @@ -44,7 +44,7 @@ Metrics::_addBlob() // The mutex must be held before calling this! ink_assert(blob); ink_assert(_cur_blob < Metrics::METRICS_MAX_BLOBS); - _blobs[_cur_blob++] = blob; + _blobs[++_cur_blob] = blob; _cur_off = 0; } diff --git a/src/api/test_Metrics.cc b/src/api/test_Metrics.cc new file mode 100644 index 0000000000..83f7d4df80 --- /dev/null +++ b/src/api/test_Metrics.cc @@ -0,0 +1,40 @@ +/** @file + + TextView unit tests. + + @section license License + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#define CATCH_CONFIG_MAIN +#include "catch.hpp" + +#include "api/Metrics.h" + +TEST_CASE("Metrics", "[libtsapi][Metrics]") +{ + ts::Metrics m; + + auto fooid = m.newMetric("foo"); + + REQUIRE(fooid == 0); + + m.increment(fooid); + + m.recordsDump([](RecT, void *, int, const char *name, int value, RecData *) { printf("Fooo: %s: %d\n", name, value); }, nullptr); +} diff --git a/src/records/CMakeLists.txt b/src/records/CMakeLists.txt index a833909460..c4c01f1a1b 100644 --- a/src/records/CMakeLists.txt +++ b/src/records/CMakeLists.txt @@ -48,6 +48,7 @@ target_link_libraries(records_p #ts::inkevent cyclic dependency; I_RecProcess.h and P_RecProcess.h ts::tscore ts::tscpputil + tsapi yaml-cpp::yaml-cpp )