Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transplant jmh test Ring.java from Loom #127

Open
wants to merge 7 commits into
base: KonaFiber
Choose a base branch
from

Conversation

HangerLIN
Copy link

How to Run the Test

  1. Make sure Java 1.8.0_362_fiber is installed.
  2. Run the command mvn clean packageto build the project.
  3. Run the main method in the class org.example.Ring to start the test.
  4. See the result in the file res.json

changes:

  1. Add jmh dependencies in pom.xml

        <dependencies>
            <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-core</artifactId>
                <version>1.23</version>
            </dependency>
            <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-generator-annprocess</artifactId>
                <version>1.23</version>
            </dependency>
        </dependencies>
  2. Altered the Method for Starting Workers:

    • In the Loom version, the startAll() method was using Thread.startVirtualThread(w); to start virtual threads.
    • In the Kona version, in the same method I use Thread.ofVirtual().scheduler(Executors.newSingleThreadExecutor()).start(w); to start the virtual threads as well.
  3. Modified Channel Creation:

    • In the Loom version, the getChannel() method used the enhanced switch expression syntax:
      return switch(stackFrame) {
      ...
      default -> throw new RuntimeException("Illegal stack parameter value: "+ stackFrame +" (allowed: 1,2,4,8)");
      };
    • In the Kona version, I changed it back to the conventional switch statement since the arrow syntax used in “switch” only can be complied in jdk 14 or other later version.
      switch (stackFrame) {
      ...
      default:
          throw new RuntimeException("Illegal stack parameter value: " + stackFrame + " (allowed: 1,2,4,8)");
      }

@johnshajiang
Copy link
Collaborator

@HangerLIN
Please remove directory target.

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.23</version>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version 1.37 may be better.

@@ -0,0 +1,171 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

老师我不知道,我为什么把下面的那个您给出的对Worker泛型需要进行修改,的那部分的意见给点掉了。我理解您意思是想要把 workers = new Worker[chans.length - 1];这样的代码修改成 List<Worker<Integer>> workers = new ArrayList<>(chans.size() - 1);这样的代码。但是我在使用前者,也就是loom原本给出来的形式去运行的时候,并没有报错?那我是否还需要修改呢?



public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缩进看不来不太对,是不是把空格转化成tab了?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这好像是我现在写代码的习惯,一般是怎么用空格的来着?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loom原始文件是什么样,这里就应该是什么样。

另外,你需要在comment里加一个git diff,列一下你和loom原始文件的差异

}


public static void main(String[] args) throws RunnerException {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要加这个main函数?


private void startAll() {
for (Worker<Integer> w : workers) {
Thread.ofVirtual().scheduler(Executors.newSingleThreadExecutor()).start(w);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要改? Kona Fiber也有startVirtualThread这个接口

而且即使改,为什么要改成单线程的调度器?

@HangerLIN
Copy link
Author

HangerLIN commented Sep 4, 2023

How to Run the Test
Make sure Java 1.8.0_362_fiber is installed.
Run the command mvn clean packageto build the project.
Run the main method in the class org.example.Ring to start the test.
See the result in the file res.json
changes:
Add jmh dependencies in pom.xml

<dependencies>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-core</artifactId>
        <version>1.37</version>
    </dependency>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-generator-annprocess</artifactId>
        <version>1.37</version>
    </dependency>
</dependencies>

Altered the Method for Starting Workers:

In the Loom version, the getChannel() method used the enhanced switch expression syntax:

return switch(stackFrame) {
...
default -> throw new RuntimeException("Illegal stack parameter value: "+ stackFrame +" (allowed: 1,2,4,8)");
};
In the Kona version, I changed it back to the conventional switch statement since the arrow syntax used in “switch” only can be complied in jdk 14 or other later version.
switch (stackFrame) {
...
default:
    throw new RuntimeException("Illegal stack parameter value: " + stackFrame + " (allowed: 1,2,4,8)");
}

Here is the git diff between the original one and the version I made it adapted to Kona.

git diff /Users/lth/Downloads/loom-fibers/test/micro/org/openjdk/bench/loom/ring/Ring.java /Users/lth/Desktop/OpenSourceRecord/kona_copy/TencentKona-8/demo/fiber/Ring_test/src/main/java/org/example/Ring.java
diff --git a/Users/lth/Downloads/loom-fibers/test/micro/org/openjdk/bench/loom/ring/Ring.java b/Users/lth/Desktop/OpenSourceRecord/kona_copy/TencentKona-8/demo/fiber/Ring_test/src/main/java/org/example/Ring.java
old mode 100755
new mode 100644
index 58f74586..98d50663
--- a/Users/lth/Downloads/loom-fibers/test/micro/org/openjdk/bench/loom/ring/Ring.java
+++ b/Users/lth/Desktop/OpenSourceRecord/kona_copy/TencentKona-8/demo/fiber/Ring_test/src/main/java/org/example/Ring.java
@@ -20,8 +20,10 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
-package org.openjdk.bench.loom.ring;
+package org.example;

+import org.example.Channel;
+import org.example.Channels;
 import org.openjdk.jmh.annotations.Benchmark;
 import org.openjdk.jmh.annotations.BenchmarkMode;
 import org.openjdk.jmh.annotations.Fork;
@@ -92,7 +94,6 @@ public class Ring {
     @Param({"true", "false"})
     public boolean singleshot;

-
     @Setup
     @SuppressWarnings("unchecked")
     public void setup() {
@@ -154,14 +155,18 @@ public class Ring {
     }

     Channel<Integer> getChannel() {
-        return switch(stackFrame) {
-            case 1 -> new Channels.ChannelFixedStackR1<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
-            case 2 -> new Channels.ChannelFixedStackR2<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
-            case 4 -> new Channels.ChannelFixedStackR4<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
-            case 8 -> new Channels.ChannelFixedStackR8<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
-            default -> throw new RuntimeException("Illegal stack parameter value: "+ stackFrame +" (allowed: 1,2,4,8)");
-        };
-
+        switch (stackFrame) {
+            case 1:
+                return new Channels.ChannelFixedStackR1<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
+            case 2:
+                return new Channels.ChannelFixedStackR2<>(getQueue(queue), stackDepth, allocalot ? 4242 : 0);
:

@HangerLIN
Copy link
Author

我已经修改了描述文档,请老师过目

@johnshajiang
Copy link
Collaborator

johnshajiang commented Sep 18, 2023

我只是确定一下,上面的diff的内容是否显示完整了?是否会一些末尾的行没有显示出来?
至少看起来,switch语句中有些条目,如4,8和default,没有列出来。

@HangerLIN
Copy link
Author

我是把原本在终端栏的全部信息都复制上来了

@johnshajiang
Copy link
Collaborator

是不是一屏幕不能显示出全部内容?
结尾处是:,我以为后面还有内容。可以把光标继续向下移动。

@HangerLIN
Copy link
Author

按照和老师会议谈论内容增加了Ring.java以及 Channel.java的实现逻辑和结果

<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少一个换行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants