lavin 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed47d43b5e5f91e1af2c3e319fa03d3588aee9826215394038359e559773a173
4
- data.tar.gz: 17261c42f46a7c4e1ecff28adbd92434c5ec66ac28d4c208d6195a55bf49f69d
3
+ metadata.gz: d3a7216c049391b24ccad2eb6c6d6424114761a11b1b4aa15824c5c58ca2bf19
4
+ data.tar.gz: 3ec27a41fdc255d3eafb3a168c39ad8b744b28ab5e436d38d8eaf9f94aa22f23
5
5
  SHA512:
6
- metadata.gz: 747a493c85dd8fe99c5da92d9fc8380248f80a7b0483bfe42ba3f48fc8a06c89ecc45dfa02f424304b75d28709dfd7c35bc9137a658b22cb449dab91a4bb13ff
7
- data.tar.gz: 304de7aa069cb83a61f3530bec34df37349e4572cdc4eb23ec20b86daa4a75606f207248f7b4efe5f3338d8f4166ba94c71ee33e856bc22e6cb36720585627cf
6
+ metadata.gz: c6689ccce7532d8438013a8e3af32052f5b8df828169a026270f03d16a9078f11677b36f2a6c8989ec27cd8bfa3248d4bb1a9bbd2ddc35cccd4d8f1df143e182
7
+ data.tar.gz: 4b2b728d3d5c775471c3ffdcf52c7b2a475d6c7e76d6aae4288b1e1154747e6f81bc802bf3f04f951aa1dec02de690671f110e01db8baed5c5442717f93f2b85
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/lavin/client.rb CHANGED
@@ -17,7 +17,7 @@ module Lavin
17
17
  }.freeze
18
18
 
19
19
  attr_reader :internet, :base_url
20
- attr_accessor :request_count, :cookie
20
+ attr_accessor :request_count, :cookie, :report_statistics
21
21
 
22
22
  def initialize(base_url = nil)
23
23
  raise NoCurrentAsyncTaskError unless Async::Task.current?
@@ -26,6 +26,7 @@ module Lavin
26
26
  @base_url = base_url
27
27
  @request_count = 0
28
28
  @cookie = nil
29
+ @report_statistics = true
29
30
  end
30
31
 
31
32
  def close
@@ -41,7 +42,7 @@ module Lavin
41
42
 
42
43
  status, headers, body = process(response)
43
44
 
44
- Statistics.register_request(method:, url:, status:, duration:)
45
+ Statistics.register_request(method:, url:, status:, duration:) if report_statistics
45
46
 
46
47
  {status:, headers:, body: body}
47
48
  end
data/lib/lavin/hook.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ #
3
+
4
+ module Lavin
5
+ class Hook
6
+ attr_reader :user, :block
7
+
8
+ def initialize(user:, &block)
9
+ @user = user
10
+ @block = block
11
+ end
12
+
13
+ def run(context: nil)
14
+ call(context:)
15
+ Runner.yield
16
+ end
17
+
18
+ def call(context:)
19
+ report_statistics = context.client.report_statistics
20
+ context.client.report_statistics = false
21
+ context.instance_exec(&block)
22
+ rescue => error
23
+ puts "Caught an error - #{error.class}: #{error.message}"
24
+ puts error.backtrace
25
+ ensure
26
+ context.client.report_statistics = report_statistics
27
+ end
28
+ end
29
+ end
@@ -9,7 +9,7 @@ module Lavin
9
9
 
10
10
  def initialize(**kwargs)
11
11
  super(**kwargs)
12
- @client = Client.new(config[:base_url])
12
+ @client = kwargs.fetch(:client) { Client.new(config[:base_url]) }
13
13
  end
14
14
 
15
15
  def cleanup
data/lib/lavin/runner.rb CHANGED
@@ -57,7 +57,7 @@ module Lavin
57
57
  rescue StandardError => error
58
58
  puts "Failed to run in thread: #{error.message}"
59
59
  thread.join
60
- thread = nil
60
+ self.thread = nil
61
61
  raise
62
62
  end
63
63
 
@@ -28,6 +28,10 @@ module Lavin
28
28
  data[:total_requests]
29
29
  end
30
30
 
31
+ def total_steps
32
+ data[:step_summary][:count]
33
+ end
34
+
31
35
  def duration
32
36
  return data[:duration] if data[:duration]
33
37
 
@@ -42,8 +46,13 @@ module Lavin
42
46
  data[:requests][key] << result
43
47
  end
44
48
 
