|
| 1 | +package org.simdjson; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.Iterator; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | + |
| 9 | +import org.checkerframework.checker.units.qual.A; |
| 10 | +import org.openjdk.jmh.annotations.Benchmark; |
| 11 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 12 | +import org.openjdk.jmh.annotations.Mode; |
| 13 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 14 | +import org.openjdk.jmh.annotations.Param; |
| 15 | +import org.openjdk.jmh.annotations.Scope; |
| 16 | +import org.openjdk.jmh.annotations.State; |
| 17 | + |
| 18 | +import com.alibaba.fastjson2.JSON; |
| 19 | +import com.alibaba.fastjson2.JSONObject; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | + |
| 24 | +@State(Scope.Benchmark) |
| 25 | +@BenchmarkMode(Mode.Throughput) |
| 26 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 27 | +public class ParseAndSelectFixPathBenchMark { |
| 28 | + @Param({"/twitter.json"}) |
| 29 | + String fileName; |
| 30 | + private byte[] buffer; |
| 31 | + private final SimdJsonParser parser = new SimdJsonParser(); |
| 32 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 33 | + private final SimdJsonParser2 parser2 = new SimdJsonParser2( |
| 34 | + "statuses.0.user.default_profile", "statuses.0.user.screen_name", |
| 35 | + "statuses.1.user.default_profile", "statuses.1.user.screen_name", |
| 36 | + "statuses.2.user.default_profile", "statuses.2.user.screen_name", |
| 37 | + "statuses.3.user.default_profile", "statuses.3.user.screen_name", |
| 38 | + "statuses.4.user.default_profile", "statuses.4.user.screen_name", |
| 39 | + "statuses.5.user.default_profile", "statuses.5.user.screen_name", |
| 40 | + "statuses.6.user.default_profile", "statuses.6.user.screen_name", |
| 41 | + "statuses.7.user.default_profile", "statuses.7.user.screen_name", |
| 42 | + "statuses.8.user.default_profile", "statuses.8.user.screen_name", |
| 43 | + "statuses.9.user.default_profile", "statuses.9.user.screen_name"); |
| 44 | + |
| 45 | + @Benchmark |
| 46 | + public String[] parseMultiValuesForFixPaths_SimdJson() { |
| 47 | + JsonValue jsonValue = parser.parse(buffer, buffer.length); |
| 48 | + String[] result = new String[20]; |
| 49 | + Iterator<JsonValue> tweets = jsonValue.get("statuses").arrayIterator(); |
| 50 | + int i = 0; |
| 51 | + while (tweets.hasNext() && i++ < 10) { |
| 52 | + JsonValue tweet = tweets.next(); |
| 53 | + result[i] = tweet.get("user").get("default_profile").asString(); |
| 54 | + result[i + 1] = tweet.get("user").get("screen_name").asString(); |
| 55 | + } |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + @Benchmark |
| 60 | + public String[] parseMultiValuesForFixPaths_SimdJson2() { |
| 61 | + return parser2.parse(buffer, buffer.length); |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public String[] parseMultiValuesForFixPaths_Jackson() throws IOException { |
| 66 | + JsonNode jacksonJsonNode = objectMapper.readTree(buffer); |
| 67 | + String[] result = new String[20]; |
| 68 | + ArrayNode tweets = (ArrayNode) jacksonJsonNode.get("statuses"); |
| 69 | + for (int i = 0; i < 10; i++) { |
| 70 | + result[i] = tweets.get(i).path("user").path("default_profile").textValue(); |
| 71 | + result[i + 1] = tweets.get(i).path("user").path("screen_name").textValue(); |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | +} |
0 commit comments