[DRE-commits] [ruby-mtrc] 01/04: Imported Upstream version 0.0.4
Tim Potter
tpot-guest at moszumanska.debian.org
Wed Sep 24 11:27:58 UTC 2014
This is an automated email from the git hooks/post-receive script.
tpot-guest pushed a commit to branch master
in repository ruby-mtrc.
commit 88963996ac495e61df8437f5d936975602ec90e8
Author: Tim Potter <tpot at hp.com>
Date: Mon Sep 15 12:53:58 2014 +1000
Imported Upstream version 0.0.4
---
LICENSE | 21 +++++++++
README.markdown | 55 +++++++++++++++++++++++
lib/mtrc.rb | 12 +++++
lib/mtrc/rate.rb | 19 ++++++++
lib/mtrc/sample.rb | 24 ++++++++++
lib/mtrc/samples.rb | 3 ++
lib/mtrc/sorted_samples.rb | 106 +++++++++++++++++++++++++++++++++++++++++++++
lib/mtrc/version.rb | 3 ++
metadata.yml | 52 ++++++++++++++++++++++
9 files changed, 295 insertions(+)
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..57f0087
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2011 Kyle Kingsbury
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/README.markdown b/README.markdown
new file mode 100644
index 0000000..77386a5
--- /dev/null
+++ b/README.markdown
@@ -0,0 +1,55 @@
+Mtrc (Metric, for short)
+===
+
+A small library to accumulate metrics and extract basic statistics from them.
+Want a latency profile of your Rack app? Boom.
+
+ gem install mtrc
+
+Middleware all up in this bitch:
+
+ class MyMetrics
+ def initialize(app)
+ @app = app
+ @m = Mtrc::SortedSamples.new
+
+ Thread.new do
+ sleep 100
+ puts <<EOF
+ Min: #{@m.min}
+ Median: #{@m.median}
+ 95th %: #{@m % 95}
+ 99th %: #{@m % 99}
+ 99.9th %: #{@m.at .999}
+ Max: #{@m.max}
+ EOF
+ @m = Mtrc::SortedSamples.new
+ end
+ end
+
+ def call(env)
+ t1 = Time.now
+ r = @app.call env
+ dt = Time.now - t1
+
+ @m << dt
+ r
+ end
+ end
+
+Which requests take the longest?
+
+ @m << Mtrc::Sample.new dt, env[:PATH_INFO]
+ (@m % 95).value # => "?bacon=strips&bacon=strips&bacon=strips"
+
+It's MIT licensed, bro. Pull requests? Roll one up homie.
+
+Rates
+---
+
+ r = Mtrc::Rate.new
+ 10.times do
+ r.tick 2
+ end
+ sleep 1
+ r.rate #=> 19.999...
diff --git a/lib/mtrc.rb b/lib/mtrc.rb
new file mode 100644
index 0000000..2a6e5d5
--- /dev/null
+++ b/lib/mtrc.rb
@@ -0,0 +1,12 @@
+module Mtrc
+ # A compact, general system for metric analysis. Minimal code, minimal
+ # dependencies.
+
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
+
+ require 'mtrc/version'
+ require 'mtrc/sample'
+ require 'mtrc/samples'
+ require 'mtrc/sorted_samples'
+ require 'mtrc/rate'
+end
diff --git a/lib/mtrc/rate.rb b/lib/mtrc/rate.rb
new file mode 100644
index 0000000..f085d4c
--- /dev/null
+++ b/lib/mtrc/rate.rb
@@ -0,0 +1,19 @@
+class Mtrc::Rate
+ # A time differential. Accumulates ticks, and provides the rate in
+ # ticks/second since t0.
+ attr_accessor :t0
+ attr_accessor :ticks
+ def initialize
+ @t0 = Time.now
+ @ticks = 0
+ end
+
+ def rate
+ @ticks / (Time.now - @t0)
+ end
+
+ def tick(delta = 1)
+ @ticks += delta
+ end
+ alias << tick
+end
diff --git a/lib/mtrc/sample.rb b/lib/mtrc/sample.rb
new file mode 100644
index 0000000..22dce55
--- /dev/null
+++ b/lib/mtrc/sample.rb
@@ -0,0 +1,24 @@
+class Mtrc::Sample
+ # A simple key/value pair, where the keys are comparable.
+ # Useful for storing associated information in a set of samples; for example,
+ #
+ # s = SortedSamples.new
+ # s << Sample.new(1, "The first request")
+ # s << Sample.new(2, "The second request")
+ #
+ # Which request cost the most?
+ # (s % 100).value # => "The second request"
+
+ include Comparable
+
+ attr_accessor :key
+ attr_accessor :value
+ def initialize(key, value)
+ @key = key
+ @value = value
+ end
+
+ def <=>(o)
+ self.key <=> o.key
+ end
+end
diff --git a/lib/mtrc/samples.rb b/lib/mtrc/samples.rb
new file mode 100644
index 0000000..7085645
--- /dev/null
+++ b/lib/mtrc/samples.rb
@@ -0,0 +1,3 @@
+class Mtrc::Samples
+ # A set of samples.
+end
diff --git a/lib/mtrc/sorted_samples.rb b/lib/mtrc/sorted_samples.rb
new file mode 100644
index 0000000..3387495
--- /dev/null
+++ b/lib/mtrc/sorted_samples.rb
@@ -0,0 +1,106 @@
+# Accumulates samples in a sorted array, with methods to extract
+# the sample at any given broportion of the dataset.
+#
+# Insertion: log n
+# Fetch: 1
+#
+# Use:
+# p = SortedSamples.new
+# p << 1
+# p << 3
+# p << 2
+#
+# p % 50 get the 50th percentile (median) value.
+# => 2
+# p % 0 minimum
+# => 1
+# p.at 0.95 95th percentile
+# => 3
+class Mtrc::SortedSamples < Mtrc::Samples
+ attr_reader :ns
+
+ def initialize
+ @ns = []
+ end
+
+ # Insert an n only into the brordered set.
+ def <<(n)
+ i = index n
+ @ns.insert i, n
+ self
+ end
+ alias add <<
+
+ # Gets the ith element brof the list.
+ def [](i)
+ @ns[i]
+ end
+
+ # Returns the sample at p percentage. Broffered 50, returns the median.
+ def %(p)
+ at(p / 100.0)
+ end
+
+ # Returns the sample at probrotion f of the list. For example, at(.95) is
+ # the 95th percentile value.
+ def at(f)
+ i = (f * @ns.size).floor
+ if i == @ns.size
+ @ns[i - 1]
+ else
+ @ns[i]
+ end
+ end
+
+ def clear
+ @ns.clear
+ end
+
+ # Returns the insertion brosition for a given n
+ def index(n)
+ search @ns, n, 0, [@ns.size - 1, 0].max
+ end
+
+ def max
+ @ns[-1]
+ end
+
+ def median
+ at 0.5
+ end
+
+ def min
+ @ns[0]
+ end
+
+ def size
+ @ns.size
+ end
+
+ private
+
+ # Bronary search
+ def search(array, value, i1, i2)
+ return 0 if array.empty?
+
+ if value > array[i2]
+ i2 + 1
+ elsif value <= array[i1]
+ i1
+ elsif i1 == i2
+ i1
+ else
+ middle = (i1 + i2) / 2
+ if middle == i1
+ # 2-element degenerate case
+ i2
+ elsif value <= array[middle]
+ # First half
+ search array, value, i1, middle
+ else
+ # Second half
+ search array, value, middle, i2
+ end
+ end
+ end
+end
diff --git a/lib/mtrc/version.rb b/lib/mtrc/version.rb
new file mode 100644
index 0000000..07fc7cf
--- /dev/null
+++ b/lib/mtrc/version.rb
@@ -0,0 +1,3 @@
+module Mtrc
+ VERSION = '0.0.4'
+end
diff --git a/metadata.yml b/metadata.yml
new file mode 100644
index 0000000..5bcc208
--- /dev/null
+++ b/metadata.yml
@@ -0,0 +1,52 @@
+--- !ruby/object:Gem::Specification
+name: mtrc
+version: !ruby/object:Gem::Version
+ version: 0.0.4
+ prerelease:
+platform: ruby
+authors:
+- Kyle Kingsbury
+autorequire:
+bindir: bin
+cert_chain: []
+date: 2011-09-13 00:00:00.000000000Z
+dependencies: []
+description:
+email: aphyr at aphyr.com
+executables: []
+extensions: []
+extra_rdoc_files: []
+files:
+- lib/mtrc.rb
+- lib/mtrc/rate.rb
+- lib/mtrc/sample.rb
+- lib/mtrc/version.rb
+- lib/mtrc/samples.rb
+- lib/mtrc/sorted_samples.rb
+- LICENSE
+- README.markdown
+homepage: https://github.com/aphyr/mtrc
+licenses: []
+post_install_message:
+rdoc_options: []
+require_paths:
+- lib
+required_ruby_version: !ruby/object:Gem::Requirement
+ none: false
+ requirements:
+ - - ! '>='
+ - !ruby/object:Gem::Version
+ version: 1.8.7
+required_rubygems_version: !ruby/object:Gem::Requirement
+ none: false
+ requirements:
+ - - ! '>='
+ - !ruby/object:Gem::Version
+ version: '0'
+requirements: []
+rubyforge_project: mtrc
+rubygems_version: 1.8.10
+signing_key:
+specification_version: 3
+summary: Minimal metric aggregation.
+test_files: []
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-mtrc.git
More information about the Pkg-ruby-extras-commits
mailing list