user967710
user967710

Reputation: 2007

Java: measure network bandwidth for a specific code segment

I want to add bandwidth measurements to my servers (written in pure java). I am thinking of an API that look like

MeasureBandwidthSignletone.getInstance().startMeasuring();
......
.....//here I may connect to a db or using http (HttpUrlConnection)...
.....
MeasureBandwidthSignletone.getInstance().endMeasuring();

Problem is that I have many different kinds of code that access the network (jdbc, HttpUrlConnection, FTP, etc...) I was wondering if I can somehow attach a threadlocal monitor to sockets, which will allow me to know how many bytes were uploaded or download.

I know one way would be using ASM / AspectJ to change the byte code - but is there any simpler way to plug in to the java socket API?

Thank you

Upvotes: 0

Views: 818

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

What about setting a custom made SocketFactoryImpl? Can that work?

That could also work for a specific version of the JVM. If you know which version you are targeting you could create a modified version of this class.

The reason this won't work for a generic JVM is this class uses internal APIs which can be different between versions which is why byte code injection is more likely to work for a broad range of JVMs.

Upvotes: 1

Related Questions