Skip to content

Commit 45a5eea

Browse files
committed
initialize
1 parent 18c93bf commit 45a5eea

File tree

6 files changed

+241
-0
lines changed

6 files changed

+241
-0
lines changed

01jvm/GCLogAnalysis.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
import java.util.Random;
3+
import java.util.concurrent.TimeUnit;
4+
import java.util.concurrent.atomic.LongAdder;
5+
/*
6+
演示GC日志生成与解读
7+
*/
8+
public class GCLogAnalysis {
9+
private static Random random = new Random();
10+
public static void main(String[] args) {
11+
// 当前毫秒时间戳
12+
long startMillis = System.currentTimeMillis();
13+
// 持续运行毫秒数; 可根据需要进行修改
14+
long timeoutMillis = TimeUnit.SECONDS.toMillis(1);
15+
// 结束时间戳
16+
long endMillis = startMillis + timeoutMillis;
17+
LongAdder counter = new LongAdder();
18+
System.out.println("正在执行...");
19+
// 缓存一部分对象; 进入老年代
20+
int cacheSize = 2000;
21+
Object[] cachedGarbage = new Object[cacheSize];
22+
// 在此时间范围内,持续循环
23+
while (System.currentTimeMillis() < endMillis) {
24+
// 生成垃圾对象
25+
Object garbage = generateGarbage(100*1024);
26+
counter.increment();
27+
int randomIndex = random.nextInt(2 * cacheSize);
28+
if (randomIndex < cacheSize) {
29+
cachedGarbage[randomIndex] = garbage;
30+
}
31+
}
32+
System.out.println("执行结束!共生成对象次数:" + counter.longValue());
33+
}
34+
35+
// 生成对象
36+
private static Object generateGarbage(int max) {
37+
int randomSize = random.nextInt(max);
38+
int type = randomSize % 4;
39+
Object result = null;
40+
switch (type) {
41+
case 0:
42+
result = new int[randomSize];
43+
break;
44+
case 1:
45+
result = new byte[randomSize];
46+
break;
47+
case 2:
48+
result = new double[randomSize];
49+
break;
50+
default:
51+
StringBuilder builder = new StringBuilder();
52+
String randomString = "randomString-Anything";
53+
while (builder.length() < randomSize) {
54+
builder.append(randomString);
55+
builder.append(max);
56+
builder.append(randomSize);
57+
}
58+
result = builder.toString();
59+
break;
60+
}
61+
return result;
62+
}
63+
}

01jvm/环境准备.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
3+
## Windows
4+
5+
1.管理员身份打开powershell
6+
7+
2.运行
8+
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
9+
10+
3.执行choco install superbenchmarker
11+
12+
4.输入 sb
13+
14+
执行 sb -u http://localhost:8088/api/hello -c 20 -N 60
15+
16+
## Mac
17+
18+
1.执行brew install wrk
19+
如果显式brew update很慢,可以ctrl+C打断更新
20+
21+
2.输入 wrk
22+
23+
执行 wrk -t8 -c40 -d60s http://localhost:8088/api/hello
24+
25+
## 压测程序
26+
27+
1.可以从github获取
28+
git clone https://github.com/kimmking/atlantis
29+
cd atlantis\gateway-server
30+
mvn clean package
31+
然后在target目录可以找到gateway-server-0.0.1-SNAPSHOT.jar
32+
33+
2.也可以从此处下载已经编译好的:
34+
链接:https://pan.baidu.com/s/1NbpYX4M3YKLYM1JJeIzgSQ
35+
提取码:sp85
36+
37+
java -jar -Xmx512m -Xms512 gateway-server-0.0.1-SNAPSHOT.jar
38+

02nio/nio01/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>java0.nio01</groupId>
8+
<artifactId>nio01</artifactId>
9+
<version>1.0</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>8</source>
17+
<target>8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
24+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package java0.nio01;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
8+
public class HttpServer01 {
9+
public static void main(String[] args) throws IOException{
10+
ServerSocket serverSocket = new ServerSocket(8801);
11+
while (true) {
12+
try {
13+
Socket socket = serverSocket.accept();
14+
service(socket);
15+
} catch (IOException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
}
20+
21+
private static void service(Socket socket) {
22+
try {
23+
Thread.sleep(20);
24+
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
25+
printWriter.println("HTTP/1.1 200 OK");
26+
printWriter.println("Content-Type:text/html;charset=utf-8");
27+
String body = "hello,nio";
28+
printWriter.println("Content-Length:" + body.getBytes().length);
29+
printWriter.println();
30+
printWriter.write(body);
31+
printWriter.close();
32+
socket.close();
33+
} catch (IOException | InterruptedException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package java0.nio01;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
8+
public class HttpServer02 {
9+
public static void main(String[] args) throws IOException{
10+
ServerSocket serverSocket = new ServerSocket(8802);
11+
while (true) {
12+
try {
13+
final Socket socket = serverSocket.accept();
14+
new Thread(() -> {
15+
service(socket);
16+
}).start();
17+
} catch (IOException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
}
22+
23+
private static void service(Socket socket) {
24+
try {
25+
Thread.sleep(20);
26+
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
27+
printWriter.println("HTTP/1.1 200 OK");
28+
printWriter.println("Content-Type:text/html;charset=utf-8");
29+
String body = "hello,nio";
30+
printWriter.println("Content-Length:" + body.getBytes().length);
31+
printWriter.println();
32+
printWriter.write(body);
33+
printWriter.close();
34+
socket.close();
35+
} catch (IOException | InterruptedException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package java0.nio01;
2+
3+
import java.io.IOException;
4+
import java.io.PrintWriter;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
10+
public class HttpServer03 {
11+
public static void main(String[] args) throws IOException{
12+
ExecutorService executorService = Executors.newFixedThreadPool(40);
13+
final ServerSocket serverSocket = new ServerSocket(8803);
14+
while (true) {
15+
try {
16+
final Socket socket = serverSocket.accept();
17+
executorService.execute(() -> service(socket));
18+
} catch (IOException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
24+
private static void service(Socket socket) {
25+
try {
26+
Thread.sleep(20);
27+
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
28+
printWriter.println("HTTP/1.1 200 OK");
29+
printWriter.println("Content-Type:text/html;charset=utf-8");
30+
String body = "hello,nio";
31+
printWriter.println("Content-Length:" + body.getBytes().length);
32+
printWriter.println();
33+
printWriter.write(body);
34+
printWriter.close();
35+
socket.close();
36+
} catch (IOException | InterruptedException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)