views:

33

answers:

1

I need to meter the number of bytes being used by a java application from within java (i.e. I don't have any special access to the JVM other then not having a security manager to deal with) but I need all the bytes sent/received including protocol headers and the like. Anyone have good ideas on how to do this?

The obvious move is to create a new URLConnectionHandler and insert metering into the IO streams. This, however, does not provide me with the header sizes of HTTP/HTTPS messages sent to/from a Java application.

+1  A: 

If you want the exact number of bytes on the wire you need help of the OS and in a very platform specific way.

Network traffic is a set of Babushka dolls.

The URLConnection adds a header, the TCP stack adds a header to each package, ethernet stack adds another header. Then there are the acknowledge, retransmits, etc occurring behind the back of your program. Then there are dns requests and routing packets, ...

You'll need special rules to separate the traffic of your app from the rest of the traffic.

All-in-all pretty hopeless.

For practical purposes I would recommend you setup your application on a machine and use wireshark or similar to capture the traffic. Inside you measure the payload as you describe, then use the external measurements to measure the overhead.

Then estimate the % overhead on typical use and add that to the payload measurement in your app.

It will be close enough that noone will notice the difference.

Peter Tillemans