mtrc-0.0.4/0000755000175000017500000000000012410622131011676 5ustar debiandebianmtrc-0.0.4/README.markdown0000644000175000017500000000205712410622131014403 0ustar debiandebianMtrc (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 < "?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... mtrc-0.0.4/metadata.yml0000644000175000017500000000206012410622131014177 0ustar debiandebian--- !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@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: [] mtrc-0.0.4/lib/0000755000175000017500000000000012410622131012444 5ustar debiandebianmtrc-0.0.4/lib/mtrc/0000755000175000017500000000000012410622131013411 5ustar debiandebianmtrc-0.0.4/lib/mtrc/rate.rb0000644000175000017500000000051212410622131014667 0ustar debiandebianclass 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 mtrc-0.0.4/lib/mtrc/sorted_samples.rb0000644000175000017500000000336412410622131016770 0ustar debiandebian# 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 mtrc-0.0.4/lib/mtrc/sample.rb0000644000175000017500000000104012410622131015212 0ustar debiandebianclass 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 mtrc-0.0.4/lib/mtrc/version.rb0000644000175000017500000000004412410622131015421 0ustar debiandebianmodule Mtrc VERSION = '0.0.4' end mtrc-0.0.4/lib/mtrc/samples.rb0000644000175000017500000000005612410622131015403 0ustar debiandebianclass Mtrc::Samples # A set of samples. end mtrc-0.0.4/lib/mtrc.rb0000644000175000017500000000045312410622131013740 0ustar debiandebianmodule 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 mtrc-0.0.4/LICENSE0000644000175000017500000000206312410622131012704 0ustar debiandebianThe 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.