Search code examples
javaformulabandwidth

Formula to calculate bandwidth used in Java


I've looked around a bit and cannot seem to find a good formula to fit my need. I have the following values for a single connection to an application

long elapsedTimeSeconds = (System.currentTimeMillis() - client.getCreationTime()) / 1000;
long totalBytesRead = conn.getReadBytes();
long totalBytesWritten = conn.getWrittenBytes();

I would like to calculate the amount of bandwidth used (read and write) for the elapsed period in Kbps.

Is this the correct formula for calculating each separately?

long readBandwidthUsed = (totalBytesRead / 1024) / elapsedTimeSeconds;

Solution

  • I ended up using this formula:

    long bandwidthUsed = (totalBytes / 1024) / elapsedTimeSeconds;