Class: Client

Inherits:
Application show all
Defined in:
lib/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/client.rb', line 6

def initialize
  # Setting IP of server
  print "Input IP address of servrer: ".green
  @ip = $stdin.gets.chomp
  @ip = 'localhost' if blank? @ip

  # Setting PORT of server
  print "Input Port of servrer: ".green
  @port = $stdin.gets.chomp
  @port = '5899' if blank? @port

  # Setting iterations count
  print "Input iterations count: ".green
  @each_count = $stdin.gets.chomp.to_i
  @each_count = 1 if @each_count.zero?

  # Setting eterations count
  print "Input threads count: ".green
  @threads_count = $stdin.gets.chomp.to_i
  @threads_count = 1 if @threads_count.zero?

  # Setting timeout
  print "Input timeout: ".green
  @http_timeout = $stdin.gets.chomp.to_f
  @http_timeout = 10 if @http_timeout.zero?

  # Building link
  @uri = "http://#{@ip}:#{@port}"

  # Variables
  @req_times = []
  @threads = []
  @package_loss = 0
  @package_rec = 0
end

Instance Method Details

#send_resultsObject



222
223
224
# File 'lib/client.rb', line 222

def send_results
  Curl.post(@uri + '/exit', { results: @req_times.join(' '), package_loss: @package_loss, package_rec: @package_rec })
end

#testObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/client.rb', line 42

def test
  @threads_count.times do
    @threads << Thread.new do
      @each_count.times do
        # Raw root GET
        begin
          t = Time.now
          response = Curl.get(@uri) do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.status == '200 OK'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Root POST with correct params
        begin
          t = Time.now
          response = Curl.post(@uri, { name: 'Oleg', github_nickname: 'sorefull' }) do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.status == '202 Accepted'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Root POST with wrong params
        begin
          t = Time.now
          response = Curl.post(@uri, { name: '', github_nickname: '' }) do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.status == '401 Unauthorized'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Lorem GET with body check
        begin
          t = Time.now
          response = Curl.get(@uri + '/lorem') do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.body == LoremIpsum.lorem
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Lorem GET with params
        begin
          t = Time.now
          response = Curl.get(@uri + '/lorem', { length: 10 }) do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.body == LoremIpsum.lorem[0..10]
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Cookie GET with correct cookies
        begin
        t = Time.now
          response = Curl.get(@uri + '/cookie') do |http|
            http.headers['Cookie'] = 'private=asdfg; public=gijj'
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.status == '200 OK'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # Cookie GET with wrong cookies
        begin
          t = Time.now
          response = Curl.get(@uri + '/cookie') do |http|
            http.headers['Cookie'] = 'private=wrong; public=wrong'
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          if response.status == '401 Unauthorized'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # File GET
        begin
          file_name = '/file.txt'
          t = Time.now
          response = Curl.get(@uri + file_name) do |http|
            http.timeout = @http_timeout
          end
          @req_times << Time.now - t
          file = File.open("#{__dir__}/../files#{file_name}", 'r')
          if response.body == file.read && response.status == '200 OK'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end

        # File POST
        begin
          file_name = 'image.jpeg'
          response = Curl::Easy.new(@uri + '/file') do |http|
            http.timeout = @http_timeout
          end
          response.multipart_form_post = true
          t = Time.now
          response.http_post(Curl::PostField.file('uploaded_image', "#{__dir__}/../files/#{file_name}"))
          @req_times << Time.now - t
          if response.status == '200 OK'
            print '.'.green
          else
            print '!'.red
          end
          @package_rec += 1
        rescue Curl::Err::TimeoutError
          print '#'.yellow
          @package_loss += 1
        end
      end
    end
  end

  # Joining threads
  @threads.each {|thread| thread.join}
end