apache/fury

Multi Thread Serialize/Deserialize benchmark issue 多线程下序列化性能异常

Opened this issue · 2 comments

Search before asking

  • I had searched in the issues and found no similar issues.

Version

os:windows 10
java: 1.8.0_271
fury:
org.apache.fury
fury-core
0.5.0

Component(s)

Java

Minimal reproduce step

private void benchmark(BaseFury fury) {
    long start = System.nanoTime();
    for (int i = 0; i < COUNT; i++) {
        User user = new User("John", "Doe", 30);
        byte[] serialize = fury.serialize(user);
        Object deserialize = fury.deserialize(serialize);
    }
    long end = System.nanoTime();
    long elapsed = end - start;
    log.info("Elapsed time: {} ns, tps= {} /s", elapsed, String.format("%.2f", COUNT / (elapsed / 1e9)));
}

@Test
public void testFury() {
    Fury fury = Fury.builder().withLanguage(Language.JAVA)
            // Allow to deserialize objects unknown types, more flexible
            // but may be insecure if the classes contains malicious code.
            .requireClassRegistration(true)
            .build();
    // Registering types can reduce class name serialization overhead, but not mandatory.
    // If class registration enabled, all custom types must be registered.
    fury.register(TestFury.class);
    fury.register(User.class);

    benchmark(fury);
}

@Test
public void testMultiFury() throws InterruptedException {
    ThreadSafeFury fury = Fury.builder().withLanguage(Language.JAVA)
            // Allow to deserialize objects unknown types, more flexible
            // but may be insecure if the classes contains malicious code.
            .requireClassRegistration(true)
            .buildThreadSafeFury();
    fury.register(TestFury.class);
    fury.register(User.class);

    int threads = Runtime.getRuntime().availableProcessors();
    for (int i = 0; i < threads; i++) {
        new Thread(() -> {
            benchmark(fury);
        }).start();
    }

    Thread.currentThread().join();
}
class User {
    private String firstName;
    private String lastName;
    private int age;

    public User(String john, String doe, int i) {
        this.firstName = john;
        this.lastName = doe;
        this.age = i;
    }

What did you expect to see?

单线程与多线程序列化反序列化性能一致

What did you see instead?

单线程Fury Benchmark

2024-05-14 12:38:36 INFO  Fury:144 [main] - Created new fury org.apache.fury.Fury@29626d54
2024-05-14 12:38:36 INFO  CompileUnit:55 [main] - Generate code for com.example.test.UserFuryCodec_1_414493378_1516500233 took 71 ms.
2024-05-14 12:38:36 INFO  JaninoUtils:121 [main] - Compile [UserFuryCodec_1_414493378_1516500233] take 265 ms
24/05/14 12:38:58.959 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 22694408600 ns, tps= 4406371.71 /s 

多线程ThreadSafeFury Benchmark

2024-05-14 12:38:58 INFO  Fury:144 [main] - Created new fury org.apache.fury.Fury@624ea235
2024-05-14 12:38:58 INFO  Fury:144 [Thread-3] - Created new fury org.apache.fury.Fury@4ff7ff2e
2024-05-14 12:38:58 INFO  Fury:144 [Thread-2] - Created new fury org.apache.fury.Fury@60bfb620
2024-05-14 12:38:58 INFO  Fury:144 [Thread-6] - Created new fury org.apache.fury.Fury@7ff887
2024-05-14 12:38:58 INFO  Fury:144 [Thread-4] - Created new fury org.apache.fury.Fury@44de8d4a
2024-05-14 12:38:58 INFO  Fury:144 [Thread-5] - Created new fury org.apache.fury.Fury@159886f2
2024-05-14 12:38:58 INFO  Fury:144 [Thread-7] - Created new fury org.apache.fury.Fury@1d5fc898
2024-05-14 12:38:58 INFO  Fury:144 [Thread-1] - Created new fury org.apache.fury.Fury@47540be1
2024-05-14 12:38:58 INFO  Fury:144 [Thread-8] - Created new fury org.apache.fury.Fury@69b38d00
24/05/14 12:39:28.896 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 29918667800 ns, tps= 3342394.81 /s 
24/05/14 12:39:30.318 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 31341023100 ns, tps= 3190706.30 /s 
24/05/14 12:39:33.827 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 34851435100 ns, tps= 2869322.30 /s 
24/05/14 12:39:34.476 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 35503676500 ns, tps= 2816609.71 /s 
24/05/14 12:39:35.605 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 36628779700 ns, tps= 2730093.68 /s 
24/05/14 12:39:36.613 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 37636506500 ns, tps= 2656994.75 /s 
24/05/14 12:39:36.622 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 37644876100 ns, tps= 2656404.01 /s 
24/05/14 12:39:36.769 [INFO ] 23 com.example.test.TestFury[23] - Elapsed time: 37792022400 ns, tps= 2646061.09 /s 

问题

  1. 多线程tps约为单线程tps一半
  2. 多线程之间tps结果差异较大

Anything Else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

It is recommended to use JMH for load testing; unit test is not accurate

@j9kkk Could you try 0.5.1 out? And it would be better if you can provide a profiling graph