Browse Source

fixing speedtest for multithreading

pull/2/head
heck 5 years ago
parent
commit
25f32c536e
  1. 156
      test/java/foundation/pEp/jniadapter/test/speedtest/SpeedTest.java

156
test/java/foundation/pEp/jniadapter/test/speedtest/SpeedTest.java

@ -6,6 +6,7 @@ import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Vector;
import java.util.Scanner;
import foundation.pEp.jniadapter.*;
public class SpeedTest {
@ -14,15 +15,14 @@ public class SpeedTest {
private static Identity me = new Identity(true);
private static Identity you = new Identity();
protected static void decodingTest(long n, String testDataEnc) {
protected static void decodingTest(Engine eng, long n, String testDataEnc) {
for (long i = 0; i < n; ++i) {
try {
Message[] msgs = codec.decode(testDataEnc);
Vector<String> keys = new Vector<String>();
Engine.decrypt_message_Return ret = pEp.decrypt_message(msgs[0], keys, 0);
Engine.decrypt_message_Return ret = eng.decrypt_message(msgs[0], keys, 0);
String txt = ret.dst.getLongmsg();
}
catch (ParseException ex) {
} catch (ParseException ex) {
System.err.println("error: parsing test data");
System.exit(3);
}
@ -32,19 +32,20 @@ public class SpeedTest {
protected class DecodingThread extends Thread {
private long _n;
private String _testDataEnc;
private Engine _localpEp;
public DecodingThread(long n, String testDataEnc)
{
public DecodingThread(long n, String testDataEnc) {
_n = n;
_testDataEnc = testDataEnc;
_localpEp = new Engine();
}
public void run() {
decodingTest(_n, _testDataEnc);
decodingTest(_localpEp, _n, _testDataEnc);
}
}
private static Message encrypt(String data) {
private static Message encrypt(Engine eng, String data) {
Message m = new Message();
m.setDir(Message.Direction.Outgoing);
m.setFrom(me);
@ -52,12 +53,12 @@ public class SpeedTest {
to.add(you);
m.setTo(to);
m.setLongmsg(data);
return pEp.encrypt_message(m, null, Message.EncFormat.Inline);
return eng.encrypt_message(m, null, Message.EncFormat.Inline);
}
protected static void encodingTest(long n, String testData) {
protected static void encodingTest(Engine eng, long n, String testData) {
for (long i = 0; i < n; ++i) {
Message enc = encrypt(testData);
Message enc = encrypt(eng, testData);
String txt = codec.encode(enc, null);
}
}
@ -65,21 +66,22 @@ public class SpeedTest {
protected class EncodingThread extends Thread {
private long _n;
private String _testData;
private Engine _localpEp;
public EncodingThread(long n, String testData)
{
public EncodingThread(long n, String testData) {
_n = n;
_testData = testData;
_localpEp = new Engine();
}
public void run() {
encodingTest(_n, _testData);
encodingTest(_localpEp, _n, _testData);
}
}
public static void main(String[] args) {
long decoding = 0;
long encoding = 0;
long decodingCount = 0;
long encodingCount = 0;
int deth = 1;
int enth = 1;
@ -90,8 +92,7 @@ public class SpeedTest {
String testData = testMessage.toString();
for (int i = 0; i < args.length; ++i) {
if (args[i].compareTo("-h") == 0 || args[i].compareTo("--help") ==0)
{
if (args[i].compareTo("-h") == 0 || args[i].compareTo("--help") == 0) {
System.out.println("SpeedTest [-e |--encode NUMBER] [-d | --decode NUMBER] [-f | --file TESTDATA] [-jd | --decoding-threads DT] [-je | --encoding-threads] [-h | --help]\n"
+ "\nEncodes and/or decodes messages to measure the speed.\n\n"
+ " -d, --decode NUMBER decode NUMBER messages\n"
@ -104,43 +105,35 @@ public class SpeedTest {
+ "NUMBER times, respectively. If you omit -f it will encode a default data set.\n"
);
System.exit(0);
}
else if (args[i].compareTo("-d") == 0 || args[i].compareTo("--decode") == 0) {
} else if (args[i].compareTo("-d") == 0 || args[i].compareTo("--decode") == 0) {
try {
decoding = Long.parseLong(args[i+1]);
decodingCount = Long.parseLong(args[i + 1]);
++i;
}
catch (NumberFormatException ex) {
} catch (NumberFormatException ex) {
System.err.println(String.format("error: decimal number expected but found %s", args[i + 1]));
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException ex) {
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println(String.format("error: %s is requiring a decimal number as argument", args[i]));
System.exit(1);
}
}
else if (args[i].compareTo("-e") == 0 || args[i].compareTo("--encode") == 0) {
} else if (args[i].compareTo("-e") == 0 || args[i].compareTo("--encode") == 0) {
try {
encoding = Long.parseLong(args[i+1]);
encodingCount = Long.parseLong(args[i + 1]);
++i;
}
catch (NumberFormatException ex) {
} catch (NumberFormatException ex) {
System.err.println(String.format("error: decimal number expected but found %s", args[i + 1]));
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException ex) {
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println(String.format("error: %s is requiring a decimal number as argument", args[i]));
System.exit(1);
}
}
else if (args[i].compareTo("-f") == 0 || args[i].compareTo("--file") == 0) {
} else if (args[i].compareTo("-f") == 0 || args[i].compareTo("--file") == 0) {
String filename = "";
try {
filename = args[i + 1];
++i;
}
catch (ArrayIndexOutOfBoundsException ex) {
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println(String.format("error: %s is requiring a filename as argument", args[i]));
System.exit(1);
}
@ -149,52 +142,43 @@ public class SpeedTest {
if (filename.compareTo("-") == 0) {
Scanner s = new Scanner(System.in).useDelimiter("\\A");
testData = s.hasNext() ? s.next() : "";
}
else {
} else {
testData = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8);
}
}
catch (Exception ex) {
} catch (Exception ex) {
System.err.println(String.format("error: cannot read file %s", args[i]));
System.exit(2);
}
}
else if (args[i].compareTo("-jd") == 0 || args[i].compareTo("----decoding-threads") == 0) {
} else if (args[i].compareTo("-jd") == 0 || args[i].compareTo("----decoding-threads") == 0) {
try {
deth = Integer.parseInt(args[i + 1]);
++i;
}
catch (NumberFormatException ex) {
} catch (NumberFormatException ex) {
System.err.println(String.format("error: decimal number expected but found %s", args[i + 1]));
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException ex) {
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println(String.format("error: %s is requiring a decimal number as argument", args[i]));
System.exit(1);
}
}
else if (args[i].compareTo("-je") == 0 || args[i].compareTo("----encoding-threads") == 0) {
} else if (args[i].compareTo("-je") == 0 || args[i].compareTo("----encoding-threads") == 0) {
try {
enth = Integer.parseInt(args[i + 1]);
++i;
}
catch (NumberFormatException ex) {
} catch (NumberFormatException ex) {
System.err.println(String.format("error: decimal number expected but found %s", args[i + 1]));
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException ex) {
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.println(String.format("error: %s is requiring a decimal number as argument", args[i]));
System.exit(1);
}
}
else {
} else {
System.err.println(String.format("illegal parameter: %s", args[i]));
System.exit(1);
}
}
if (decoding < 0 || encoding < 0 || !(encoding > 0 || decoding > 0)) {
if (decodingCount < 0 || encodingCount < 0 || !(encodingCount > 0 || decodingCount > 0)) {
System.err.println("arguments error: -d or -e (or both) must be used with a positive number");
System.exit(1);
}
@ -214,65 +198,83 @@ public class SpeedTest {
you.user_id = me.user_id;
pEp.myself(you); // make a key for it
Message enc = encrypt(testData);
Message enc = encrypt(pEp, testData);
String testDataEnc = codec.encode(enc, null);
Thread[] dts = new Thread[deth];
Thread[] ets = new Thread[enth];
System.out.println("Initializing...");
System.out.println("Creating "+ deth + " decoding threads");
// create threads
if (deth > 1) {
SpeedTest st = new SpeedTest();
for (int i = 0; i < deth; ++i) {
dts[i] = st.new DecodingThread(decodingCount, testDataEnc);
}
}
System.out.println("Creating "+ enth + " encoding threads");
if (enth > 1) {
SpeedTest st = new SpeedTest();
for (int i = 0; i < enth; ++i) {
ets[i] = st.new EncodingThread(encodingCount, testData);
}
}
// Benchmark starting
System.out.println("Starting benchmark...");
System.out.println("decoding...");
long startTime = System.nanoTime();
if (decoding > 0) {
if (deth == 1) {
decodingTest(decoding, testDataEnc);
}
else {
decodingTest(pEp, decodingCount, testDataEnc);
} else {
SpeedTest st = new SpeedTest();
Thread[] dts = new Thread[deth];
for (int i = 0; i < deth; ++i) {
dts[i] = st.new DecodingThread(decoding, testDataEnc);
dts[i].start();
}
for (int i = 0; i < deth; ++i) {
try {
dts[i].join();
}
catch (InterruptedException ex) { }
} catch (InterruptedException ex) {
}
}
}
long decodingTime = System.nanoTime();
long decodingDelta = decodingTime - startTime;
if (encoding > 0) {
System.out.println("encoding...");
if (enth == 1) {
encodingTest(decoding, testData);
}
else {
encodingTest(pEp, decodingCount, testData);
} else {
SpeedTest st = new SpeedTest();
Thread[] ets = new Thread[enth];
for (int i = 0; i < enth; ++i) {
ets[i] = st.new EncodingThread(encoding, testData);
ets[i].start();
}
for (int i = 0; i < enth; ++i) {
try {
ets[i].join();
}
catch (InterruptedException ex) { }
} catch (InterruptedException ex) {
}
}
}
long encodingDelta = System.nanoTime() - decodingTime;
double ent = (double) encodingDelta / 1000000000;
double det = (double) decodingDelta / 1000000000;
double encTimeSecs = (double) encodingDelta / 1000000000;
double decTimeSecs = (double) decodingDelta / 1000000000;
double enr = (double) encoding / ent;
double der = (double) decoding / det;
double enr = (double) encodingCount * enth / encTimeSecs;
double der = (double) decodingCount * deth / decTimeSecs;
System.out.println(String.format(
"encrypted and encoded %d messages in %.3f sec. (%.1f msg./sec. per core)\n"
+ "decrypted and decoded %d messages in %.3f sec. (%.1f msg./sec. per core)",
encoding, ent, enr, decoding, det, der));
encodingCount, encTimeSecs, enr, decodingCount, decTimeSecs, der));
}
}

Loading…
Cancel
Save