# Webrick Servlet: 'chomping' ? Is this slowing it down?

**URL:** <https://www.ruby-forum.com/t/webrick-servlet-chomping-is-this-slowing-it-down/147155>\
**Category:** Ruby\
**Created:** [August 28, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/webrick-servlet-chomping-is-this-slowing-it-down/147155 "2008-08-28T21:40:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [August 28, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/webrick-servlet-chomping-is-this-slowing-it-down/147155/1 "2008-08-28T21:40:21Z")

</div>

Hi,

I’m not using Rails: just a standalone ‘Webrick’ Server. I have a very  
simple Servlet which is serving text files: it works, but I notice that  
it seeming to be two (maybe related?) things I need to fix:

1. It is ‘chomping’ the text files: removing any ‘\r’, leaving ‘\n’  
chars at the end of my response string.

2. It runs slower than Tomcat (fair enough), but _really_ slow (~4-5  
times slower) when I access over a VPN (or a different n/w): the  
VPN/other n/w _seems_ to be the link here. When running on a local n/w  
(or directly on the box itself), there is no noticeable difference in  
performance: but there a distinctive 5-10 second lag on the browser in  
the case of VPN/different n/w.

So any hints on optimizing the Ruby Servlet greatly appreciated - you  
can see from the Java version that I’m really not interested in doing  
_anything_ to the data - I just want to delivery as fast as possible -  
in tact.

The Webrick server is running on Ruby 1.8.x on a Windows 2000 box.

Anyway , here’s the servlets (stripped down to bare function).

–Ruby Servlet  
…  
def do\_GET(request, response)  
r=“”; #probably not necessary…but habits from static languages  
carry-over 😉

file\_data=

f=File.open(file\_data,“r”); # load as pure bytes like the java  
version?how?  
r \<\< f.read  
f.close;

response.body=r;  
response.status=200;  
response[‘Content-Type’] =“text/plain”  
end  
…  
–End of Ruby Servlet

–Equivalent Java Servlet  
…doGet(HttpServletRequest request, HttpServletResponse response)…  
out=response.getOutputStream()  
f=  
…  
int len=bytes;  
InputStream in = new FileInputStream(f);  
byte[] buf = new byte[len];  
while ((len = in.read(buf)) \> 0)  
{  
out.write(buf,0,len);  
}  
out.flush();  
out.close();  
in.close();  
…  
–End of Java Servlet

Cheers

John

And also just for proof , here’s the timing from 2 ‘curl’ invocations  
running over a VPN to get at the server.

==  
curl “[http://server:8080/tomcat](http://server:8080/tomcat)” -o tomcat.out  
% Total % Received % Xferd Average Speed Time Time Time  
Current  
Dload Upload Total Spent Left  
Speed  
100 27594 100 27594 0 0 10336 0 0:00:02 0:00:02 --:–:–  
10652

# curl “[http://server:2000/webrick](http://server:2000/webrick)” -o webrick.out % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 27394 100 27394 0 0 2876 0 0:00:09 0:00:09 --:–:– 6173

The file in question was exactly 200 lines, and the correct size on disk  
is ‘27594’ NOT ‘27394’: ‘od -c’ shows that the Ruby Servlet is  
‘chomping’ on an extra ‘\r’ character…
