Commit 37112a72 authored by akwizgran's avatar akwizgran

Test for a fixed duration rather than fixed amount of traffic.

parent 344cfac8
......@@ -59,7 +59,7 @@ public class MainViewModel extends AndroidViewModel {
private static final int BUFFER_SIZE = 4096;
private static final int MS_PER_UPDATE = 250;
private static final int BYTES_TO_TRANSFER = 10_000_000;
private static final int MS_PER_TRANSFER = 5_000;
private static final int TRANSFERS_PER_CONDITION = 10;
private static final String TAG = "Interferometer";
......@@ -121,7 +121,7 @@ public class MainViewModel extends AndroidViewModel {
Future<?> discoveryTask = startDiscovery(adapter);
try {
testPhase.postValue(DISCOVERY);
Log.i(TAG, "Testing throughput with Bluetooth enabled");
Log.i(TAG, "Testing throughput with Bluetooth discovery");
List<Long> downloadDiscovery = new ArrayList<>();
List<Long> uploadDiscovery = new ArrayList<>();
if (!testThroughput(hostPort, downloadDiscovery, uploadDiscovery)) return;
......@@ -265,24 +265,26 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Connected, requesting upload");
OutputStream out = s.getOutputStream();
out.write(CONNECTION_TYPE_UPLOAD);
out.flush();
byte[] buf = new byte[BUFFER_SIZE];
new Random().nextBytes(buf);
long start = System.nanoTime();
long now = start;
long end = start + MS_PER_TRANSFER * 1_000_000L;
long lastUpdate = 0;
int bytesSinceLastUpdate = 0;
int bytesTotal = 0;
while (bytesTotal < BYTES_TO_TRANSFER) {
int len = Math.min(BUFFER_SIZE, BYTES_TO_TRANSFER - bytesTotal);
out.write(buf, 0, len);
while (now < end) {
out.write(buf);
if (Thread.currentThread().isInterrupted()) {
Log.i(TAG, "Upload to " + hostPort + " interrupted");
return -1;
}
bytesSinceLastUpdate += len;
bytesTotal += len;
long now = System.nanoTime();
bytesSinceLastUpdate += BUFFER_SIZE;
bytesTotal += BUFFER_SIZE;
now = System.nanoTime();
long msSinceLastUpdate = (now - lastUpdate) / 1_000_000;
if (msSinceLastUpdate >= MS_PER_UPDATE) {
long bytesPerSecond = bytesSinceLastUpdate * 1000L / msSinceLastUpdate;
......@@ -292,7 +294,6 @@ public class MainViewModel extends AndroidViewModel {
}
}
Log.i(TAG, "Upload to " + hostPort + " finished");
long now = System.nanoTime();
long msSinceStart = (now - start) / 1_000_000;
return bytesTotal * 1000L / msSinceStart;
} catch (IOException e) {
......@@ -315,17 +316,19 @@ public class MainViewModel extends AndroidViewModel {
Log.i(TAG, "Connected, requesting download");
OutputStream out = s.getOutputStream();
out.write(CONNECTION_TYPE_DOWNLOAD);
out.flush();
InputStream in = s.getInputStream();
byte[] buf = new byte[BUFFER_SIZE];
long start = System.nanoTime();
long now = start;
long end = start + MS_PER_TRANSFER * 1_000_000L;
long lastUpdate = 0;
int bytesSinceLastUpdate = 0;
int bytesTotal = 0;
while (bytesTotal < BYTES_TO_TRANSFER) {
int len = Math.min(BUFFER_SIZE, BYTES_TO_TRANSFER - bytesTotal);
int read = in.read(buf, 0, len);
while (now < end) {
int read = in.read(buf);
if (read == -1) {
Log.w(TAG, "Download from " + hostPort + " finished early");
return -1;
......@@ -336,7 +339,7 @@ public class MainViewModel extends AndroidViewModel {
}
bytesSinceLastUpdate += read;
bytesTotal += read;
long now = System.nanoTime();
now = System.nanoTime();
long msSinceLastUpdate = (now - lastUpdate) / 1_000_000;
if (msSinceLastUpdate >= MS_PER_UPDATE) {
long bytesPerSecond = bytesSinceLastUpdate * 1000L / msSinceLastUpdate;
......@@ -346,7 +349,7 @@ public class MainViewModel extends AndroidViewModel {
}
}
Log.i(TAG, "Download from " + hostPort + " finished");
long now = System.nanoTime();
now = System.nanoTime();
long msSinceStart = (now - start) / 1_000_000;
return bytesTotal * 1000L / msSinceStart;
} catch (IOException e) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment