1 package perf; 2 3 import com.fasterxml.jackson.core.*; 4 5 public class ManualReadPerfWithMedia extends ParserTestBase 6 { 7 protected final JsonFactory _factory; 8 9 protected final String _json; 10 ManualReadPerfWithMedia(JsonFactory f, String json)11 private ManualReadPerfWithMedia(JsonFactory f, String json) throws Exception { 12 _factory = f; 13 _json = json; 14 } 15 main(String[] args)16 public static void main(String[] args) throws Exception 17 { 18 if (args.length != 0) { 19 System.err.println("Usage: java ..."); 20 System.exit(1); 21 } 22 MediaItem.Content content = new MediaItem.Content(); 23 content.setTitle("Performance micro-benchmark, to be run manually"); 24 content.addPerson("William"); 25 content.addPerson("Robert"); 26 content.setWidth(900); 27 content.setHeight(120); 28 content.setBitrate(256000); 29 content.setDuration(3600 * 1000L); 30 content.setCopyright("none"); 31 content.setPlayer(MediaItem.Player.FLASH); 32 content.setUri("http://whatever.biz"); 33 34 MediaItem input = new MediaItem(content); 35 input.addPhoto(new MediaItem.Photo("http://a.com", "title1", 200, 100, MediaItem.Size.LARGE)); 36 input.addPhoto(new MediaItem.Photo("http://b.org", "title2", 640, 480, MediaItem.Size.SMALL)); 37 38 final JsonFactory f = new JsonFactory(); 39 final String jsonStr = input.asJsonString(f); 40 final byte[] json = jsonStr.getBytes("UTF-8"); 41 42 new ManualReadPerfWithMedia(f, jsonStr).test("String", "char[]", json.length); 43 } 44 45 @Override testRead1(int reps)46 protected void testRead1(int reps) throws Exception 47 { 48 while (--reps >= 0) { 49 // JsonParser p = _factory.createParser(new StringReader(_json)); 50 JsonParser p = _factory.createParser(_json); 51 _stream(p); 52 p.close(); 53 } 54 } 55 56 @Override testRead2(int reps)57 protected void testRead2(int reps) throws Exception 58 { 59 final char[] ch = _json.toCharArray(); 60 while (--reps >= 0) { 61 JsonParser p = _factory.createParser(ch, 0, ch.length); 62 _stream(p); 63 p.close(); 64 } 65 } 66 _stream(JsonParser p)67 private final void _stream(JsonParser p) throws Exception 68 { 69 JsonToken t; 70 71 while ((t = p.nextToken()) != null) { 72 // force decoding/reading of scalar values too (booleans are fine, nulls too) 73 if (t == JsonToken.VALUE_STRING) { 74 p.getText(); 75 } else if (t == JsonToken.VALUE_NUMBER_INT) { 76 p.getLongValue(); 77 } 78 } 79 } 80 } 81