|
| 1 | +package com.github.piotrrzysko.simdjson; |
| 2 | + |
| 3 | +import com.alibaba.fastjson2.JSON; |
| 4 | +import com.alibaba.fastjson2.JSONObject; |
| 5 | +import com.fasterxml.jackson.databind.JsonNode; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.jsoniter.JsonIterator; |
| 8 | +import com.jsoniter.any.Any; |
| 9 | +import org.openjdk.jmh.annotations.Benchmark; |
| 10 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 11 | +import org.openjdk.jmh.annotations.Level; |
| 12 | +import org.openjdk.jmh.annotations.Mode; |
| 13 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 14 | +import org.openjdk.jmh.annotations.Scope; |
| 15 | +import org.openjdk.jmh.annotations.Setup; |
| 16 | +import org.openjdk.jmh.annotations.State; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import static com.github.piotrrzysko.simdjson.SimdJsonPaddingUtil.padded; |
| 26 | + |
| 27 | +@State(Scope.Benchmark) |
| 28 | +@BenchmarkMode(Mode.Throughput) |
| 29 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 30 | +public class ParseAndSelectBenchmark { |
| 31 | + |
| 32 | + private final SimdJsonParser simdJsonParser = new SimdJsonParser(); |
| 33 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 34 | + |
| 35 | + private byte[] buffer; |
| 36 | + private byte[] bufferPadded; |
| 37 | + |
| 38 | + @Setup(Level.Trial) |
| 39 | + public void setup() throws IOException { |
| 40 | + try (InputStream is = ParseBenchmark.class.getResourceAsStream("/twitter.json")) { |
| 41 | + buffer = is.readAllBytes(); |
| 42 | + bufferPadded = padded(buffer); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + public int countUniqueUsersWithDefaultProfile_jackson() throws IOException { |
| 48 | + JsonNode jacksonJsonNode = objectMapper.readTree(buffer); |
| 49 | + Set<String> defaultUsers = new HashSet<>(); |
| 50 | + Iterator<JsonNode> tweets = jacksonJsonNode.get("statuses").elements(); |
| 51 | + while (tweets.hasNext()) { |
| 52 | + JsonNode tweet = tweets.next(); |
| 53 | + JsonNode user = tweet.get("user"); |
| 54 | + if (user.get("default_profile").asBoolean()) { |
| 55 | + defaultUsers.add(user.get("screen_name").textValue()); |
| 56 | + } |
| 57 | + } |
| 58 | + return defaultUsers.size(); |
| 59 | + } |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + public int countUniqueUsersWithDefaultProfile_fastjson() { |
| 63 | + JSONObject jsonObject = (JSONObject) JSON.parse(buffer); |
| 64 | + Set<String> defaultUsers = new HashSet<>(); |
| 65 | + Iterator<Object> tweets = jsonObject.getJSONArray("statuses").iterator(); |
| 66 | + while (tweets.hasNext()) { |
| 67 | + JSONObject tweet = (JSONObject) tweets.next(); |
| 68 | + JSONObject user = (JSONObject) tweet.get("user"); |
| 69 | + if (user.getBoolean("default_profile")) { |
| 70 | + defaultUsers.add(user.getString("screen_name")); |
| 71 | + } |
| 72 | + } |
| 73 | + return defaultUsers.size(); |
| 74 | + } |
| 75 | + |
| 76 | + @Benchmark |
| 77 | + public int countUniqueUsersWithDefaultProfile_jsoniter() { |
| 78 | + Any json = JsonIterator.deserialize(buffer); |
| 79 | + Set<String> defaultUsers = new HashSet<>(); |
| 80 | + for (Any tweet : json.get("statuses")) { |
| 81 | + Any user = tweet.get("user"); |
| 82 | + if (user.get("default_profile").toBoolean()) { |
| 83 | + defaultUsers.add(user.get("screen_name").toString()); |
| 84 | + } |
| 85 | + } |
| 86 | + return defaultUsers.size(); |
| 87 | + } |
| 88 | + |
| 89 | + @Benchmark |
| 90 | + public int countUniqueUsersWithDefaultProfile_simdjson() { |
| 91 | + JsonValue simdJsonValue = simdJsonParser.parse(buffer, buffer.length); |
| 92 | + Set<String> defaultUsers = new HashSet<>(); |
| 93 | + Iterator<JsonValue> tweets = simdJsonValue.get("statuses").arrayIterator(); |
| 94 | + while (tweets.hasNext()) { |
| 95 | + JsonValue tweet = tweets.next(); |
| 96 | + JsonValue user = tweet.get("user"); |
| 97 | + if (user.get("default_profile").asBoolean()) { |
| 98 | + defaultUsers.add(user.get("screen_name").asString()); |
| 99 | + } |
| 100 | + } |
| 101 | + return defaultUsers.size(); |
| 102 | + } |
| 103 | + |
| 104 | + @Benchmark |
| 105 | + public int countUniqueUsersWithDefaultProfile_simdjsonPadded() { |
| 106 | + JsonValue simdJsonValue = simdJsonParser.parse(bufferPadded, buffer.length); |
| 107 | + Set<String> defaultUsers = new HashSet<>(); |
| 108 | + Iterator<JsonValue> tweets = simdJsonValue.get("statuses").arrayIterator(); |
| 109 | + while (tweets.hasNext()) { |
| 110 | + JsonValue tweet = tweets.next(); |
| 111 | + JsonValue user = tweet.get("user"); |
| 112 | + if (user.get("default_profile").asBoolean()) { |
| 113 | + defaultUsers.add(user.get("screen_name").asString()); |
| 114 | + } |
| 115 | + } |
| 116 | + return defaultUsers.size(); |
| 117 | + } |
| 118 | +} |
0 commit comments