Class: TTCP::TTCP

Inherits:
Object
  • Object
show all
Defined in:
lib/ttcp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TTCP

Create a TTCP test program instance. All configuration is done via the options hash passed in here.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ttcp.rb', line 41

def initialize(options = {})
  @options = TTCP.default_options.merge options

  if @options[:udp]
    # enforce buffer length to be more than udp sentinel size.
    @options[:length] = [@options[:length], 5].max
  end

  @bytes_received = 0

  # by default, use the stdout and stderr.
  @stdout = $stdout
  @stderr = $stderr

end

Instance Attribute Details

#bytes_receivedObject (readonly)

Returns the value of attribute bytes_received.



30
31
32
# File 'lib/ttcp.rb', line 30

def bytes_received
  @bytes_received
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/ttcp.rb', line 29

def options
  @options
end

#stderrObject

Returns the value of attribute stderr.



31
32
33
# File 'lib/ttcp.rb', line 31

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



31
32
33
# File 'lib/ttcp.rb', line 31

def stdout
  @stdout
end

Class Method Details

.default_optionsObject



33
34
35
# File 'lib/ttcp.rb', line 33

def self.default_options
  DEFAULT_OPTIONS
end

Instance Method Details

#closeObject

close any sockets



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ttcp.rb', line 158

def close
  unless @socket.nil?
    begin
      @socket.shutdown Socket::SHUT_RDWR
    rescue
      # ignore any errors closing the socket
    ensure
      @socket = nil
    end
  end
end

#cpu_durationObject



151
152
153
# File 'lib/ttcp.rb', line 151

def cpu_duration
  @finish_cpu_time - @start_cpu_time if @finish_cpu_time
end

#durationObject



147
148
149
# File 'lib/ttcp.rb', line 147

def duration
  @finish_time - @start_time if @finish_time
end

#runObject

Run the TTCP test program according to the options specified with the .new call



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
# File 'lib/ttcp.rb', line 60

def run
  begin
    num_bytes = 0
    num_calls = 0

    if @options[:transmit]
      message "buflen=%d, nbuf=%d, remote port=%d" % [@options[:length], @options[:num_buffers], @options[:port]]

      buf = source_buffer

      @start_time = Time.now
      @start_cpu_time = get_cpu_time

      socket.write "ttcp" if @options[:udp]
      @options[:num_buffers].times do
        socket.write buf
        num_bytes += buf.length
        num_calls += 1
      end
      socket.write "ttcp" if @options[:udp]

    elsif @options[:receive]
      message "buflen=%d, nbuf=%d, local port=%d" % [@options[:length], @options[:num_buffers], @options[:port]]

      receiving = true
      sentinel_count = 0

      if @options[:tcp]
        receiving_socket = socket.accept
        remote_address =
            if receiving_socket.respond_to? :remote_address
              "#{receiving_socket.remote_address.ip_address}:#{receiving_socket.remote_address.ip_port}"
            elsif receiving_socket.respond_to? :peeraddr # this is what JRuby 1.6.5 has instead
              "#{receiving_socket.peeraddr[2]}:#{receiving_socket.peeraddr[1]}"
            else
              "<Unknown>"
            end
        message("accept from #{remote_address}")
      else
        receiving_socket = socket
      end

      @start_time = Time.now
      @start_cpu_time = get_cpu_time

      while receiving

        buf = receiving_socket.recv @options[:length]
        num_bytes += buf.length unless buf.nil?
        num_calls += 1

        if buf.nil? || (@options[:tcp] && buf.length == 0)
          receiving = false
        else
          if @options[:udp]
            if buf.length <= 4
              sentinel_count += 1
              if sentinel_count >= 2
                receiving = false
              end
            else
              sink buf
              touch buf if @options[:touch]
              @bytes_received += buf.length
            end
          end
        end
      end

      if @options[:tcp]
        receiving_socket.close unless receiving_socket.closed?
        receiving_socket = nil
      end

    end

  ensure
    @finish_time = Time.now
    @finish_cpu_time = get_cpu_time
    close
  end

  message("%d bytes in %.3f real seconds = %s/sec +++" % [num_bytes, duration, format_rate(num_bytes, duration)])

end

#stdout_to_nullObject

set the stdout to point to nothing



174
175
176
177
# File 'lib/ttcp.rb', line 174

def stdout_to_null
  @stdout = File.open("/dev/null", "w")
  @stderr = File.open("/dev/null", "w")
end