Last active
July 15, 2024 05:05
-
-
Save komamitsu/ee4b5ee8292ff08d25ba660958101147 to your computer and use it in GitHub Desktop.
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
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1:56:58 PM: Executing ':CasualBenchForFury.main()'...
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()'.