45
- def register_step
46
- # TODO
49
+ def register_step(user:, step_name:, failure: nil)
50
+ data[:step_summary][:count] += 1
51
+ data[:step_summary][:success] += 1 unless failure
52
+ data[:step_summary][:failure] += 1 if failure
53
+ key = [user, step_name]
54
+ data[:steps][key][:success] += 1 unless failure
55
+ data[:steps][key][:failure] += 1 if failure
47
56
  end
48
57
 
49
58
  def stats
@@ -68,7 +77,7 @@ module Lavin
68
77
  statuses: statuses.tally,
69
78
  avg_duration: avg_duration,
70
79
  min_duration: min_duration,
71
- max_duration: max_duration,
80
+ max_duration: max_duration
72
81
  }
73
82
  end
74
83
 
@@ -76,6 +85,8 @@ module Lavin
76
85
  duration: duration,
77
86
  total_requests: total_requests,
78
87
  rate: duration ? format("%.2f", total_requests / duration) : 0,
88
+ step_summary: data[:step_summary],
89
+ steps: data[:steps],
79
90
  requests: requests
80
91
  ).tap do |stats|
81
92
  # FIXME remove!
@@ -88,6 +99,14 @@ module Lavin
88
99
 
89
100
  show_summary(values)
90
101
 
102
+ show_steps(values) do |(user, step), hash|
103
+ format(
104
+ "%-24<user_step>s %8<success>d %8<failure>d",
105
+ user_step: "#{user}.#{step}",
106
+ **hash
107
+ )
108
+ end
109
+
91
110
  show_table(values) do |request_values|
92
111
  format(
93
112
  "%-6<method>s %-100<url>s %6<requests>d %12<avg_duration>fs %12<min_duration>fs %12<max_duration>fs",
@@ -116,7 +135,12 @@ module Lavin
116
135
  start: nil,
117
136
  duration: nil,
118
137
  total_requests: 0,
119
- steps: [],
138
+ step_summary: {
139
+ count: 0,
140
+ success: 0,
141
+ failure: 0
142
+ },
143
+ steps: Hash.new { |h, k| h[k] = {success: 0, failure: 0} },
120
144
  requests: Hash.new { |h, k| h[k] = [] }
121
145
  }
122
146
  end
@@ -127,11 +151,27 @@ module Lavin
127
151
  Lavin results:
128
152
  Test ran for #{values.duration}s
129
153
  Total number of requests: #{values.total_requests}
130
- Rate: #{format("%.2f", values.total_requests/values.duration)} rps
154
+ Rate: #{format("%.2f", values.total_requests / values.duration)} rps
155
+
156
+ Total number of steps: #{values.total_steps}
157
+ Step success rate: #{format("%.2f %%", 100 * values.successful_steps.to_f / values.total_steps)}
131
158
 
132
159
  RESULT
133
160
  end
134
161
 
162
+ def show_steps(values)
163
+ puts format(
164
+ "%-24<user_step>s %8<success>s %8<failure>s",
165
+ user_step: "Steps",
166
+ success: "Success",
167
+ failure: "Failure"
168
+ )
169
+ divider = "-" * 42
170
+ puts divider
171
+ values.each_step { |step_values| puts yield step_values }
172
+ puts "#{divider}\n\n"
173
+ end
174
+
135
175
  def show_table(values)
136
176
  puts format(
137
177
  "%-6<method>s %-100<url>s %-6<requests>s %12<avg_duration>s %12<min_duration>s %12<max_duration>s",
data/lib/lavin/stats.rb CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  module Lavin
4
4
  class Stats
5
- attr_reader :duration, :total_requests, :rate, :requests
5
+ attr_reader :duration, :total_requests, :rate, :requests, :step_summary, :steps
6
6
 
7
- def initialize(duration:, total_requests:, rate:, requests: [])
7
+ def initialize(duration:, total_requests:, rate:, requests: [], step_summary: {}, steps: [])
8
8
  @duration = duration
9
9
  @total_requests = total_requests
10
10
  @rate = rate
11
11
  @requests = requests
12
+ @step_summary = step_summary
13
+ @steps = steps
12
14
  end
13
15
 
14
16
  def empty?
@@ -20,10 +22,28 @@ module Lavin
20
22
  duration:,
21
23
  total_requests:,
22
24
  rate:,
23
- requests:
25
+ requests:,
26
+ step_summary:,
27
+ steps:
24
28
  }
25
29
  end
26
30
 
