Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of HistogramVec by providing LuaC bindings to Rust module prometheus #502

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ doc/locale/en/

build.luarocks
packpack
metrics-rs/target
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(metrics NONE)

if(NOT DEFINED TARANTOOL_INSTALL_LIBDIR)
set(TARANTOOL_INSTALL_LIBDIR "${PROJECT_SOURCE_DIR}/.rocks/lib/tarantool")
endif()

add_subdirectory(metrics-rs)

## Install ####################################################################
###############################################################################

Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ update-pot:
update-po:
sphinx-intl update -p doc/locale/en/ -d doc/locale/ -l "ru"

bench: .rocks
$(TTCTL) rocks install --server https://moonlibs.org luabench 0.3.0
$(TTCTL) rocks install --server https://luarocks.org argparse 0.7.1
.rocks/bin/luabench -d 3s

.PHONY: clean
clean:
rm -rf .rocks
195 changes: 195 additions & 0 deletions benchmarks/histogram_bench.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
local metrics = require 'metrics'
local luahist = assert(metrics.histogram, 'no histogram')
local rusthist = assert(metrics.histogram_vec, 'no histogram_vec')

local M = {}

do
local lh = luahist('no_labels', 'histogram')
local rh = rusthist('rust_no_labels', 'histogram')
local random = math.random

---@param b luabench.B
function M.bench_001_no_labels_001_observe(b)
b:run("histogram:observe", function(sb)
for _ = 1, sb.N do lh:observe(random()) end
end)
b:run("histogram_vec:observe", function(sb)
for _ = 1, sb.N do rh:observe(random()) end
end)
end

---@param b luabench.B
function M.bench_001_no_labels_002_collect(b)
b:run("histogram:collect", function(sb)
for _ = 1, sb.N do lh:collect() end
end)
b:run("histogram_vec:collect", function(sb)
for _ = 1, sb.N do rh:collect() end
end)
b:run("histogram_vec:collect_str", function(sb)
for _ = 1, sb.N do rh:collect_str() end
end)
end
end

do
local lh = luahist('one_label', 'histogram')
local rh = rusthist('rust_one_label', 'histogram', {'status'})
local random = math.random

---@param b luabench.B
function M.bench_002_one_label_001_observe(b)
b:run("histogram:observe", function(sb)
for _ = 1, sb.N do
local x = random()
lh:observe(x, {status = x < 0.99 and 'ok' or 'fail'})
end
end)
b:run("histogram_vec:observe", function(sb)
for _ = 1, sb.N do
local x = random()
rh:observe(x, {status = x < 0.99 and 'ok' or 'fail'})
end
end)
end

---@param b luabench.B
function M.bench_002_one_label_002_collect(b)
b:run("histogram:collect", function(sb)
for _ = 1, sb.N do lh:collect() end
end)
b:run("histogram_vec:collect", function(sb)
for _ = 1, sb.N do rh:collect() end
end)
b:run("histogram_vec:collect_str", function(sb)
for _ = 1, sb.N do rh:collect_str() end
end)
end
end

do
local lh = luahist('two_labels', 'histogram')
local rh = rusthist('rust_two_labels', 'histogram', {'status', 'method'})
local random = math.random

local methods = {'GET', 'POST', 'PUT', 'DELETE'}

---@param b luabench.B
function M.bench_002_two_labels_001_observe(b)
b:run("histogram:observe", function(sb)
for _ = 1, sb.N do
local x = random()
lh:observe(x, {status = x < 0.99 and 'ok' or 'fail', method = methods[random(1, 4)]})
end
end)
b:run("histogram_vec:observe", function(sb)
for _ = 1, sb.N do
local x = random()
rh:observe(x, {status = x < 0.99 and 'ok' or 'fail', method = methods[random(1, 4)]})
end
end)
end

---@param b luabench.B
function M.bench_002_two_labels_002_collect(b)
b:run("histogram:collect", function(sb)
for _ = 1, sb.N do lh:collect() end
end)
b:run("histogram_vec:collect", function(sb)
for _ = 1, sb.N do rh:collect() end
end)
b:run("histogram_vec:collect_str", function(sb)
for _ = 1, sb.N do rh:collect_str() end
end)
end
end

do
local lh = luahist('three_labels', 'histogram')
local rh = rusthist('rust_three_labels', 'histogram', {'status', 'method', 'func'})
local random = math.random

local methods = {'GET', 'POST', 'PUT', 'DELETE'}
local funcs = {
'api.pet.find_by_status',
'api.pet.find_by_id',
'api.pet.get',
'api.pet.find_by_tags',
'api.pet.post',
'api.pet.put',
'api.pet.delete',
'api.store.inventory.get',
'api.store.order.post',
'api.store.order.get',
'api.store.order.delete',
'api.user.get',
'api.user.put',
'api.user.delete',
'api.user.post',
'api.user.login',
'api.user.logout',
}
local n_funcs = #funcs

---@param b luabench.B
function M.bench_003_three_labels_001_observe(b)
b:run("histogram:observe", function(sb)
for _ = 1, sb.N do
local x = random()
lh:observe(x, {
status = x < 0.99 and 'ok' or 'fail',
method = methods[random(1, 4)],
func = funcs[random(1, n_funcs)],
})
end
end)
b:run("histogram_vec:observe", function(sb)
for _ = 1, sb.N do
local x = random()
rh:observe(x, {
status = x < 0.99 and 'ok' or 'fail',
method = methods[random(1, 4)],
func = funcs[random(1, n_funcs)],
})
end
end)
end

---@param b luabench.B
function M.bench_003_three_labels_002_collect(b)
b:run("histogram:collect", function(sb)
for _ = 1, sb.N do lh:collect() end
end)
b:run("histogram_vec:collect", function(sb)
for _ = 1, sb.N do rh:collect() end
end)
b:run("histogram_vec:collect_str", function(sb)
for _ = 1, sb.N do rh:collect_str() end
end)
end
end

do
local rs = require('override.metrics.rs')
function M.bench_004_rust_gather(b)
for _ = 1, b.N do
rs.gather()
end
end

function M.bench_004_lua_gather(b)
for _ = 1, b.N do
local result = {}
for _, c in pairs(metrics.registry.collectors) do
if not c.collect_str then
for _, obs in ipairs(c:collect()) do
table.insert(result, obs)
end
end
end
end
end
end

return M
23 changes: 23 additions & 0 deletions metrics-rs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(metrics-rs NONE)

if (NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
if (NOT DEFINED TARANTOOL_INSTALL_LIBDIR)
message(SEND_ERROR "TARANTOOL_INSTALL_LIBDIR is not defined")
endif()

set(RUST_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/target/${CMAKE_BUILD_TYPE})

find_program(CARGO cargo)
add_custom_target(metrics-rs ALL
COMMAND ${CARGO} build --profile "${CMAKE_BUILD_TYPE}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

install(
FILES ${RUST_BUILD_DIR}/libmetrics_rs${CMAKE_SHARED_LIBRARY_SUFFIX}
RENAME metrics_rs${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION ${TARANTOOL_INSTALL_LIBDIR}
)
Loading