Recording HTTP Traffic Bandwidth

600 views
Skip to first unread message

cle...@iparadigms.com

unread,
Jun 6, 2014, 12:42:36 AM6/6/14
to golan...@googlegroups.com
I would like to be able to measure the bandwidth my program is consuming. The go http library is making this a little difficult, as it sets the Content-Length field in the http.Response object to -1 when the body is gzipped.

I ~could~ read out the body from the io.Reader and take the length of that slice, but that would give the length of the uncompressed content, which isn't representative of the traffic on the wire. Also, that would require extra juggling to route the byte slice from traffic measurement to wherever the content is processed.

Is there any clean way to accomplish this?

Steven Hartland

unread,
Jun 6, 2014, 8:52:04 AM6/6/14
to cle...@iparadigms.com, golan...@googlegroups.com
Read the Content-Length Header directly?

Regards
Steve
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christopher Lenart

unread,
Jun 6, 2014, 9:50:48 AM6/6/14
to golan...@googlegroups.com
The Go transport library erases the content length header when the content is gzipped.

http://golang.org/src/pkg/net/http/transport.go

Line 720

Matt Harden

unread,
Jun 6, 2014, 8:04:12 PM6/6/14
to Christopher Lenart, golang-nuts
You could create a Transport and set DisableCompression to true, then request compression on your own. This would bypass the automatic decompression and you will have to gunzip the data yourself.


Christopher Lenart

unread,
Jun 7, 2014, 3:19:44 PM6/7/14
to golan...@googlegroups.com, cle...@iparadigms.com
Thanks for the suggestion! I'm sure that would be one way to do it.


Works great, and also records the HTTP header bytes!

rjeczalik

unread,
Jun 7, 2014, 4:22:43 PM6/7/14
to cle...@iparadigms.com, golan...@googlegroups.com
You could also wrap your net.Listener and count all the bytes read / written for every net.Conn it creates - I used something similar to this in order to record all the transmission data in [1].



Reply all
Reply to author
Forward
0 new messages