Skip to content

Instantly share code, notes, and snippets.

@RichardWarburton
Created January 6, 2013 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardWarburton/4467295 to your computer and use it in GitHub Desktop.
Save RichardWarburton/4467295 to your computer and use it in GitHub Desktop.
package org.glassfish.json;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.json.JsonObject;
import javax.json.JsonReader;
import junit.framework.TestCase;
public class JsonPerformanceTest extends TestCase {
private static final int TIMES = 1000 * 1000;
public static JsonObject object;
public void testMuchosThreads() throws IOException, InterruptedException {
int numberOfCores = Runtime.getRuntime().availableProcessors();
int factor = 3; // Initially 1
int numberOfThreads = factor * numberOfCores;
long time = System.currentTimeMillis();
ReadStrings[] threads = new ReadStrings[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
threads[i] = new ReadStrings();
threads[i].start();
}
for (int i = 0; i < numberOfThreads; i++) {
threads[i].join();
}
long taken = System.currentTimeMillis() - time;
System.out.println(taken);
System.err.println(object.hashCode());
}
class ReadStrings extends Thread {
@Override
public void run() {
Path path = FileSystems.getDefault().getPath(".", "src/test/resources/other-file.json");
String someJson;
try {
someJson = new String(Files.readAllBytes(path));
for (int i = 0; i < TIMES; i++) {
JsonReader reader = new JsonReader(new StringReader(someJson));
object = reader.readObject();
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment