1 package perf; 2 3 import java.io.IOException; 4 import java.util.concurrent.atomic.AtomicInteger; 5 6 import com.fasterxml.jackson.core.*; 7 8 /** 9 * Manual performance test to try out various synchronization 10 * methods for symbol tables. 11 */ 12 public class ConcurrencyReadTest 13 { 14 private final static int THREADS = 50; 15 test()16 private void test() throws Exception 17 { 18 final JsonFactory jf = new JsonFactory(); 19 final byte[] INPUT = "{\"a\":1}".getBytes("UTF-8"); 20 final AtomicInteger count = new AtomicInteger(); 21 22 for (int i = 0; i < THREADS; ++i) { 23 new Thread(new Runnable() { 24 @Override 25 public void run() 26 { 27 try { 28 while (true) { 29 parse(jf, INPUT); 30 count.addAndGet(1); 31 } 32 } catch (IOException e) { 33 System.err.println("PROBLEM: "+e); 34 } 35 } 36 }).start(); 37 } 38 39 // wait slightly.... 40 Thread.sleep(200L); 41 42 double totalTime = 0.0; 43 double totalCount = 0.0; 44 45 while (true) { 46 long start = System.currentTimeMillis(); 47 int startCount = count.get(); 48 49 Thread.sleep(1000L); 50 51 int done = count.get() - startCount; 52 long time = System.currentTimeMillis() - start; 53 54 totalTime += time; 55 totalCount += done; 56 57 double rate = (double) done / (double) time; 58 System.out.printf("Rate: %.1f (avg: %.1f)\n", rate, totalCount/totalTime); 59 } 60 } 61 parse(JsonFactory jf, byte[] input)62 protected void parse(JsonFactory jf, byte[] input) throws IOException 63 { 64 JsonParser jp = jf.createParser(input, 0, input.length); 65 while (jp.nextToken() != null) { 66 ; 67 } 68 jp.close(); 69 } 70 main(String[] args)71 public static void main(String[] args) throws Exception 72 { 73 if (args.length != 0) { 74 System.err.println("Usage: java ..."); 75 System.exit(1); 76 } 77 new ConcurrencyReadTest().test(); 78 } 79 80 } 81