siege_siege 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 5132d5648931cb1f5c1fca9dd34de1eadbeb04d6
4
- data.tar.gz: 40e8d21f5127dfbee7b5e8aeca7a0687aba4b6f2
3
+ metadata.gz: 61a8221e13d4085e5cfd876ba6f39e42f9bff7d4
4
+ data.tar.gz: fe46819a05c8e586cddfb5be20857c25df2ef5ae
5
5
  SHA512:
6
- metadata.gz: b73732098ff9da13441255b7a3d1e460f651c2d01420bbb4dab6fe56e49b2f6b5dedb995e4d7585986ddf984637d3230dd133ee8c531798b375424f3f58a5073
7
- data.tar.gz: 94d18ad5ec1f91f00fcddc8e808ca4cdd4cf609161970c17d87c51f8a3de02f8d9c8f300de45e3e0fd035e603c971c8bd460eabc67488e70cfeff27aa8c43bf9
6
+ metadata.gz: 08801251141e63fd9a19a5750f4ecbc6657b99ed7107b714d4b08cda3bab423fd6a781b63c3e650c094564d68cf9cc13cd3a32fc5235a1e9509aae6b43756db8
7
+ data.tar.gz: d295da59d3a1e9bcc6817790355947b082f27d13c22ee3b7f5d805b32d284850b3ed4f5aadd64138ea9ba1730f560e83f859a55ba4379b43e1f926da723bc538
data/README.md CHANGED
@@ -1,41 +1,22 @@
1
1
  # SiegeSiege
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/siege_siege`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ SiegeSiege is siege wrapper.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## SIEGE
6
6
 
7
- ## Installation
7
+ ```
8
+ $ siege -V
9
+ SIEGE 3.1.4
10
+ ```
8
11
 
9
- Add this line to your application's Gemfile:
12
+ ## Installation
10
13
 
11
14
  ```ruby
12
15
  gem 'siege_siege'
