Skip to content

Commit 22d6674

Browse files
heixiaoma黑小马
andauthored
[java] Add hserver and hserver-business (#9405)
* hserver framework submit * hserver framework submit * lowercase fix * Required response header missing: Date fix * Date format fix * update hserver version * update hserver threadPool * update hserver * update hserver * update hserver query * update hserver query * Update README.md * update hserver query * update hserver query * update hserver query * update hserver query * update hserver query * update hserver query * Optimal configuration of hserver framework * Optimal configuration of hserver framework * Optimal configuration of hserver framework * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Update pom.xml Upgrade dependencies, optimize performance. * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration and Optimize log printing * Update pom.xml * Update StartApp.java * 1 * 1 * 1 * update jdk version * update version * update version * update version * Update app.properties * Update config.toml * Update benchmark_config.json * Update benchmark_config.json * Update config.toml * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * Upgrade hserver Framework version and optimize configuration * update version * add hserver-business test --------- Co-authored-by: 黑小马 <[email protected]>
1 parent d078293 commit 22d6674

File tree

22 files changed

+552
-16
lines changed

22 files changed

+552
-16
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# HServer Benchmarking Test
2+
This is the HServer portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
3+
4+
### Tests
5+
* [JSON test source](src/main/java/com/test/hserver/controller/TestController.java)
6+
* [Plaintext test source](src/main/java/com/test/hserver/controller/TestController.java)
7+
* [Data-Store test source](src/main/java/com/test/hserver/controller/TestController.java)
8+
* [Data-Update test source](src/main/java/com/test/hserver/controller/TestController.java)
9+
* [Fortunes test source](src/main/java/com/test/hserver/controller/TestController.java)
10+
11+
## Infrastructure Software Versions
12+
13+
* [HServer](https://gitee.com/HServer/HServer)
14+
* [Java OpenJDK 1.8](http://openjdk.java.net/)
15+
16+
## Test URLs
17+
18+
### JSON Encoding Test
19+
20+
http://localhost:8888/json
21+
22+
### Plain Text Test
23+
24+
http://localhost:8888/plaintext
25+
26+
### Data-Store/Database Mapping Test
27+
28+
http://localhost:8888/db?queries=2
29+
30+
### Update Test
31+
32+
http://localhost:8888/updates?queries=2
33+
34+
### Fortunes Test
35+
36+
http://localhost:8888/fortunes
37+
38+
### Query Test
39+
http://localhost:8888/queries?queries=2
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{
3+
"framework": "hserver",
4+
"tests": [
5+
{
6+
"default": {
7+
"db_url": "/db",
8+
"fortune_url": "/fortunes",
9+
"plaintext_url": "/plaintext",
10+
"json_url": "/json",
11+
"query_url": "/queries?queries=",
12+
"update_url": "/updates?queries=",
13+
"port": 8888,
14+
"approach": "Realistic",
15+
"classification": "Fullstack",
16+
"database": "Postgres",
17+
"framework": "hserver",
18+
"language": "Java",
19+
"flavor": "None",
20+
"orm": "Full",
21+
"platform": "hserver",
22+
"webserver": "hserver",
23+
"os": "Linux",
24+
"database_os": "Linux",
25+
"display_name": "hserver-business",
26+
"notes": "",
27+
"versus": "hserver"
28+
}
29+
}
30+
]
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[framework]
2+
name = "hserver-business"
3+
4+
[main]
5+
urls.plaintext = "/plaintext"
6+
urls.json = "/json"
7+
urls.db = "/db"
8+
urls.query = "/queries?queries="
9+
urls.update = "/updates?queries="
10+
urls.fortune = "/fortunes"
11+
approach = "Realistic"
12+
classification = "Fullstack"
13+
os = "Linux"
14+
orm = "Full"
15+
platform = "hserver"
16+
webserver = "hserver"
17+
versus = "hserver"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM maven:3.8.4-openjdk-17-slim as maven
2+
WORKDIR /hserver
3+
COPY pom.xml pom.xml
4+
COPY src src
5+
RUN mvn package
6+
7+
FROM openjdk:17.0.2
8+
WORKDIR /hserver
9+
COPY --from=maven /hserver/target/hserver-1.0.jar app.jar
10+
11+
EXPOSE 8888
12+
13+
CMD ["java", "-jar", "app.jar"]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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>com.test.hserver</groupId>
8+
<artifactId>hserver</artifactId>
9+
<version>1.0</version>
10+
11+
<parent>
12+
<artifactId>hserver-parent</artifactId>
13+
<groupId>cn.hserver</groupId>
14+
<version>3.6.0</version>
15+
</parent>
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<version.hikaricp>3.3.1</version.hikaricp>
19+
<version.postgres>42.7.2</version.postgres>
20+
</properties>
21+
22+
<dependencies>
23+
<!-- 核心依赖-->
24+
<dependency>
25+
<artifactId>hserver</artifactId>
26+
<groupId>cn.hserver</groupId>
27+
</dependency>
28+
<!-- web框架 -->
29+
<dependency>
30+
<artifactId>hserver-plugin-web</artifactId>
31+
<groupId>cn.hserver</groupId>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.zaxxer</groupId>
35+
<artifactId>HikariCP</artifactId>
36+
<version>${version.hikaricp}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.postgresql</groupId>
40+
<artifactId>postgresql</artifactId>
41+
<version>${version.postgres}</version>
42+
</dependency>
43+
</dependencies>
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<artifactId>hserver-plugin-maven</artifactId>
48+
<groupId>cn.hserver</groupId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.test.hserver;
2+
3+
4+
import cn.hserver.HServerApplication;
5+
import cn.hserver.core.ioc.annotation.HServerBoot;
6+
import cn.hserver.core.server.context.ConstConfig;
7+
8+
9+
/**
10+
* @author hxm
11+
*/
12+
@HServerBoot
13+
public class StartApp {
14+
15+
public static void main(String[] args) {
16+
HServerApplication.run(StartApp.class, 8888, args);
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.test.hserver.bean;
2+
3+
public final class Fortune implements Comparable<Fortune> {
4+
public final int id;
5+
6+
public final String message;
7+
8+
public Fortune(int id, String message) {
9+
this.id = id;
10+
this.message = message;
11+
}
12+
@Override
13+
public int compareTo(Fortune other) {
14+
return message.compareTo(other.message);
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public String getMessage() {
22+
return message;
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.test.hserver.bean;
2+
3+
public class Message {
4+
private String message = "Hello, World!";
5+
6+
public String getMessage() {
7+
return message;
8+
}
9+
10+
public void setMessage(String message) {
11+
this.message = message;
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.test.hserver.bean;
2+
3+
public class World implements Comparable<World> {
4+
private final int id;
5+
6+
private final int randomNumber;
7+
8+
public World(int id, int randomNumber) {
9+
this.id = id;
10+
this.randomNumber = randomNumber;
11+
}
12+
13+
public int getId() {
14+
return id;
15+
}
16+
17+
public int getRandomNumber() {
18+
return randomNumber;
19+
}
20+
21+
@Override public int compareTo(World o) {
22+
return id - o.id;
23+
}
24+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.test.hserver.controller;
2+
3+
import cn.hserver.core.ioc.annotation.Autowired;
4+
import cn.hserver.plugin.web.annotation.Controller;
5+
import cn.hserver.plugin.web.annotation.GET;
6+
import cn.hserver.plugin.web.interfaces.HttpRequest;
7+
import cn.hserver.plugin.web.interfaces.HttpResponse;
8+
import com.test.hserver.bean.Fortune;
9+
import com.test.hserver.bean.Message;
10+
import com.test.hserver.bean.World;
11+
import com.test.hserver.util.DateUtil;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import javax.sql.DataSource;
16+
import java.sql.Connection;
17+
import java.sql.PreparedStatement;
18+
import java.sql.ResultSet;
19+
import java.sql.SQLException;
20+
import java.util.*;
21+
22+
import static com.test.hserver.util.Util.getQueries;
23+
import static com.test.hserver.util.Util.randomWorld;
24+
25+
/**
26+
* @author hxm
27+
*/
28+
@Controller
29+
public class TestController {
30+
private static final String HELLO = "Hello, World!";
31+
private static final String SELECT_WORLD = "select * from world where id=?";
32+
33+
@Autowired
34+
private DataSource dataSource;
35+
36+
@GET("/json")
37+
public Message json(HttpResponse response) {
38+
response.setHeader("Date", DateUtil.getTime());
39+
return new Message();
40+
}
41+
42+
@GET("/plaintext")
43+
public String plaintext(HttpResponse response) {
44+
response.setHeader("Date", DateUtil.getTime());
45+
return HELLO;
46+
}
47+
48+
@GET("/db")
49+
public void db(HttpResponse response) throws SQLException {
50+
World result;
51+
try (Connection conn = dataSource.getConnection()) {
52+
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
53+
statement.setInt(1, randomWorld());
54+
try (ResultSet rs = statement.executeQuery()) {
55+
rs.next();
56+
result = new World(rs.getInt("id"), rs.getInt("randomNumber"));
57+
}
58+
}
59+
}
60+
response.setHeader("Date", DateUtil.getTime());
61+
response.sendJson(result);
62+
}
63+
64+
@GET("/queries")
65+
public void queries(HttpRequest request,HttpResponse response) throws Exception {
66+
World[] result = new World[getQueries(request.query("queries"))];
67+
try (Connection conn = dataSource.getConnection()) {
68+
for (int i = 0; i < result.length; i++) {
69+
try (final PreparedStatement statement = conn.prepareStatement(SELECT_WORLD)) {
70+
statement.setInt(1, randomWorld());
71+
try (ResultSet rs = statement.executeQuery()) {
72+
rs.next();
73+
result[i] = new World(rs.getInt("id"), rs.getInt("randomNumber"));
74+
}
75+
}
76+
}
77+
}
78+
response.setHeader("Date", DateUtil.getTime());
79+
response.sendJson(result);
80+
}
81+
82+
83+
@GET("/updates")
84+
public void updates(HttpRequest request,HttpResponse response) throws Exception {
85+
World[] result = new World[getQueries(request.query("queries"))];
86+
StringJoiner updateSql = new StringJoiner(
87+
", ",
88+
"UPDATE world SET randomNumber = temp.randomNumber FROM (VALUES ",
89+
" ORDER BY 1) AS temp(id, randomNumber) WHERE temp.id = world.id");
90+
91+
try (Connection connection = dataSource.getConnection()) {
92+
try (PreparedStatement statement = connection.prepareStatement(SELECT_WORLD)) {
93+
for (int i = 0; i < result.length; i++) {
94+
statement.setInt(1, randomWorld());
95+
try (ResultSet rs = statement.executeQuery()) {
96+
rs.next();
97+
result[i] = new World(rs.getInt("id"), randomWorld());
98+
}
99+
// prepare update query
100+
updateSql.add("(?, ?)");
101+
}
102+
}
103+
104+
try (PreparedStatement statement = connection.prepareStatement(updateSql.toString())) {
105+
int i = 0;
106+
for (World world : result) {
107+
statement.setInt(++i, world.getRandomNumber());
108+
statement.setInt(++i, world.getRandomNumber());
109+
}
110+
statement.executeUpdate();
111+
}
112+
}
113+
response.setHeader("Date", DateUtil.getTime());
114+
response.sendJson(result);
115+
}
116+
117+
@GET("/fortunes")
118+
public void fortunes(HttpResponse response) throws Exception {
119+
List<Fortune> fortunes = new ArrayList<>();
120+
try (Connection connection = dataSource.getConnection()) {
121+
try (PreparedStatement stt = connection.prepareStatement("select * from fortune")) {
122+
try (ResultSet rs = stt.executeQuery()) {
123+
while (rs.next()) {
124+
fortunes.add(new Fortune(rs.getInt("id"), rs.getString("message")));
125+
}
126+
}
127+
}
128+
}
129+
fortunes.add(new Fortune(0, "Additional fortune added at request time."));
130+
Collections.sort(fortunes);
131+
response.setHeader("Date", DateUtil.getTime());
132+
Map<String,Object> data=new HashMap<>();
133+
data.put("data",fortunes);
134+
response.sendTemplate("fortunes.ftl",data);
135+
}
136+
}

0 commit comments

Comments
 (0)