Skip to content

Commit 3e4e2d2

Browse files
author
tanshuai
committed
提交作业
1 parent 4929af1 commit 3e4e2d2

File tree

24 files changed

+955
-0
lines changed

24 files changed

+955
-0
lines changed

Week_02/周六作业/HomeWork/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
*.iml
25+
*.idea/
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+
}
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+
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# netty-gateway
2+
3+
```
4+
5+
6+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.github.kimmking</groupId>
7+
<artifactId>netty-gateway</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>netty-gateway</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.4.RELEASE</version>
18+
<relativePath/>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
29+
<dependency>
30+
<groupId>io.netty</groupId>
31+
<artifactId>netty-all</artifactId>
32+
<version>4.1.45.Final</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>commons-logging</groupId>
37+
<artifactId>commons-logging</artifactId>
38+
<version>1.2</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>1.7.25</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-log4j12</artifactId>
48+
<version>1.7.25</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.apache.httpcomponents</groupId>
52+
<artifactId>httpasyncclient</artifactId>
53+
<version>4.1.4</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.squareup.okhttp3</groupId>
57+
<artifactId>okhttp</artifactId>
58+
<version>4.9.0</version>
59+
</dependency>
60+
61+
<!--
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-web</artifactId>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-test</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
-->
73+
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
86+
</project>

0 commit comments

Comments
 (0)