-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jimeng
committed
Oct 7, 2024
1 parent
ebe3d00
commit 5c92d47
Showing
5 changed files
with
138 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.simdjson; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Param; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public class Parse2VsJacksonBenchMark { | ||
@Param({"/twitter.json"}) | ||
String fileName; | ||
private byte[] buffer; | ||
private final SimdJsonParser2 parser = new SimdJsonParser2("statuses.0.metadata", "metadata.0.created_at", "metadata.0.id", | ||
"statuses.1.metadata", "metadata.1.created_at", "metadata.1.id"); | ||
private final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
@Setup(Level.Trial) | ||
public void setup() throws IOException { | ||
try (InputStream is = ParseBenchmark.class.getResourceAsStream(fileName)) { | ||
assert is != null; | ||
buffer = is.readAllBytes(); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void parseBySimdJson() { | ||
String[] result = parser.parse(buffer, buffer.length); | ||
} | ||
|
||
@Benchmark | ||
public void parseByJackson() throws Exception { | ||
ArrayNode arrayNode = (ArrayNode) MAPPER.readTree(buffer).path("statuses"); | ||
String[] result = new String[6]; | ||
result[0] = arrayNode.get(0).path("metadata").toString(); | ||
result[1] = arrayNode.get(0).path("created_at").toString(); | ||
result[2] = arrayNode.get(0).path("id").toString(); | ||
result[3] = arrayNode.get(0).path("metadata").toString(); | ||
result[4] = arrayNode.get(0).path("created_at").toString(); | ||
result[5] = arrayNode.get(0).path("id").toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package org.simdjson; | ||
|
||
import java.util.Arrays; | ||
|
||
public class BitIndexes { | ||
|
||
private final int[] indexes; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.simdjson; | ||
|
||
import static org.simdjson.testutils.SimdJsonAssertions.assertThat; | ||
import static org.simdjson.testutils.TestUtils.toUtf8; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class JsonMultiValueParsingTest { | ||
@Test | ||
public void testParseMultiValue() { | ||
byte[] json = toUtf8("{\"field1\":{\"field2\":\"value2\",\"field3\":3},\"field4\":[\"value4\",\"value5\"],\"field5\":null}"); | ||
SimdJsonParser2 parser = new SimdJsonParser2("field1.field2", "field1.field3", "field4", "field4.0", "field5"); | ||
String[] result = parser.parse(json, json.length); | ||
assertThat(result[0]).isEqualTo("value2"); | ||
assertThat(result[1]).isEqualTo("3"); | ||
assertThat(result[2]).isEqualTo("[\"value4\",\"value5\"]"); | ||
assertThat(result[3]).isEqualTo("value4"); | ||
assertThat(result[4]).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
public void testNonAsciiCharacters() { | ||
byte[] json = toUtf8("{\"ąćśńźż\": 1, \"\\u20A9\\u0E3F\": 2, \"αβγ\": 3, \"😀abc😀\": 4}"); | ||
SimdJsonParser2 parser = new SimdJsonParser2("ąćśńźż", "\\u20A9\\u0E3F", "αβγ", "😀abc😀"); | ||
// when | ||
String[] result = parser.parse(json, json.length); | ||
// then | ||
assertThat(result[0]).isEqualTo("1"); | ||
assertThat(result[1]).isEqualTo("2"); | ||
assertThat(result[2]).isEqualTo("3"); | ||
assertThat(result[3]).isEqualTo("4"); | ||
} | ||
} |