31
+ def total_steps
32
+ @step_summary[:count]
33
+ end
34
+
35
+ def successful_steps
36
+ @step_summary[:success]
37
+ end
38
+
39
+ def failed_steps
40
+ @step_summary[:failure]
41
+ end
42
+
43
+ def each_step(&block)
44
+ steps.each(&block)
45
+ end
46
+
27
47
  def each_request(&block)
28
48
  requests.each(&block)
29
49
  end
data/lib/lavin/step.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  # frozen_string_literal: true
2
+ #
2
3
 
3
4
  module Lavin
4
5
  class Step
5
- attr_reader :user, :block, :repeat
6
+ attr_reader :name, :user, :block, :repeat
6
7
 
7
- def initialize(repeat: 1, &block)
8
+ def initialize(name:, user:, repeat: 1, &block)
9
+ @name = name
10
+ @user = user
8
11
  @repeat = repeat
9
12
  @block = block
10
13
  end
@@ -18,11 +21,11 @@ module Lavin
18
21
 
19
22
  def call(context:)
20
23
  context.instance_exec(&block)
21
- # Report Success!
24
+ Statistics.register_step(user: user.name, step_name: name)
22
25
  rescue => error
23
26
  puts "Caught an error - #{error.class}: #{error.message}"
24
27
  puts error.backtrace
25
- # Report Failure!
28
+ Statistics.register_step(user: user.name, step_name: name, failure: error.message)
26
29
  end
27
30
  end
28
31
  end
data/lib/lavin/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lavin
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/lavin/worker.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'lavin/step'
4
+ require 'lavin/hook'
4
5
 
5
6
  module Lavin
6
7
  module Worker
@@ -8,21 +9,22 @@ module Lavin
8
9
  def before(&block)
9
10
  return @before unless block
10
11
 
11
- @before = block
12
+ @before = Hook.new(user: self, &block)
12
13
  end
13
14
 
14
15
  def after(&block)
15
16
  return @after unless block
16
17
 
17
- @after = block
18
+ @after = Hook.new(user: self, &block)
18
19
  end
19
20
 
20
21
  def steps
21
22
  @steps ||= []
22
23
  end
23
24
 
24
- def step(**options, &block)
25
- steps << Step.new(**options, &block)
25
+ def step(name: nil, **options, &block)
26
+ name ||= "Step##{steps.size + 1}"
27
+ steps << Step.new(user: self, name:, **options, &block)
26
28
  end
27
29
  end
28
30
 
@@ -38,11 +40,11 @@ module Lavin
38
40
  end
39
41
 
40
42
  def run
41
- self.class.before.call.then { Runner.yield } if self.class.before
43
+ self.class.before.run(context: self).then { Runner.yield } if self.class.before
42
44
 
43
45
  run_step until finished?
44
46
 
45
- self.class.after.call.then { Runner.yield } if self.class.after
47
+ self.class.after.run(context: self).then { Runner.yield } if self.class.after
46
48
  end
47
49
 
48
50
  private
data.tar.gz.sig CHANGED
@@ -1 +1 @@
1
- w���r�We��sk8ƚ�JA��sl��� k*�z4��\��wx
1
+ �r�)ꮠo���%e�[n�a��/&�m�˧*�#�RG�1���2�[L{@u��E��/ıi�����kD3{�<f��o�jNo�*/�C��Q���� ��Һ���y�'��;�UC� �Ф�)���4}q�M�����G��G~��R<�`)�+����n��{�=U~��r̒ ���MW��l�.Y=��Qi��5*S���R��bwC��!��C�J����~0D?�B쬗��r����
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lavin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sammy Henningsson
@@ -29,7 +29,7 @@ cert_chain:
29
29
  DVzXaUnsmwP+jQ1PkDa5q8ibBzMd2c6Hmm87UDqPxZtML0bF9SjrpbyLMjwtXaMA
30
30
  WDPp0ajpdUZ9GPHsrVNYXiOfQIqcmlmpYVsH1o7vuneUIcIDMrnMDChh
31
31
  -----END CERTIFICATE-----
32
- date: 2022-10-30 00:00:00.000000000 Z
32
+ date: 2022-10-31 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: async
@@ -113,6 +113,7 @@ files:
113
113
  - lib/lavin.rb
114
114
  - lib/lavin/client.rb
115
115
  - lib/lavin/error.rb
116
+ - lib/lavin/hook.rb
116
117
  - lib/lavin/http_client.rb
117
118
  - lib/lavin/runner.rb
118
119
  - lib/lavin/statistics.rb
metadata.gz.sig CHANGED
Binary file