Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Last active July 15, 2024 05:05
Show Gist options
  • Save komamitsu/ee4b5ee8292ff08d25ba660958101147 to your computer and use it in GitHub Desktop.
Save komamitsu/ee4b5ee8292ff08d25ba660958101147 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.fury.Fury;
import org.apache.fury.config.Language;
import org.msgpack.jackson.dataformat.MessagePackMapper;
import java.io.IOException;
import java.util.*;
public class CasualBenchForFury {
public static final int USERS_SIZE = 10000;
public static User[] USERS = new User[USERS_SIZE];
static {
Random random = new Random();
for (int i = 0; i < USERS_SIZE; i++) {
int childrenSize = random.nextInt(10);
List<String> childIds = new ArrayList<>(childrenSize);
for (int ci = 0; ci < childrenSize; ci++) {
childIds.add(UUID.randomUUID().toString());
}
USERS[i] = new User(UUID.randomUUID().toString(), random.nextInt(100), childIds);
}
}
public static class User {
public final String id;
public final int age;
public final List<String> childIds;
@JsonCreator
public User(
@JsonProperty("id") String id,
@JsonProperty("age") int age,
@JsonProperty("childIds") List<String> childIds) {
this.id = id;
this.age = age;
this.childIds = childIds;
}
}
private static void serToJson() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (User u : USERS) {
objectMapper.writeValueAsString(u);
}
}
System.out.println("SarToJson: Duration: " + (System.currentTimeMillis() - start));
}
private static void serToMsgpack() throws JsonProcessingException {
ObjectMapper objectMapper = new MessagePackMapper();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (User u : USERS) {
objectMapper.writeValueAsBytes(u);
}
}
System.out.println("SarToMsgpack: Duration: " + (System.currentTimeMillis() - start));
}
private static void serToFury() throws JsonProcessingException {
Fury fury = Fury.builder().withLanguage(Language.JAVA).build();
fury.register(User.class);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (User u : USERS) {
fury.serialize(u);
}
}
System.out.println("SarToFury: Duration: " + (System.currentTimeMillis() - start));
}
private static void deserFromJson() throws JsonProcessingException, InterruptedException {
ObjectMapper objectMapper = new ObjectMapper();
String[] serialized = new String[USERS_SIZE];
for (int i = 0; i < USERS_SIZE; i++) {
User u = USERS[i];
serialized[i] = objectMapper.writeValueAsString(u);
}
System.gc();
Thread.sleep(5000);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (String v : serialized) {
objectMapper.readValue(v, User.class);
}
}
System.out.println("DeserFromJson: Duration: " + (System.currentTimeMillis() - start));
}
private static void deserFromMsgpack() throws IOException, InterruptedException {
ObjectMapper objectMapper = new ObjectMapper();
byte[][] serialized = new byte[USERS_SIZE][];
for (int i = 0; i < USERS_SIZE; i++) {
User u = USERS[i];
serialized[i] = objectMapper.writeValueAsBytes(u);
}
System.gc();
Thread.sleep(5000);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (byte[] v : serialized) {
objectMapper.readValue(v, User.class);
}
}
System.out.println("DeserFromMsgpack: Duration: " + (System.currentTimeMillis() - start));
}
private static void deserFromFury() throws IOException, InterruptedException {
Fury fury = Fury.builder().withLanguage(Language.JAVA).build();
fury.register(User.class);
byte[][] serialized = new byte[USERS_SIZE][];
for (int i = 0; i < USERS_SIZE; i++) {
User u = USERS[i];
serialized[i] = fury.serialize(u);
}
System.gc();
Thread.sleep(5000);
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
for (byte[] v : serialized) {
fury.deserialize(v);
}
}
System.out.println("DeserFromFury: Duration: " + (System.currentTimeMillis() - start));
}
public static void main(String[] args) throws IOException, InterruptedException {
for (int i = 0; i < 2; i++) {
serToJson();
System.gc();
Thread.sleep(5000);
serToMsgpack();
System.gc();
Thread.sleep(5000);
serToFury();
System.gc();
Thread.sleep(5000);
deserFromJson();
System.gc();
Thread.sleep(5000);
deserFromMsgpack();
System.gc();
Thread.sleep(5000);
deserFromFury();
System.gc();
Thread.sleep(5000);
}
}
}
@komamitsu
Copy link
Author

komamitsu commented Jul 15, 2024

1:56:58 PM: Executing ':CasualBenchForFury.main()'...

Task :compileJava
Task :processResources NO-SOURCE
Task :classes
Task :CasualBenchForFury.main()
SarToJson: Duration: 33817
SarToMsgpack: Duration: 41209
2024-07-15 01:58:23 INFO Fury:141 [main] - Created new fury org.apache.fury.Fury@ed7f8b4
2024-07-15 01:58:23 INFO CompileUnit:55 [main] - Generate code for CasualBenchForFury_UserFuryCodec_1_1327763628_1452126962 took 27 ms.
2024-07-15 01:58:24 INFO JaninoUtils:121 [main] - Compile [CasualBenchForFury_UserFuryCodec_1_1327763628_1452126962] take 77 ms
SarToFury: Duration: 8398
DeserFromJson: Duration: 53917
DeserFromMsgpack: Duration: 49883
2024-07-15 02:00:41 INFO Fury:141 [main] - Created new fury org.apache.fury.Fury@79d94571
DeserFromFury: Duration: 9527
SarToJson: Duration: 34857
SarToMsgpack: Duration: 41942
2024-07-15 02:02:27 INFO Fury:141 [main] - Created new fury org.apache.fury.Fury@eda25e5
SarToFury: Duration: 8044
DeserFromJson: Duration: 54125
DeserFromMsgpack: Duration: 50695
2024-07-15 02:04:45 INFO Fury:141 [main] - Created new fury org.apache.fury.Fury@67304a40
DeserFromFury: Duration: 9464

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.9/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 8m 6s
2 actionable tasks: 2 executed
2:05:05 PM: Execution finished ':CasualBenchForFury.main()'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment