From 5ab5de4198d03e1b71de2e1da2eabef556baef79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Fri, 24 Jun 2022 15:36:34 -1000 Subject: [PATCH] (#12) Switch from Beefcake to Protobuf Performance of the Protobuf library is slightly better: comparing the test suite "benchmark" (should query quickly, should be threadsafe) before and after the change, we have the following figures: Queries / second: | Protocol | Before | After | Improvement | |:---------|--------:|-------:|------------:| | TLS | 544.38 | 2957.54| 543% | | TCP | 1076.96 | 3688.43| 342% | | UDP | 1282.52 | 3644.92| 284% | Inserts / second: | Protocol | Before | After | Improvement | |:---------|--------:|--------:|------------:| | TLS | 1452.51 | 5155.62 | 354% | | TCP | 2860.25 | 6709.74 | 234% | | UDP | 4834.24 | 8158.35 | 168% | --- lib/riemann.rb | 3 --- lib/riemann/attribute.rb | 8 +++----- lib/riemann/event.rb | 40 ++++++++++++++++++++-------------------- lib/riemann/message.rb | 14 ++++++-------- lib/riemann/query.rb | 4 +--- lib/riemann/state.rb | 24 ++++++++++++------------ riemann-client.gemspec | 2 +- 7 files changed, 43 insertions(+), 52 deletions(-) diff --git a/lib/riemann.rb b/lib/riemann.rb index 1cb676e..9b00d52 100644 --- a/lib/riemann.rb +++ b/lib/riemann.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true module Riemann - require 'rubygems' - require 'beefcake' - require 'timeout' require 'riemann/version' require 'riemann/state' require 'riemann/attribute' diff --git a/lib/riemann/attribute.rb b/lib/riemann/attribute.rb index acd4d39..eabd02e 100644 --- a/lib/riemann/attribute.rb +++ b/lib/riemann/attribute.rb @@ -1,10 +1,8 @@ # frozen_string_literal: true module Riemann - class Attribute - include Beefcake::Message - - required :key, :string, 1 - optional :value, :string, 2 + class Attribute < Protobuf::Message + required :string, :key, 1 + optional :string, :value, 2 end end diff --git a/lib/riemann/event.rb b/lib/riemann/event.rb index e735200..9ae7453 100644 --- a/lib/riemann/event.rb +++ b/lib/riemann/event.rb @@ -1,30 +1,29 @@ # frozen_string_literal: true module Riemann - class Event + class Event < Protobuf::Message require 'set' - include Beefcake::Message - - optional :time, :int64, 1 - optional :state, :string, 2 - optional :service, :string, 3 - optional :host, :string, 4 - optional :description, :string, 5 - repeated :tags, :string, 7 - optional :ttl, :float, 8 - repeated :attributes, Attribute, 9 - optional :time_micros, :int64, 10 - - optional :metric_sint64, :sint64, 13 - optional :metric_d, :double, 14 - optional :metric_f, :float, 15 + + optional :int64, :time, 1 + optional :string, :state, 2 + optional :string, :service, 3 + optional :string, :host, 4 + optional :string, :description, 5 + repeated :string, :tags, 7 + optional :float, :ttl, 8 + repeated Attribute, :attributes, 9 + optional :int64, :time_micros, 10 + + optional :sint64, :metric_sint64, 13 + optional :double, :metric_d, 14 + optional :float, :metric_f, 15 # Fields which don't really exist in protobufs, but which are reserved # and can't be used as attributes. VIRTUAL_FIELDS = Set.new([:metric]) # Fields which are specially encoded in the Event protobuf--that is, they # can't be used as attributes. - RESERVED_FIELDS = fields.map do |_i, field| + RESERVED_FIELDS = fields.map do |field| field.name.to_sym end.reduce(VIRTUAL_FIELDS) do |set, field| # rubocop:disable Style/MultilineBlockChain set << field @@ -190,9 +189,10 @@ def initialize(hash = nil) end def metric - metric_d || - metric_sint64 || - metric_f + return metric_d if field?(:metric_d) + return metric_sint64 if field?(:metric_sint64) + + metric_f end def metric=(value) diff --git a/lib/riemann/message.rb b/lib/riemann/message.rb index 372ad08..610302c 100644 --- a/lib/riemann/message.rb +++ b/lib/riemann/message.rb @@ -1,14 +1,12 @@ # frozen_string_literal: true module Riemann - class Message - include Beefcake::Message - - optional :ok, :bool, 2 - optional :error, :string, 3 - repeated :states, State, 4 - optional :query, Query, 5 - repeated :events, Event, 6 + class Message < Protobuf::Message + optional :bool, :ok, 2 + optional :string, :error, 3 + repeated State, :states, 4 + optional Query, :query, 5 + repeated Event, :events, 6 def encode_with_length encoded_string = encode.to_s diff --git a/lib/riemann/query.rb b/lib/riemann/query.rb index aaef526..546a43a 100644 --- a/lib/riemann/query.rb +++ b/lib/riemann/query.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true module Riemann - class Query - include Beefcake::Message - + class Query < Protobuf::Message optional :string, :string, 1 end end diff --git a/lib/riemann/state.rb b/lib/riemann/state.rb index 948c9bb..6bad8dd 100644 --- a/lib/riemann/state.rb +++ b/lib/riemann/state.rb @@ -1,18 +1,18 @@ # frozen_string_literal: true -module Riemann - class State - include Beefcake::Message +require 'protobuf' - optional :time, :int64, 1 - optional :state, :string, 2 - optional :service, :string, 3 - optional :host, :string, 4 - optional :description, :string, 5 - optional :once, :bool, 6 - repeated :tags, :string, 7 - optional :ttl, :float, 8 - optional :metric_f, :float, 15 +module Riemann + class State < Protobuf::Message + optional :int64, :time, 1 + optional :string, :state, 2 + optional :string, :service, 3 + optional :string, :host, 4 + optional :string, :description, 5 + optional :bool, :once, 6 + repeated :string, :tags, 7 + optional :float, :ttl, 8 + optional :float, :metric_f, 15 def initialize super diff --git a/riemann-client.gemspec b/riemann-client.gemspec index 5917caf..fd089d5 100644 --- a/riemann-client.gemspec +++ b/riemann-client.gemspec @@ -29,6 +29,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rubocop' spec.add_development_dependency 'timecop' - spec.add_dependency 'beefcake', ['>= 1.0.0 '] spec.add_dependency 'mtrc', '>= 0.0.4' + spec.add_dependency 'protobuf', '~> 3.0' end