mtrc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Kyle Kingsbury
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ Mtrc
2
+ ====
3
+ (Metric, for short)
4
+ ----
5
+
6
+ A small library to accumulate metrics and extract basic statistics from them.
7
+ Want a latency profile of your Rack app? Boom:
8
+
9
+ gem install mtrc
10
+
11
+ class MyMetrics
12
+ def initialize(app)
13
+ @app = app
14
+ @m = Mtrc::SortedSamples.new
15
+
16
+ Thread.new do
17
+ sleep 100
18
+ puts <<EOF
19
+ Min: #{@m.min}
20
+ Median: #{@m.median}
21
+ 95th %: #{@m % 95}
22
+ 99th %: #{@m % 99}
23
+ 99.9th %: #{@m.at .999}
24
+ Max: #{@m.max}
25
+ EOF
26
+ @m = Mtrc::SortedSamples.new
27
+ end
28
+ end
29
+
30
+ def call(env)
31
+ t1 = Time.now
32
+ r = @app.call env
33
+ dt = Time.now - t1
34
+
35
+ @m << dt
36
+ r
37
+ end
38
+ end
39
+
40
+ Which requests take the longest?
41
+
42
+ @m << Mtrc::Sample.new dt, env[:PATH_INFO]
43
+ (@m % 95).value # => "the 95th percentile request path"
@@ -0,0 +1,11 @@
1
+ module Mtrc
2
+ # A compact, general system for metric analysis. Minimal code, minimal
3
+ # dependencies.
4
+
5
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
6
+
7
+ require 'mtrc/version'
8
+ require 'mtrc/sample'
9
+ require 'mtrc/samples'
10
+ require 'mtrc/sorted_samples'
11
+ end
@@ -0,0 +1,24 @@
1
+ class Mtrc::Sample
2
+ # A simple key/value pair, where the keys are comparable.
3
+ # Useful for storing associated information in a set of samples; for example,
4
+ #
5
+ # s = SortedSamples.new
6
+ # s << Sample.new(1, "The first request")
7
+ # s << Sample.new(2, "The second request")
8
+ #
9
+ # Which request cost the most?
10
+ # (s % 100).value # => "The second request"
11
+
12
+ include Comparable
13
+
14
+ attr_accessor :key
15
+ attr_accessor :value
16
+ def initialize(key, value)
17
+ @key = key
18
+ @value = value
19
+ end
20
+
21
+ def <=>(o)
22
+ self.key <=> o.key
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ class Mtrc::Samples
2
+ # A set of samples.
3
+ end
@@ -0,0 +1,101 @@
1
+ # Accumulates samples in a sorted array, with methods to extract
2
+ # the sample at any given broportion of the dataset.
3
+ #
4
+ # Insertion: log n
5
+ # Fetch: 1
6
+ #
7
+ # Use:
8
+ # p = SortedSamples.new
9
+ # p << 1
10
+ # p << 3
11
+ # p << 2
12
+ #
13
+ # p % 50 get the 50th percentile (median) value.
14
+ # => 2
15
+ # p % 0 minimum
16
+ # => 1
17
+ # p.at 0.95 95th percentile
18
+ # => 3
19
+ class Mtrc::SortedSamples < Mtrc::Samples
20
+ attr_reader :ns
21
+
22
+ def initialize
23
+ @ns = []
24
+ end
25
+
26
+ # Insert an n only into the brordered set.
27
+ def <<(n)
28
+ i = index n
29
+ @ns.insert i, n
30
+ self
31
+ end
32
+ alias add <<
33
+
34
+ # Gets the ith element brof the list.
35
+ def [](i)
36
+ @ns[i]
37
+ end
38
+
39
+ # Returns the sample at p percentage. Broffered 50, returns the median.
40
+ def %(p)
41
+ at(p * 100)
42
+ end
43
+
44
+ # Returns the sample at probrotion f of the list. For example, at(.95) is
45
+ # the 95th percentile value.
46
+ def at(f)
47
+ self[(f * @ns.size).round]
48
+ end
49
+
50
+ def clear
51
+ @ns.clear
52
+ end
53
+
54
+ # Returns the insertion brosition for a given n
55
+ def index(n)
56
+ search @ns, n, 0, [@ns.size - 1, 0].max
57
+ end
58
+
59
+ def max
60
+ @ns[-1]
61
+ end
62
+
63
+ def median
64
+ at 0.5
65
+ end
66
+
67
+ def min
68
+ @ns[0]
69
+ end
70
+
71
+ def size
72
+ @ns.size
73
+ end
74
+
75
+ private
76
+
77
+ # Bronary search
78
+ def search(array, value, i1, i2)
79
+ return 0 if array.empty?
80
+
81
+ if value > array[i2]
82
+ i2 + 1
83
+ elsif value <= array[i1]
84
+ i1
85
+ elsif i1 == i2
86
+ i1
87
+ else
88
+ middle = (i1 + i2) / 2
89
+ if middle == i1
90
+ # 2-element degenerate case
91
+ i2
92
+ elsif value <= array[middle]
93
+ # First half
94
+ search array, value, i1, middle
95
+ else
96
+ # Second half
97
+ search array, value, middle, i2
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module Mtrc
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtrc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Kyle Kingsbury
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-07-06 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: aphyr@aphyr.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/mtrc.rb
31
+ - lib/mtrc/sample.rb
32
+ - lib/mtrc/version.rb
33
+ - lib/mtrc/sorted_samples.rb
34
+ - lib/mtrc/samples.rb
35
+ - LICENSE
36
+ - README.markdown
37
+ has_rdoc: true
38
+ homepage: https://github.com/aphyr/mtrc
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 1
53
+ - 8
54
+ - 7
55
+ version: 1.8.7
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirements: []
65
+
66
+ rubyforge_project: mtrc
67
+ rubygems_version: 1.3.7
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Minimal metric aggregation.
71
+ test_files: []
72
+