Skip to content

Commit

Permalink
add_stats method added
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRuiz committed Mar 27, 2019
1 parent e2afe8f commit b0140b3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,19 @@ www.reqres.in:443:
:average: 0.20434114699999997
```

If you want to add specific stats for your processes you can use the method `NiceHttp.add_stats`

```ruby
started = Time.now
@http.send_request Requests::Customer.add_customer
30.times do
resp = @http.get(Requests::Customer.get_customer)
break if resp.code == 200
sleep 0.5
end
NiceHttp.add_stats(:customer, :create, started, Time.now)
```

## Contributing

Bug reports are very welcome on GitHub at https://github.com/marioruiz/nice_http.
Expand Down
38 changes: 38 additions & 0 deletions lib/nice_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ def self.defaults=(par = {})
@create_stats = par[:create_stats] if par.key?(:create_stats)
end

######################################################
# To add specific stats
# The stats will be added to NiceHttp.stats[:specific]
#
# @param name [Symbol] name to group your specific stats
# @param state [Symbol] state of the name supplied to group your specific stats
# @param started [Time] when the process you want the stats started
# @param finished [Time] when the process you want the stats finished
#
# @example
# started = Time.now
# @http.send_request Requests::Customer.add_customer
# 30.times do
# resp = @http.get(Requests::Customer.get_customer)
# break if resp.code == 200
# sleep 0.5
# end
# NiceHttp.add_stats(:customer, :create, started, Time.now)
######################################################
def self.add_stats(name, state, started, finished)
self.stats[:specific] ||= {}
self.stats[:specific][name] ||= {num: 0, time_elapsed: {total:0, maximum:0, minimum:1000, average: 0}}
self.stats[:specific][name][:num] += 1
time_elapsed = self.stats[:specific][name][:time_elapsed]
time_elapsed[:total] += finished - started
time_elapsed[:maximum] = (finished - started) if time_elapsed[:maximum]<(finished-started)
time_elapsed[:minimum] = (finished - started) if time_elapsed[:minimum]>(finished-started)
time_elapsed[:average] = time_elapsed[:total]/self.stats[:specific][name][:num]

self.stats[:specific][name][state] ||= {num: 0, time_elapsed: {total:0, maximum:0, minimum:1000, average: 0}}
self.stats[:specific][name][state][:num] += 1
time_elapsed = self.stats[:specific][name][state][:time_elapsed]
time_elapsed[:total] += finished - started
time_elapsed[:maximum] = (finished - started) if time_elapsed[:maximum]<(finished-started)
time_elapsed[:minimum] = (finished - started) if time_elapsed[:minimum]>(finished-started)
time_elapsed[:average] = time_elapsed[:total]/self.stats[:specific][name][state][:num]
end

######################################################
# Creates a new http connection.
#
Expand Down
1 change: 1 addition & 0 deletions lib/nice_http/manage_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,5 @@ def create_stats(resp)
set_stats(self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response][resp.code])
end
end

end
4 changes: 2 additions & 2 deletions nice_http.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'nice_http'
s.version = '1.7.0'
s.version = '1.7.1'
s.summary = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources."
s.description = "NiceHttp -- simplest library for accessing and testing HTTP and REST resources. Manage different hosts on the fly. Easily get the value you want from the JSON strings. Use hashes on your requests."
s.authors = ["Mario Ruiz"]
Expand All @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = ["LICENSE","README.md"]
s.homepage = 'https://github.com/MarioRuiz/nice_http'
s.license = 'MIT'
s.add_runtime_dependency 'nice_hash', '~> 1.11', '>= 1.11.0'
s.add_runtime_dependency 'nice_hash', '~> 1.12', '>= 1.12.0'
s.add_development_dependency 'rspec', '~> 3.8', '>= 3.8.0'
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ["lib"]
Expand Down
66 changes: 57 additions & 9 deletions spec/nice_http/nice_http_stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

RSpec.describe NiceHttp do
let(:klass) { Class.new NiceHttp }

describe "stats" do
describe "all" do
it "counts correctly the number of requests" do
Expand Down Expand Up @@ -98,18 +98,18 @@
it "counts correctly the number of requests" do
klass.create_stats = true
http = klass.new("http://example.com")
resp = http.get({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:num_requests]).to eq 1
resp = http.get({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:num_requests]).to eq 2
end
it "counts correctly time_elapsed" do
klass.create_stats = true
http = klass.new("http://example.com")
resp = http.get({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:time_elapsed][:total]).to eq resp[:time_elapsed]
prev_time = resp[:time_elapsed]
resp = http.get({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:time_elapsed][:total]).to eq (resp[:time_elapsed] + prev_time)
expect(klass.stats[:name]["exam_name"][:time_elapsed][:maximum]).to be >= klass.stats[:all][:time_elapsed][:minimum]
expect(klass.stats[:name]["exam_name"][:time_elapsed][:minimum]).to be <= klass.stats[:all][:time_elapsed][:maximum]
Expand All @@ -118,8 +118,8 @@
it "creates correctly the http method stats" do
klass.create_stats = true
http = klass.new("http://example.com")
resp = http.get({path: "/", name: 'exam_name'})
resp = http.post({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
resp = http.post({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:method].keys).to eq (["GET", "POST"])
expect(klass.stats[:name]["exam_name"][:method]["GET"].keys).to eq ([:num_requests, :time_elapsed, :response])
expect(klass.stats[:name]["exam_name"][:method]["GET"][:time_elapsed][:total]).to be > 0
Expand All @@ -128,13 +128,61 @@
it "creates correctly the http response stats" do
klass.create_stats = true
http = klass.new("http://example.com")
resp = http.get({path: "/", name: 'exam_name'})
resp = http.post({path: "/", name: 'exam_name'})
resp = http.get({path: "/", name: "exam_name"})
resp = http.post({path: "/", name: "exam_name"})
expect(klass.stats[:name]["exam_name"][:method]["GET"][:response].keys).to eq (["200"])
expect(klass.stats[:name]["exam_name"][:method]["GET"][:response]["200"].keys).to eq ([:num_requests, :time_elapsed])
expect(klass.stats[:name]["exam_name"][:method]["GET"][:response]["200"][:time_elapsed][:total]).to be > 0
end
end

describe "specific" do
it "counts correctly the number of records" do
klass.create_stats = true
started = Time.now
http = klass.new("http://example.com")
resp = http.get({path: "/", name: "exam_name"})
resp = http.get({path: "/", name: "exam_name"})
klass.add_stats(:example, :correct, started, Time.now)
expect(klass.stats[:specific][:example][:num]).to eq 1
expect(klass.stats[:specific][:example][:correct][:num]).to eq 1
started = Time.now
resp = http.get({path: "/", name: "exam_name"})
resp = http.get({path: "/", name: "exam_name"})
klass.add_stats(:example, :correct, started, Time.now)
expect(klass.stats[:specific][:example][:num]).to eq 2
expect(klass.stats[:specific][:example][:correct][:num]).to eq 2
started = Time.now
http = klass.new("http://example.com")
resp = http.get({path: "/", name: "exam_name"})
resp = http.get({path: "/", name: "exam_name"})
klass.add_stats(:example, :correct, started, Time.now)
expect(klass.stats[:specific][:example][:num]).to eq 3
expect(klass.stats[:specific][:example][:correct][:num]).to eq 3
end
it "counts correctly time_elapsed" do
klass.create_stats = true
started = Time.now
http = klass.new("http://example.com")
resp = http.get({path: "/", name: "exam_name"})
resp = http.get({path: "/", name: "exam_name"})
finished = Time.now
klass.add_stats(:example, :correct, started, finished)
expect(klass.stats[:specific][:example][:time_elapsed][:total]).to eq (finished-started)
expect(klass.stats[:specific][:example][:correct][:time_elapsed][:total]).to eq (finished-started)
end
it "creates correctly the hash" do
klass.create_stats = true
started = Time.now
http = klass.new("http://example.com")
resp = http.get({path: "/", name: "exam_name"})
resp = http.post({path: "/", name: "exam_name"})
klass.add_stats(:example, :correct, started, Time.now)
expect(klass.stats[:specific][:example].keys).to eq ([:num, :time_elapsed, :correct])
expect(klass.stats[:specific][:example][:time_elapsed].keys).to eq ([:total, :maximum, :minimum, :average])
expect(klass.stats[:specific][:example][:correct].keys).to eq ([:num, :time_elapsed])
expect(klass.stats[:specific][:example][:correct][:time_elapsed].keys).to eq ([:total, :maximum, :minimum, :average])
end
end
end
end

0 comments on commit b0140b3

Please sign in to comment.