|
| 1 | +package perf; |
| 2 | + |
| 3 | +import tools.jackson.core.JsonGenerator; |
| 4 | +import tools.jackson.core.io.CharTypes; |
| 5 | +import tools.jackson.core.json.JsonFactory; |
| 6 | + |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +/** |
| 14 | + * Benchmarks the performance of writing UTF-8 encoded bytes, in particular the difference between using a 7-bit wide |
| 15 | + * lookup table for escapes, versus a full 8-bit wide table. The latter is beneficial when processing encoded UTF-8 |
| 16 | + * bytes, as the byte itself can directly be used as table index instead of needing an additional branch. |
| 17 | + * <p> |
| 18 | + * This benchmark implements the escaping UTF-8 write loops using both 7-bit and 8-bit tables to show their respective |
| 19 | + * differences, as well as testing {@link JsonGenerator#writeUTF8String} for benchmarking the production implementation. |
| 20 | + * |
| 21 | + * @see <a href="https://github.com/FasterXML/jackson-core/pull/1349">Github PR</a> |
| 22 | + */ |
| 23 | +public class ManualUtf8WriteTest |
| 24 | +{ |
| 25 | + private String test(byte[] utf8) throws Exception |
| 26 | + { |
| 27 | + final byte[] OUTPUT = new byte[utf8.length * 2]; |
| 28 | + ByteArrayOutputStream OUTPUT_STREAM = new ByteArrayOutputStream(utf8.length * 2); |
| 29 | + JsonGenerator generator = new JsonFactory().createGenerator(OUTPUT_STREAM); |
| 30 | + |
| 31 | + // Let's try to guestimate suitable size, N megs of output |
| 32 | + final int REPS = (int) ((double) (80 * 1000 * 1000) / (double) utf8.length); |
| 33 | + System.out.printf("%d bytes to scan, will do %d repetitions\n", |
| 34 | + utf8.length, REPS); |
| 35 | + |
| 36 | + int i = 0; |
| 37 | + int roundsDone = 0; |
| 38 | + final int TYPES = 3; |
| 39 | + final int WARMUP_ROUNDS = 5; |
| 40 | + final int ROUNDS = WARMUP_ROUNDS + 10; |
| 41 | + |
| 42 | + final long[] times = new long[TYPES]; |
| 43 | + |
| 44 | + while (i < ROUNDS * TYPES) { |
| 45 | + int round = i++ % TYPES; |
| 46 | + |
| 47 | + String msg; |
| 48 | + |
| 49 | + long msecs; |
| 50 | + switch (round) { |
| 51 | + case 0: |
| 52 | + msg = "Write UTF-8 [7-bit escaping table]"; |
| 53 | + msecs = writeUtf8_7BitEscapingTable(REPS, utf8, OUTPUT); |
| 54 | + break; |
| 55 | + case 1: |
| 56 | + msg = "Write UTF-8 [8-bit escaping table]"; |
| 57 | + msecs = writeUtf8_8BitEscapingTable(REPS, utf8, OUTPUT); |
| 58 | + break; |
| 59 | + case 2: |
| 60 | + msg = "JsonGenerator.writeUTF8String "; |
| 61 | + msecs = writeUtf8_JsonGenerator(REPS, utf8, OUTPUT_STREAM, generator); |
| 62 | + break; |
| 63 | + default: |
| 64 | + throw new Error(); |
| 65 | + } |
| 66 | + // skip first 5 rounds to let results stabilize |
| 67 | + if (roundsDone >= WARMUP_ROUNDS) { |
| 68 | + times[round] += msecs; |
| 69 | + } |
| 70 | + |
| 71 | + System.out.printf("Test '%s' -> %3d msecs\n", msg, msecs); |
| 72 | + if (round == TYPES - 1) { |
| 73 | + ++roundsDone; |
| 74 | + if ((roundsDone % 3) == 0) { |
| 75 | + System.out.println("[GC]"); |
| 76 | + Thread.sleep(100L); |
| 77 | + System.gc(); |
| 78 | + Thread.sleep(100L); |
| 79 | + } |
| 80 | + System.out.println(); |
| 81 | + } |
| 82 | + } |
| 83 | + double den = roundsDone - WARMUP_ROUNDS; |
| 84 | + |
| 85 | + return String.format("(7-bit, 8-bit, JsonGenerator): %5.1f / %5.1f / %5.1f msecs", |
| 86 | + times[0] / den, times[1] / den, times[2] / den); |
| 87 | + } |
| 88 | + |
| 89 | + private final long writeUtf8_7BitEscapingTable(int REPS, byte[] input, byte[] output) |
| 90 | + { |
| 91 | + long start = System.currentTimeMillis(); |
| 92 | + int[] outputEscapes = CharTypes.get7BitOutputEscapes(); |
| 93 | + |
| 94 | + while (--REPS >= 0) { |
| 95 | + int inOffset = 0; |
| 96 | + int outOffset = 0; |
| 97 | + int len = input.length; |
| 98 | + |
| 99 | + while (inOffset < len) { |
| 100 | + byte b = input[inOffset++]; |
| 101 | + int ch = b; |
| 102 | + if (ch < 0 || outputEscapes[ch] == 0) { |
| 103 | + output[outOffset++] = b; |
| 104 | + continue; |
| 105 | + } |
| 106 | + int escape = outputEscapes[ch]; |
| 107 | + if (escape > 0) { |
| 108 | + output[outOffset++] = (byte) '\\'; |
| 109 | + output[outOffset++] = (byte) escape; |
| 110 | + } else { |
| 111 | + throw new UnsupportedOperationException("ctrl character escapes are not covered in test"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + long time = System.currentTimeMillis() - start; |
| 116 | + return time; |
| 117 | + } |
| 118 | + |
| 119 | + private final long writeUtf8_8BitEscapingTable(int REPS, byte[] input, byte[] output) |
| 120 | + { |
| 121 | + long start = System.currentTimeMillis(); |
| 122 | + |
| 123 | + int[] outputEscapes = CharTypes.get7BitOutputEscapes(); |
| 124 | + int[] extendedOutputEscapes = new int[0xFF]; |
| 125 | + System.arraycopy(outputEscapes, 0, extendedOutputEscapes, 0, outputEscapes.length); |
| 126 | + |
| 127 | + while (--REPS >= 0) { |
| 128 | + int inOffset = 0; |
| 129 | + int outOffset = 0; |
| 130 | + int len = input.length; |
| 131 | + |
| 132 | + while (inOffset < len) { |
| 133 | + byte b = input[inOffset++]; |
| 134 | + int ch = b & 0xFF; |
| 135 | + int escape = extendedOutputEscapes[ch]; |
| 136 | + if (escape == 0) { |
| 137 | + output[outOffset++] = b; |
| 138 | + continue; |
| 139 | + } |
| 140 | + if (escape > 0) { |
| 141 | + output[outOffset++] = (byte) '\\'; |
| 142 | + output[outOffset++] = (byte) escape; |
| 143 | + } else { |
| 144 | + throw new UnsupportedOperationException("ctrl character escapes are not covered in test"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + long time = System.currentTimeMillis() - start; |
| 150 | + return time; |
| 151 | + } |
| 152 | + |
| 153 | + private final long writeUtf8_JsonGenerator(int REPS, byte[] input, ByteArrayOutputStream output, JsonGenerator generator) throws IOException { |
| 154 | + long start = System.currentTimeMillis(); |
| 155 | + |
| 156 | + while (--REPS >= 0) { |
| 157 | + output.reset(); |
| 158 | + generator.writeUTF8String(input, 0, input.length); |
| 159 | + generator.flush(); |
| 160 | + } |
| 161 | + |
| 162 | + long time = System.currentTimeMillis() - start; |
| 163 | + return time; |
| 164 | + } |
| 165 | + |
| 166 | + public static void main(String[] args) throws Exception |
| 167 | + { |
| 168 | + if (args.length != 0) { |
| 169 | + System.err.println("Usage: java ..."); |
| 170 | + System.exit(1); |
| 171 | + } |
| 172 | + |
| 173 | + final int[] LENGTHS = new int[]{8, 16, 32, 256, 512, 1024, 1024 * 8}; |
| 174 | + final String[] ESCAPE_VARIANTS = new String[] {"none", "start", "end"}; |
| 175 | + final List<String> results = new ArrayList<String>(); |
| 176 | + for (int length : LENGTHS){ |
| 177 | + final byte[] buffer = new byte[length]; |
| 178 | + |
| 179 | + for (int j = 0; j < ESCAPE_VARIANTS.length; j++) { |
| 180 | + Arrays.fill(buffer, (byte) 'a'); |
| 181 | + |
| 182 | + if (j == 1) { |
| 183 | + buffer[0] = '"'; |
| 184 | + } else if (j == 2) { |
| 185 | + buffer[buffer.length - 1] = '"'; |
| 186 | + } |
| 187 | + |
| 188 | + String LABEL = String.format("Length %4d, %5s escape", length, ESCAPE_VARIANTS[j]); |
| 189 | + |
| 190 | + System.out.printf("Starting %s %n", LABEL); |
| 191 | + String result = new ManualUtf8WriteTest().test(buffer); |
| 192 | + System.out.printf("Finished %s %n", LABEL); |
| 193 | + System.out.println("================================================================================"); |
| 194 | + |
| 195 | + results.add(String.format("%s: %s", LABEL, result)); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + for (String result : results) { |
| 200 | + System.out.println(result); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments