Skip to content

Commit

Permalink
add bandwidth test
Browse files Browse the repository at this point in the history
  • Loading branch information
arons committed Jun 15, 2024
1 parent 18f29a1 commit 8db28c6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 17 deletions.
9 changes: 4 additions & 5 deletions src/main/ch/arons/jbench/pg/PG.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,34 @@ public static void main(String[] args) {
logCurrentSystem();



System.out.println("Start: " + new Date());
long startMs = System.currentTimeMillis();
try {
PGTest pgtest = new PGTest(url, user, pw);
pgtest.setAdditional(additional);

if ("params".equalsIgnoreCase(command)) {
pgtest.params();
return;
}

if ("prepare".equalsIgnoreCase(command)) {
pgtest.prepare();
return;
}

if ("clean".equalsIgnoreCase(command)) {
pgtest.clean();
return;
}

if ("test".equalsIgnoreCase(command)) {
pgtest.run();
return;
}

} catch (SQLException e) {
e.printStackTrace();
}

long endMs = System.currentTimeMillis();
System.out.println("End: " + new Date());
System.out.printf("Duration s: %s", (endMs - startMs) / 1000);
}
}
4 changes: 3 additions & 1 deletion src/main/ch/arons/jbench/pg/PGTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ch.arons.jbench.pg.data.DataClean;
import ch.arons.jbench.pg.data.DataCreate;
import ch.arons.jbench.pg.test.BlobBandwidth;
import ch.arons.jbench.pg.test.DBTest;
import ch.arons.jbench.pg.test.OpenLatency;
import ch.arons.jbench.pg.test.PGParameterCheck;
Expand Down Expand Up @@ -53,7 +54,8 @@ public void run() {
new PrintInfo(db),
new OpenLatency(db),
new Statistics(db),
new TrivialQuery(db)
new TrivialQuery(db),
new BlobBandwidth(db)
};
for (DBTest t : tests) {
t.test();
Expand Down
108 changes: 108 additions & 0 deletions src/main/ch/arons/jbench/pg/test/BlobBandwidth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package ch.arons.jbench.pg.test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import javax.management.timer.Timer;

import ch.arons.jbench.pg.DB;
import ch.arons.jbench.utils.BlobStream;

/**
* test upload, internal copy and download bandwidth.
*/
public class BlobBandwidth extends SingleConDBTest {

private static final int size = 20 * 1024 * 1024; //20 MG

public BlobBandwidth(DB db) {
super(db);
}

@Override
protected void test(Connection c) throws SQLException {
System.out.printf("Test bynary data bandwidth:\n");

String id = UUID.randomUUID().toString();
String id2 = UUID.randomUUID().toString();


/*
* upload
*/
try (PreparedStatement ps = c.prepareStatement("insert into tbbm_lob (id, doc) values (?, ?)");) {
long start = System.currentTimeMillis();
ps.setString(1, id2);
ps.setNull(2, Types.OTHER);
ps.executeUpdate();
long duration = System.currentTimeMillis() - start;
System.out.printf(" NULL BLOB insert: %d ms\n", duration);

// create blob
start = System.currentTimeMillis();
ps.setString(1, id);
ps.setBinaryStream(2, new BlobStream(size));
ps.executeUpdate();
duration = System.currentTimeMillis() - start;
float mbps = (size / 1024f / 1024f) / ((float) duration / Timer.ONE_SECOND);
System.out.printf(" BLOB upload: %f MB/s\n", mbps);

}

/*
* in db copy
*/
try (PreparedStatement ps = c.prepareStatement("update tbbm_lob set doc = (select doc from tbbm_lob where id=?) where id=?");) {
long start = System.currentTimeMillis();
ps.setString(1, id);
ps.setString(2, id2);
ps.executeUpdate();
long duration = System.currentTimeMillis() - start;
float mbps = (size / 1024f / 1024f) / ((float) duration / Timer.ONE_SECOND);
System.out.printf(" BLOB in-DB copy: %f MB/s\n", mbps);
}

/*
* test download
*/
try (PreparedStatement ps = c.prepareStatement("select doc from tbbm_lob where id = ?");) {
long start = System.currentTimeMillis();
ps.setString(1, id2);
ResultSet rs = ps.executeQuery();
rs.next();
long bytes = 0;
try (InputStream is = rs.getBinaryStream(1)) {
byte[] buf = new byte[4096];
int len = 0;
while ((len = is.read(buf)) != -1) {
bytes += len;
}
} catch (IOException e) {
e.printStackTrace();
}
rs.close();
rs = null;
long duration = System.currentTimeMillis() - start;
float mbps = (bytes / 1024f / 1024f) / ((float) duration / Timer.ONE_SECOND);
System.out.printf(" BLOB download: %f MB/s\n", mbps);
}

/*
* clean db
*/
try (PreparedStatement ps = c.prepareStatement("delete from tbbm_lob where id = ?");) {
ps.setString(1, id);
ps.executeUpdate();
ps.setString(1, id2);
ps.executeUpdate();
}

}

}
5 changes: 4 additions & 1 deletion src/main/ch/arons/jbench/pg/test/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public Statistics(DB db) {

@Override
public void test(Connection conn) throws SQLException {
System.out.printf("Perform statistics\n");
System.out.printf("Perform statistics");
long startMs = System.currentTimeMillis();
DataRun.executeQuery(conn, "analyze jbench.TBBM_PARENT");
DataRun.executeQuery(conn, "analyze jbench.TBBM_CHILD");
long endMs = System.currentTimeMillis();
System.out.printf(", ms: %d\n", (endMs - startMs));
}

}
26 changes: 16 additions & 10 deletions src/main/ch/arons/jbench/pg/test/TrivialQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
public class TrivialQuery extends SingleConDBTest {


private static final int runtTimeMs = 3000;
private static final int minimalQueries = 1000;

public TrivialQuery(DB db) {
super(db);
}
Expand All @@ -26,6 +30,7 @@ protected void test(Connection c) throws SQLException {
try (PreparedStatement ps = c.prepareStatement(" select 1 ");) {
ResultSet rs = null;

System.out.printf("Trivial SELECT test with fetch: ");
long totStart = System.currentTimeMillis();
int count = 0;
while (true) {
Expand All @@ -36,45 +41,46 @@ protected void test(Connection c) throws SQLException {
if (rs.next()) {
rs.getInt(1);
}
if (count++ % 1000 == 0) {
if (count++ % minimalQueries == 0) {
long duration = System.currentTimeMillis() - totStart;
if (duration > 3000) {
if (duration > runtTimeMs) {
break;
}
}
}
long totDuration = System.currentTimeMillis() - totStart;


System.out.printf("Trivial SELECT test with fetch");

float qps = (float) count / ((float) totDuration / Timer.ONE_SECOND);
System.out.printf("Trivial SELECT: %f qps\n", qps);
System.out.printf("%f qps\n", qps);
float lat = (float) totDuration / count;
System.out.printf("=> min. query latency: %f ms (should be dominated only by network latency)\n", lat);
System.out.printf("=> min. query latency: %f ms (should be dominated only by network latency. Queries: %d)\n", lat, count);



System.out.printf("Trivial SELECT test NO fetch: ");
totStart = System.currentTimeMillis();
count = 0;
while (true) {
if (rs != null) {
rs.close();
}
rs = ps.executeQuery();
if (count++ % 1000 == 0) {
if (count++ % minimalQueries == 0) {
long duration = System.currentTimeMillis() - totStart;
if (duration > 3000) {
if (duration > runtTimeMs) {
break;
}
}
}
totDuration = System.currentTimeMillis() - totStart;

System.out.printf("Trivial SELECT test NO fetch");

qps = (float) count / ((float) totDuration / Timer.ONE_SECOND);
System.out.printf("Trivial SELECT: %f qps\n", qps);
System.out.printf("%f qps\n", qps);
lat = (float) totDuration / count;
System.out.printf("=> min. query latency: %f ms (should be dominated only by network latency)\n", lat);
System.out.printf("=> min. query latency: %f ms (should be dominated only by network latency. Queries: %d)\n", lat, count);


}
Expand Down
57 changes: 57 additions & 0 deletions src/main/ch/arons/jbench/utils/BlobStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.arons.jbench.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

/**
* Random blob data stream.
*/
public class BlobStream extends InputStream {
private Random rng = new Random();
private int avail;

public BlobStream(int length) {
this.avail = length;
}

@Override
public int read() throws IOException {
if (avail > 0) {
avail--;
return rng.nextInt(256);
} else {
return -1;
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (len > avail) {
len = avail;
}
if (len <= 0) {
return -1;
}
avail -= len;
if ((b.length == len) && (off == 0)) {
rng.nextBytes(b);
} else {
byte[] tmp = new byte[len];
rng.nextBytes(tmp);
System.arraycopy(tmp, 0, b, off, len);
}
return len;
}

@Override
public int available() throws IOException {
return avail;
}

@Override
public boolean markSupported() {
return false;
}

}

0 comments on commit 8db28c6

Please sign in to comment.