13
16
  ```
14
17
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install siege_siege
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/siege_siege.
36
-
37
-
38
- ## License
39
-
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
18
+ then
41
19
 
20
+ ```bash
21
+ $ bundle install
22
+ ```
@@ -0,0 +1,5 @@
1
+ module SiegeSiege
2
+ class AverageLog < Struct.new(:id, :url, :count, :secs, :siege_url)
3
+
4
+ end
5
+ end
@@ -0,0 +1,97 @@
1
+ module SiegeSiege
2
+ class Configuration
3
+ OPTION_MAP = {
4
+ verbose: 'v',
5
+ concurrent: 'c',
6
+ internet: 'i',
7
+ time: 't',
8
+ reps: 'r',
9
+ log: 'l',
10
+ mark: 'm',
11
+ delay: 'd',
12
+ header: 'H',
13
+ user_agent: 'A',
14
+ content_type: 'T',
15
+ rc: 'R',
16
+ file: 'f',
17
+ url: ''
18
+ }
19
+
20
+ RC_MAP = {
21
+ verbose: true,
22
+ quiet: false,
23
+ gmethod: 'HEAD',
24
+ csv: false,
25
+ timestamp: true,
26
+ fullurl: true,
27
+ display_id: true,
28
+ limit: 255,
29
+ show_logfile: true,
30
+ logging: true,
31
+ logfile: nil,
32
+ protocol: 'HTTP/1.1',
33
+ chunked: true,
34
+ cache: false,
35
+ timeout: 2000,
36
+ expire_session: false,
37
+ cookies: false,
38
+ failures: 1024,
39
+ benchmark: true,
40
+ accept_encoding: 'gzip',
41
+ url_escaping: true,
42
+ spinner: false,
43
+ login: nil,
44
+ login_url: nil,
45
+ ftp_login: nil,
46
+ unique: true,
47
+ ssl_cert: nil,
48
+ ssl_key: nil,
49
+ ssl_timeout: nil,
50
+ ssl_ciphers: nil,
51
+ proxy_host: nil,
52
+ proxy_port: nil,
53
+ proxy_login: nil,
54
+ follow_location: true,
55
+ zero_data_ok: true
56
+ }
57
+
58
+ def initialize(configuration)
59
+ @configuration = configuration
60
+ end
61
+
62
+ def urls
63
+ Array(@configuration[:urls]).map { |url|
64
+ (URL === url ? url : URL.new(url))
65
+ }
66
+ end
67
+
68
+ def rc
69
+ RC_MAP.inject('') do |a, (key, value)|
70
+ inserting = @configuration[key] || value
71
+ if inserting
72
+ a << "#{key.to_s.gsub('_', '-')} = #{inserting}\n"
73
+ else
74
+ a
75
+ end
76
+ end
77
+ end
78
+
79
+ def options
80
+ OPTION_MAP.inject([]) { |a, (key, value)|
81
+ inserting = @configuration[key]
82
+ case
83
+ when TrueClass === inserting
84
+ a << "-#{value}"
85
+ when FalseClass === inserting
86
+ a
87
+ when inserting && key == :time
88
+ a << "-#{value} #{@configuration[key]}s"
89
+ when inserting
90
+ a << "-#{value} #{@configuration[key]}"
91
+ else
92
+ a
93
+ end
94
+ }.join(' ')
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,22 @@
1
+ module SiegeSiege
2
+ class LineLog < Struct.new(:week_day, :date, :protocol, :status, :secs, :bytes, :url, :id, :date2, :siege_url)
3
+ def initialize(*)
4
+ super
5
+
6
+ raise InvalidLine unless date2
7
+
8
+ self.secs = secs.to_f
9
+ self.bytes = bytes.to_i
10
+ self.id = id.to_i
11
+ end
12
+
13
+ def take_in_detail(url_map)
14
+ self.siege_url = url_map[id]
15
+ self
16
+ end
17
+
18
+ class InvalidLine < StandardError
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,55 @@
1
+ module SiegeSiege
2
+ class Result
3
+ def initialize(command, urls, raw, raw_result)
4
+ @command = command
5
+ @raw = raw
6
+ @raw_result = raw_result
7
+ @url_map = urls.each_with_index.inject({}) do |a, (url, index)|
8
+ a.merge!(index => url)
9
+ end
10
+
11
+ #puts raw
12
+ end
13
+
14
+ def total_result
15
+ {
16
+ command: @command
17
+ }.merge!(
18
+ @raw_result.split("\n").inject({}) { |a, line|
19
+ if line.include?('unable to create log file')
20
+ a
21
+ elsif re = line.match(/(.+?):[^0-9]*([0-9\.]+) ?(.*)/)
22
+ a.merge!(re[1].gsub(' ', '_').underscore.to_sym => {
23
+ value: re[2].to_f,
24
+ unit: re[3].to_s
25
+ })
26
+ else
27
+ a
28
+ end
29
+ }
30
+ )
31
+ end
32
+
33
+ def raw_log
34
+ @stored_logs ||= begin
35
+ @raw
36
+ .gsub(/\e.+?m/, '')
37
+ .gsub('[', '')
38
+ .gsub(']', '')
39
+ .gsub(' ', '')
40
+ .split("\n")
41
+ .map { |line| LineLog.new(*line.split(',')).take_in_detail(@url_map) rescue nil }
42
+ .compact
43
+ end
44
+ end
45
+
46
+ def average_log
47
+ @stored_average_log ||= raw_log.group_by { |line| line.id }.map { |id, group|
48
+ count = group.size
49
+ average = (group.inject(0) { |a, log| a + log.secs } / count).round(3)
50
+ head = group.first
51
+ AverageLog.new(id, head.url, count, average, head.siege_url)
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,70 @@
1
+ module SiegeSiege
2
+ class Runner
3
+ attr_accessor :conf, :urls
4
+
5
+ def initialize(raw_configuration = {})
6
+ @rc_file = Tempfile.open
7
+ @urls_file = Tempfile.open
8
+ @command = nil
9
+
10
+ Configuration.new(
11
+ {
12
+ concurrent: 1,
13
+ time: 10,
14
+ reps: 1
15
+ }.merge!(raw_configuration).merge!(
16
+ verbose: true,
17
+ rc: @rc_file.path,
18
+ csv: true,
19
+ display_id: true,
20
+ quiet: false,
21
+ file: raw_configuration[:url] ? nil : @urls_file.path
22
+ )
23
+ ).tap do |conf|
24
+ File.write(@rc_file, conf.rc)
25
+ File.write(@urls_file, conf.urls.map(&:to_siege_url).join("\n"))
26
+ @command = "siege #{conf.options}"
27
+ @urls = conf.urls
28
+ end
29
+ end
30
+
31
+ def run
32
+ puts "\e[32m#{@command}\e[0m"
33
+ _, stdout, stderr = Open3.popen3(@command)
34
+
35
+ indicate
36
+
37
+ out = stdout.read
38
+ err = stderr.read
39
+
40
+ indicate_end
41
+
42
+ Result.new(@command, @urls, out, err)
43
+ ensure
44
+ indicate_end
45
+ end
46
+
47
+
48
+ private
49
+
50
+ def indicate
51
+ @indicator = Thread.start do
52
+ chars = %w[| / - \\]
53
+ i = 0
54
+ loop do
55
+ print "\e[31m#{chars[i % chars.length]}\e[0m"
56
+ sleep 0.1
57
+ i += 1
58
+ print "\b"
59
+ end
60
+ end
61
+ end
62
+
63
+ def indicate_end
64
+ return unless @indicator
65
+ Thread.kill(@indicator)
66
+ print "\b"
67
+ @indicator = nil
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,42 @@
1
+ module SiegeSiege
2
+ class URL
3
+ attr_reader :url, :http_method, :parameter
4
+
5
+ def initialize(url, http_method = nil, parameter = nil)
6
+
7
+ splat = url.split(' ')
8
+
9
+ if splat.size > 1
10
+ @url = splat[0]
11
+ @http_method = splat[1].downcase.to_sym
12
+ @parameter = splat[2]
13
+ else
14
+ @url = url
15
+ end
16
+
17
+ @http_method ||= http_method || :get
18
+ @parameter ||= parameter || {}
19
+ end
20
+
21
+ def parameter_string
22
+ case parameter
23
+ when Hash
24
+ parameter.to_param
25
+ else
26
+ parameter
27
+ end
28
+ end
29
+
30
+ def to_siege_url
31
+ if http_method && http_method.to_s.downcase == 'post'
32
+ [url, 'POST', parameter_string]
33
+ else
34
+ url
35
+ end
36
+ end
37
+
38
+ class RequireURL < StandardError
39
+
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module SiegeSiege
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/siege_siege.rb CHANGED
@@ -6,205 +6,15 @@ require 'pp'
6
6
  require 'active_support'
7
7
  require 'active_support/core_ext'
8
8
 
9
+ require 'siege_siege/average_log'
10
+ require 'siege_siege/configuration'
11
+ require 'siege_siege/line_log'
12
+ require 'siege_siege/result'
13
+ require 'siege_siege/runner'
14
+ require 'siege_siege/url'
9
15
 
10
16
  module SiegeSiege
11
- class Runner
12
- attr_accessor :conf, :urls
13
-
14
- def initialize(raw_configuration = {})
15
- @conf = Tempfile.open
16
- @urls = Tempfile.open
17
- @command = nil
18
-
19
- Configuration.new(
20
- {
21
- concurrent: 1,
22
- time: 10,
23
- reps: 1
24
- }.merge!(raw_configuration).merge!(
25
- verbose: true,
26
- rc: @conf.path,
27
- csv: true,
28
- display_id: true,
29
- quiet: false,
30
- file: raw_configuration[:url] ? nil : @urls.path
31
- )
32
- ).tap do |conf|
33
- File.write(@conf, conf.rc)
34
- File.write(@urls, conf.urls)
35
- @command = "siege #{conf.options}"
36
- end
37
- puts @command
38
- end
39
-
40
- def run
41
- _, stdout, stderr = Open3.popen3(@command)
42
- Result.new(stdout.read, stderr.read)
43
- end
44
- end
45
-
46
- class URL < Struct.new(:url, :method, :parameter)
47
- def initialize(*)
48
- super
49
-
50
- raise RequireURL unless url
51
-
52
- self.method ||= :get
53
- self.parameter ||= {}
54
- end
55
-
56
- def to_siege_url
57
- if method && method.to_s.downcase == 'post'
58
- [url, 'POST', parameter.to_param]
59
- else
60
- url
61
- end
62
- end
63
-
64
- class RequireURL < StandardError
65
-
66
- end
67
- end
68
-
69
- class Result
70
- def initialize(raw, raw_result)
71
- @raw = raw
72
- puts @raw_result = raw_result
73
- end
74
-
75
- def raw_log
76
- @stored_logs ||= begin
77
- @raw
78
- .gsub(/\e.+?m/, '')
79
- .gsub('[', '')
80
- .gsub(']', '')
81
- .gsub(' ', '')
82
- .split("\n").map { |line| LineLog.new(*line.split(',')) rescue nil }
83
- .compact
84
- end
85
- end
86
-
87
- def average_log
88
- @stored_average_log ||= raw_log.group_by { |line| line.id }.map { |id, group|
89
- count = group.size
90
- average = (group.inject(0) { |a, log| a + log.secs } / count).round(3)
91
- head = group.first
92
- AverageLog.new(id, head.url, count, average)
93
- }
94
- end
95
- end
96
-
97
- class AverageLog < Struct.new(:id, :url, :count, :secs)
98
-
99
- end
100
-
101
- class LineLog < Struct.new(:week_day, :date, :protocol, :status, :secs, :bytes, :url, :id, :date2)
102
- def initialize(*)
103
- super
104
-
105
- raise InvalidLine unless date2
106
-
107
- self.secs = secs.to_f
108
- self.bytes = bytes.to_i
109
- self.id = id.to_i
110
- end
111
-
112
- class InvalidLine < StandardError
113
-
114
- end
115
- end
116
-
117
- class Configuration
118
- OPTION_MAP = {
119
- verbose: 'v',
120
- concurrent: 'c',
121
- internet: 'i',
122
- time: 't',
123
- reps: 'r',
124
- log: 'l',
125
- mark: 'm',
126
- delay: 'd',
127
- header: 'H',
128
- user_agent: 'A',
129
- content_type: 'T',
130
- rc: 'R',
131
- file: 'f',
132
- url: ''
133
- }
134
-
135
- RC_MAP = {
136
- verbose: true,
137
- quiet: false,
138
- gmethod: 'HEAD',
139
- csv: false,
140
- timestamp: true,
141
- fullurl: true,
142
- display_id: true,
143
- limit: 255,
144
- show_logfile: true,
145
- logging: true,
146
- logfile: nil,
147
- protocol: 'HTTP/1.1',
148
- chunked: true,
149
- cache: false,
150
- timeout: 2000,
151
- expire_session: false,
152
- cookies: false,
153
- failures: 1024,
154
- benchmark: true,
155
- accept_encoding: 'gzip',
156
- url_escaping: true,
157
- spinner: false,
158
- login: nil,
159
- login_url: nil,
160
- ftp_login: nil,
161
- unique: true,
162
- ssl_cert: nil,
163
- ssl_key: nil,
164
- ssl_timeout: nil,
165
- ssl_ciphers: nil,
166
- proxy_host: nil,
167
- proxy_port: nil,
168
- proxy_login: nil,
169
- follow_location: true,
170
- zero_data_ok: true
171
- }
172
-
173
- def initialize(configuration)
174
- @configuration = configuration
175
- end
176
-
177
- def urls
178
- Array(@configuration[:urls]).map { |url|
179
- (URL === url ? url : URL.new(url)).to_siege_url
180
- }.join("\n")
181
- end
182
-
183
- def rc
184
- RC_MAP.inject('') do |a, (key, value)|
185
- inserting = @configuration[key] || value
186
- if inserting
187
- a << "#{key.to_s.gsub('_', '-')} = #{inserting}\n"
188
- else
189
- a
190
- end
191
- end
192
- end
193
-
194
- def options
195
- OPTION_MAP.inject([]) { |a, (key, value)|
196
- inserting = @configuration[key]
197
- case
198
- when TrueClass === inserting, FalseClass === inserting
199
- a << "-#{value}"
200
- when inserting && key == :time
201
- a << "-#{value} #{@configuration[key]}s"
202
- when inserting
203
- a << "-#{value} #{@configuration[key]}"
204
- else
205
- a
206
- end
207
- }.join(' ')
208
- end
17
+ def self.run(*args)
18
+ Runner.new(*args).run
209
19
  end
210
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siege_siege
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmmpa
@@ -79,10 +79,13 @@ files:
79
79
  - LICENSE.txt
80
80
  - README.md
81
81
  - Rakefile
82
- - bin/console
83
- - bin/setup
84
82
  - lib/siege_siege.rb
85
- - lib/siege_siege/siege.rb
83
+ - lib/siege_siege/average_log.rb
84
+ - lib/siege_siege/configuration.rb
85
+ - lib/siege_siege/line_log.rb
86
+ - lib/siege_siege/result.rb
87
+ - lib/siege_siege/runner.rb
88
+ - lib/siege_siege/url.rb
86
89
  - lib/siege_siege/version.rb
87
90
  - server.rb
88
91
  - siege_siege.gemspec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "siege_siege"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,51 +0,0 @@
1
- module SiegeSiege
2
- class Siege
3
- CONFIGURATIONS = %w(
4
- verbose
5
- quiet
6
- gmethod
7
- csv
8
- timestamp
9
- fullurl
10
- display-id
11
- limit
12
- show-logfile
13
- logging
14
- logfile
15
- protocol
16
- chunked
17
- cache
18
- connection
19
- concurrent
20
- time
21
- reps
22
- file
23
- url
24
- delay
25
- timeout
26
- expire-session
27
- cookies
28
- failures
29
- internet
30
- benchmark
31
- user-agent
32
- accept-encoding
33
- url-escaping
34
- spinner
35
- login
36
- login-url
37
- ftp-login
38
- unique
39
- ssl-cert
40
- ssl-key
41
- ssl-timeout
42
- ssl-ciphers
43
- proxy-host
44
- proxy-port
45
- proxy-login
46
- follow-location
47
- zero-data-ok
48
- )
49
-
50
- end
51
- end