IoUtil { public static String fastOpIdExtraction(String md) { if (md.length() < 18) return "00000000"; int opidlength = 4; int taskidlength = 4; byte flag = (byte) Integer.parseInt(md.substring(0, 2), 16); if ((flag & 0x08) != 0) { opidlength = 8; } switch (flag & 0x03) { case 0x00: taskidlength = 4; break; case 0x01: taskidlength = 8; break; case 0x02: taskidlength = 12; break; case 0x03: taskidlength = 20; break; default: } return md.substring(2 + 2*taskidlength, 2 + 2*taskidlength + 2*opidlength); }  static String fastOpIdExtraction(String md); static String bytesToString(final byte[] bytes); static String bytesToString(final byte[] bytes,
		                              final int offset,
		                              final int length); static byte[] stringToBytes(final String str); static boolean validateHexString(final String hex); static String intToString(int i); static String longToString(long l); static long hexStringToLong(String s); static String escapeNewlines(String str);  }
@Test public void testFastOpIdExtraction() { XTraceMetadata md = new XTraceMetadata(new TaskID(4), (int) 0); assertEquals("00000000", IoUtil.fastOpIdExtraction(md.toString())); md = new XTraceMetadata(new TaskID(4), (long) 0); assertEquals("0000000000000000", IoUtil.fastOpIdExtraction(md.toString())); md = new XTraceMetadata(new TaskID(12), (int) 0); assertEquals("00000000", IoUtil.fastOpIdExtraction(md.toString())); md = new XTraceMetadata(new TaskID(12), (long) 0); assertEquals("0000000000000000", IoUtil.fastOpIdExtraction(md.toString())); final int NUM_TESTS = 10000; for (int i = 0; i < NUM_TESTS; i++) { if (rnd.nextBoolean()) { md = new XTraceMetadata(tasks[rnd.nextInt(4)], rnd.nextInt()); } else { md = new XTraceMetadata(tasks[rnd.nextInt(4)], rnd.nextLong()); } assertEquals(md.getOpIdString(), IoUtil.fastOpIdExtraction(md.toString())); } }
@Test public void opidExtractionPerformance() { final int NUM_TESTS = 100000; String[] md = new String[NUM_TESTS]; for (int i = 0; i < NUM_TESTS; i++) { if (rnd.nextBoolean()) { md[i] = new XTraceMetadata(tasks[rnd.nextInt(4)], rnd.nextInt()).toString(); } else { md[i] = new XTraceMetadata(tasks[rnd.nextInt(4)], rnd.nextLong()).toString(); } } long startStandard = System.currentTimeMillis(); for (int i = 0; i < md.length; i++) { XTraceMetadata mdobj = XTraceMetadata.createFromString(md[i]); String opidstr = mdobj.getOpIdString(); } long endStandard = System.currentTimeMillis(); long startFast = System.currentTimeMillis(); for (int i = 0; i < md.length; i++) { String opidstr = IoUtil.fastOpIdExtraction(md[i]); } long endFast = System.currentTimeMillis(); System.out.println("Standard completed in " + (endStandard - startStandard) + " ms"); System.out.println("Fast completed in " + (endFast - startFast) + " ms"); }