diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-01-14 02:44:53 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-01-14 02:44:53 +0000 |
commit | 7db24a6269c85313ccf57d601ccef684f2cdb4fa (patch) | |
tree | 19133e22a407f9d589d53b49305e22ece6f34ac7 /lib | |
parent | 982e9e623501dc1c6655ca37fef505c3ec7af8d1 (diff) | |
download | ruby-7db24a6269c85313ccf57d601ccef684f2cdb4fa.tar.gz |
net/http: use writev for HTTP chunked request bodies
This reduces both user and system CPU time for large
uploads with dynamically-generated request bodies.
user system total real
before: 0.393334 1.580000 1.973334 ( 1.971066)
after: 0.223334 0.976666 1.200000 ( 1.198514)
------
require 'socket'
require 'net/http'
require 'benchmark'
nr = 1024 * 1024 * 1024
s = TCPServer.new('127.0.0.1', 0)
addr = s.addr
at_exit { Process.waitall }
fork do
c = s.accept
# not exactly accurate but fast
IO.copy_stream(c, '/dev/null', nr + 500000)
begin
buf = c.readpartial(16384)
tmp = ''
until buf.end_with?(-"0\r\n\r\n")
buf << c.readpartial(16384, tmp)
end
rescue EOFError
end
c.write "HTTP/1.1 201 Created\r\nConnection:close\r\n\r\n"
c.close
end
r, w = IO.pipe
fork do
r.close
IO.copy_stream('/dev/zero', w, nr)
w.close
end
w.close
Net::HTTP.start(addr[3], addr[1]) do |http|
put = Net::HTTP::Put.new('/dev0/foo')
put['Content-Type'] = 'application/content-type'
put['Transfer-Encoding'] = 'chunked'
put.body_stream = r
puts(Benchmark.measure { http.request(put) })
end
------
* lib/net/http/generic_request.rb (write): use multi-arg write
* lib/net/protocol.rb (write): support multi-arg
(write0): ditto
[ruby-core:84845] [Feature #14339]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61812 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/net/http/generic_request.rb | 5 | ||||
-rw-r--r-- | lib/net/protocol.rb | 10 |
2 files changed, 7 insertions, 8 deletions
diff --git a/lib/net/http/generic_request.rb b/lib/net/http/generic_request.rb index 526cc333fc..a78ec16431 100644 --- a/lib/net/http/generic_request.rb +++ b/lib/net/http/generic_request.rb @@ -168,9 +168,8 @@ class Net::HTTPGenericRequest def write(buf) # avoid memcpy() of buf, buf can huge and eat memory bandwidth - @sock.write("#{buf.bytesize.to_s(16)}\r\n") - rv = @sock.write(buf) - @sock.write("\r\n") + rv = buf.bytesize + @sock.write("#{rv.to_s(16)}\r\n", buf, "\r\n") rv end diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 380d31ed5d..e6994477b8 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -209,9 +209,9 @@ module Net # :nodoc: public - def write(str) + def write(*strs) writing { - write0 str + write0 *strs } end @@ -235,9 +235,9 @@ module Net # :nodoc: bytes end - def write0(str) - @debug_output << str.dump if @debug_output - len = @io.write(str) + def write0(*strs) + @debug_output << strs.map(&:dump).join if @debug_output + len = @io.write(*strs) @written_bytes += len len end |