forked from fafeidou/fast-cloud-nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
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
qinfuxiang
committed
Nov 26, 2021
1 parent
80128f2
commit b9e2590
Showing
13 changed files
with
608 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...e-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/ArrayListVsLinkedList.java
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,45 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
|
||
import joptsimple.internal.Strings; | ||
import org.springframework.util.StopWatch; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class ArrayListVsLinkedList { | ||
private final static String DATA_STRING = "DUMMY DATA"; | ||
private final static int MAX_CAPACITY = 1_000_000; | ||
private final static int MAX_ITERATIONS = 1000; | ||
|
||
private static void test(List<String> list) { | ||
for (int i = 0; i < MAX_CAPACITY; i++) { | ||
list.add(DATA_STRING); | ||
} | ||
} | ||
|
||
private static void arrayListPerfTest(int iterations) { | ||
final List<String> list = new ArrayList<>(); | ||
final StopWatch stopWatch = new StopWatch(); | ||
stopWatch.start(); | ||
test(list); | ||
stopWatch.stop(); | ||
System.out.println(stopWatch.prettyPrint()); | ||
} | ||
|
||
private static void linkListPerfTest(int iterations) { | ||
final List<String> list = new LinkedList<>(); | ||
final StopWatch stopWatch = new StopWatch(); | ||
stopWatch.start(); | ||
test(list); | ||
stopWatch.stop(); | ||
System.out.println(stopWatch.prettyPrint()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
arrayListPerfTest(MAX_ITERATIONS); | ||
System.out.println(Strings.repeat('#',100)); | ||
linkListPerfTest(MAX_ITERATIONS); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...source-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample1.java
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,44 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
public class JMHExample1 { | ||
private final static String DATA_STATE = "DUMMY DATA"; | ||
private List<String> arrayList; | ||
private List<String> linkedList; | ||
|
||
@Setup(Level.Iteration) | ||
public void setup() { | ||
arrayList = new ArrayList<>(); | ||
linkedList = new LinkedList<>(); | ||
} | ||
|
||
@Benchmark | ||
public List<String> arrayListAdd() { | ||
arrayList.add(DATA_STATE); | ||
return arrayList; | ||
} | ||
|
||
@Benchmark | ||
public List<String> linkedListAdd() { | ||
linkedList.add(DATA_STATE); | ||
return linkedList; | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder().include(JMHExample1.class.getSimpleName()).forks(1).measurementIterations(10).warmupIterations(10).build(); | ||
new Runner(opts).run(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ource-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample10.java
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,64 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Hashtable; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentSkipListMap; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@Measurement(iterations = 10) | ||
@Warmup(iterations = 5) | ||
@Threads(5) | ||
@State(Scope.Benchmark) | ||
public class JMHExample10 { | ||
@Param({"1", "2", "3", "4"}) | ||
private int type; | ||
|
||
private Map<Long, Long> map; | ||
|
||
@Setup | ||
public void setup() { | ||
switch (type) { | ||
case 1: | ||
map = new ConcurrentHashMap<>(); | ||
break; | ||
case 2: | ||
map = new ConcurrentSkipListMap<>(); | ||
break; | ||
case 3: | ||
map = new Hashtable<>(); | ||
break; | ||
case 4: | ||
map = Collections.synchronizedMap(new HashMap<>()); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("illegal map type."); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public void test() { | ||
map.put(System.nanoTime(), System.nanoTime()); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder() | ||
.include(JMHExample10.class.getSimpleName()) | ||
.forks(1) | ||
// .timeUnit(TimeUnit.NANOSECONDS) | ||
// .measurementIterations(10) | ||
// .warmupIterations(10) | ||
.build(); | ||
new Runner(opts).run(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ource-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample11.java
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 fast.cloud.nacos.codejuc.jmh; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@Measurement(iterations = 10) | ||
@Warmup(iterations = 5) | ||
@Threads(5) | ||
@State(Scope.Thread) | ||
public class JMHExample11 { | ||
//定义了list但是没有初始化 | ||
private List<String> list; | ||
|
||
@Setup | ||
public void setup() { | ||
list = new ArrayList<>(); | ||
} | ||
|
||
@Benchmark | ||
public void measureRight() { | ||
list.add("test"); | ||
} | ||
|
||
@Benchmark | ||
public void measureWrong() { | ||
|
||
} | ||
|
||
@TearDown | ||
public void tearDown() { | ||
assert list.size() > 0 : "muster greeter than zero"; | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder() | ||
.include(JMHExample11.class.getSimpleName()) | ||
.forks(1) | ||
.jvmArgs("-ea") | ||
// .timeUnit(TimeUnit.NANOSECONDS) | ||
// .measurementIterations(10) | ||
// .warmupIterations(10) | ||
.build(); | ||
new Runner(opts).run(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...ource-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample12.java
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,44 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static java.lang.Math.PI; | ||
import static java.lang.Math.log; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@Measurement(iterations = 10) | ||
@Warmup(iterations = 5) | ||
@Threads(5) | ||
@State(Scope.Thread) | ||
public class JMHExample12 { | ||
|
||
@Benchmark | ||
// @CompilerControl(CompilerControl.Mode.EXCLUDE) | ||
public void test1() { | ||
|
||
} | ||
|
||
@Benchmark | ||
// @CompilerControl(CompilerControl.Mode.EXCLUDE) | ||
public void test2() { | ||
log(PI); | ||
} | ||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder() | ||
.include(JMHExample12.class.getSimpleName()) | ||
.forks(1) | ||
// .timeUnit(TimeUnit.NANOSECONDS) | ||
// .measurementIterations(10) | ||
// .warmupIterations(10) | ||
.build(); | ||
new Runner(opts).run(); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...source-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample2.java
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,24 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
public class JMHExample2 { | ||
public void normalMethod() { | ||
|
||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder().include(JMHExample2.class.getSimpleName()).forks(1).measurementIterations(10).warmupIterations(10).build(); | ||
new Runner(opts).run(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...source-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample3.java
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,38 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
@Measurement(iterations = 5) | ||
@Warmup(iterations = 2) | ||
public class JMHExample3 { | ||
@Benchmark | ||
public void test() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@Measurement(iterations = 10) | ||
@Warmup(iterations = 3) | ||
@Benchmark | ||
public void test2() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder() | ||
.include(JMHExample3.class.getSimpleName()) | ||
.forks(1) | ||
// .measurementIterations(10) | ||
// .warmupIterations(10) | ||
.build(); | ||
new Runner(opts).run(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...source-code-analysis/code-juc/src/main/java/fast/cloud/nacos/codejuc/jmh/JMHExample4.java
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,63 @@ | ||
package fast.cloud.nacos.codejuc.jmh; | ||
|
||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@State(Scope.Thread) | ||
@Measurement(iterations = 1) | ||
@Warmup(iterations = 1) | ||
public class JMHExample4 { | ||
@BenchmarkMode(Mode.AverageTime) | ||
@Benchmark | ||
public void testAverageTime() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@BenchmarkMode(Mode.Throughput) | ||
@Benchmark | ||
public void testThroughput() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@BenchmarkMode(Mode.SampleTime) | ||
@Benchmark | ||
public void testSampleTime() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@BenchmarkMode(Mode.SingleShotTime) | ||
@Benchmark | ||
public void testSingleShotTime() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@BenchmarkMode({Mode.AverageTime,Mode.Throughput}) | ||
@Benchmark | ||
public void testAverageTimeAndThroughput() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
@BenchmarkMode(Mode.All) | ||
@Benchmark | ||
public void testAll() throws InterruptedException { | ||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opts = new OptionsBuilder() | ||
.include(JMHExample4.class.getSimpleName()) | ||
.forks(1) | ||
// .measurementIterations(10) | ||
// .warmupIterations(10) | ||
.build(); | ||
new Runner(opts).run(); | ||
} | ||
} |
Oops, something went wrong.