Skip to content

Commit f3b8d38

Browse files
committed
✨ 更新框架,新增 cached_query_url
1 parent a4402c1 commit f3b8d38

File tree

14 files changed

+146
-73
lines changed

14 files changed

+146
-73
lines changed

frameworks/Java/today/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ http://localhost:8080/db
1717

1818
http://localhost:8080/queries?queries=
1919

20+
### Caching QUERY
21+
22+
http://localhost:8080/cached?count=10
23+
2024
### UPDATE
2125

2226
http://localhost:8080/update?queries=

frameworks/Java/today/benchmark_config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"query_url": "/queries?queries=",
1010
"fortune_url": "/fortunes",
1111
"update_url": "/updates?queries=",
12+
"cached_query_url": "/cached?count=",
1213
"port": 8080,
1314
"approach": "Realistic",
1415
"classification": "Fullstack",
@@ -21,7 +22,7 @@
2122
"webserver": "None",
2223
"os": "Linux",
2324
"database_os": "Linux",
24-
"display_name": "Today",
25+
"display_name": "TODAY",
2526
"notes": "",
2627
"versus": "None"
2728
}

frameworks/Java/today/build.gradle

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
description = "benchmark"
22

3-
apply plugin: "java"
4-
apply plugin: "application"
5-
apply plugin: 'cn.taketoday.application'
6-
apply plugin: 'io.spring.dependency-management'
3+
apply plugin: 'java'
4+
apply plugin: 'application'
5+
apply plugin: 'infra.application'
76

87
configure(allprojects) {
98
group = "cn.taketoday.benchmark"
109

1110
repositories {
1211
mavenCentral()
13-
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
12+
maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
1413
}
1514
}
1615

@@ -24,13 +23,20 @@ dependencies {
2423
implementation 'mysql:mysql-connector-java'
2524

2625
implementation 'ch.qos.logback:logback-classic'
26+
implementation 'com.github.ben-manes.caffeine:caffeine'
2727

2828
implementation('io.netty:netty-transport-native-epoll') {
2929
artifact {
3030
classifier = 'linux-x86_64'
3131
}
3232
}
3333

34+
implementation('io.netty.incubator:netty-incubator-transport-native-io_uring:0.0.21.Final') {
35+
artifact {
36+
classifier = 'linux-x86_64'
37+
}
38+
}
39+
3440
// implementation('io.netty:netty-transport-native-kqueue') {
3541
// artifact {
3642
// classifier = 'osx-aarch_64'
@@ -40,17 +46,20 @@ dependencies {
4046
}
4147

4248
java {
43-
sourceCompatibility = JavaVersion.VERSION_17
44-
targetCompatibility = JavaVersion.VERSION_17
49+
sourceCompatibility = JavaVersion.VERSION_21
50+
targetCompatibility = JavaVersion.VERSION_21
4551
}
4652

4753
application {
4854
mainClass = 'cn.taketoday.benchmark.BenchmarkApplication'
4955
applicationDefaultJvmArgs = [
5056
"-server",
57+
"-Xms2G",
58+
"-Xmx2G",
5159
"-XX:+UseNUMA",
52-
"-XX:+UseG1GC",
53-
"-XX:+DisableExplicitGC",
60+
"-XX:+UseParallelGC",
61+
"-XX:+AggressiveOpts",
62+
"-Djava.lang.Integer.IntegerCache.high=10000",
5463
"-XX:-StackTraceInThrowable",
5564
"-XX:+UseStringDeduplication",
5665
"-Dinfra.profiles.active=test",

frameworks/Java/today/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ urls.db = "/db"
88
urls.query = "/queries?queries="
99
urls.update = "/updates?queries="
1010
urls.fortune = "/fortunes"
11+
urls.cached_query = "/cached?count="
1112
approach = "Realistic"
1213
classification = "Fullstack"
1314
database = "mysql"

frameworks/Java/today/gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
version=1.0.0
2-
#infraVersion=4.0.0-Draft.6-SNAPSHOT
3-
infraVersion=4.0.0-Draft.6
1+
version=1.1.0
2+
#infraVersion=5.0-Draft.2
3+
infraVersion=5.0-Draft.2-SNAPSHOT
44

55
org.gradle.caching=true
66
org.gradle.jvmargs=-Xmx2048m

frameworks/Java/today/src/main/java/cn/taketoday/benchmark/AppConfig.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
import javax.sql.DataSource;
66

7-
import cn.taketoday.beans.factory.annotation.DisableAllDependencyInjection;
8-
import cn.taketoday.beans.factory.config.BeanDefinition;
9-
import cn.taketoday.context.annotation.Configuration;
10-
import cn.taketoday.context.annotation.Role;
11-
import cn.taketoday.framework.web.netty.NettyRequestConfig;
12-
import cn.taketoday.framework.web.netty.SendErrorHandler;
13-
import cn.taketoday.jdbc.RepositoryManager;
14-
import cn.taketoday.jdbc.persistence.EntityManager;
15-
import cn.taketoday.stereotype.Component;
16-
import io.netty.handler.codec.http.DefaultHttpHeadersFactory;
7+
import infra.beans.factory.annotation.DisableAllDependencyInjection;
8+
import infra.beans.factory.config.BeanDefinition;
9+
import infra.context.annotation.Configuration;
10+
import infra.context.annotation.Role;
11+
import infra.jdbc.RepositoryManager;
12+
import infra.persistence.EntityManager;
13+
import infra.stereotype.Component;
14+
import infra.web.server.error.SendErrorHandler;
15+
import infra.web.server.support.NettyRequestConfig;
16+
import io.netty.handler.codec.http.DefaultHttpHeaders;
1717
import io.netty.handler.codec.http.HttpHeaders;
1818
import io.netty.handler.codec.http.HttpHeadersFactory;
1919
import io.netty.handler.codec.http.multipart.DefaultHttpDataFactory;
2020

21-
import static cn.taketoday.http.HttpHeaders.DATE_FORMATTER;
21+
import static infra.http.HttpHeaders.DATE_FORMATTER;
2222

2323
/**
2424
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
@@ -29,42 +29,47 @@
2929
@Configuration(proxyBeanMethods = false)
3030
class AppConfig {
3131

32-
private static final DefaultHttpHeadersFactory headersFactory = DefaultHttpHeadersFactory.headersFactory();
33-
3432
@Component
35-
static RepositoryManager repositoryManager(DataSource dataSource) {
33+
public static RepositoryManager repositoryManager(DataSource dataSource) {
3634
return new RepositoryManager(dataSource);
3735
}
3836

3937
@Component
40-
static EntityManager entityManager(RepositoryManager repositoryManager) {
38+
public static EntityManager entityManager(RepositoryManager repositoryManager) {
4139
return repositoryManager.getEntityManager();
4240
}
4341

4442
@Component
4543
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
46-
static NettyRequestConfig nettyRequestConfig(SendErrorHandler sendErrorHandler) {
44+
public static NettyRequestConfig nettyRequestConfig(SendErrorHandler sendErrorHandler) {
4745
var factory = new DefaultHttpDataFactory(false);
48-
return NettyRequestConfig.forBuilder()
46+
return NettyRequestConfig.forBuilder(false)
4947
.httpDataFactory(factory)
5048
.sendErrorHandler(sendErrorHandler)
5149
.headersFactory(new HttpHeadersFactory() {
5250

5351
@Override
5452
public HttpHeaders newHeaders() {
55-
HttpHeaders headers = headersFactory.newHeaders();
53+
HttpHeaders headers = new ResponseHeaders();
5654
headers.set("Server", "TODAY");
5755
headers.set("Date", DATE_FORMATTER.format(ZonedDateTime.now()));
5856
return headers;
5957
}
6058

6159
@Override
6260
public HttpHeaders newEmptyHeaders() {
63-
return headersFactory.newEmptyHeaders();
61+
return new ResponseHeaders();
6462
}
6563
})
66-
.secure(false)
6764
.build();
6865
}
6966

67+
static class ResponseHeaders extends DefaultHttpHeaders {
68+
69+
public ResponseHeaders() {
70+
super(name -> { }, v -> { });
71+
}
72+
73+
}
74+
7075
}

frameworks/Java/today/src/main/java/cn/taketoday/benchmark/BenchmarkApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cn.taketoday.benchmark;
22

3-
import cn.taketoday.framework.Application;
4-
import cn.taketoday.framework.InfraApplication;
3+
import infra.app.Application;
4+
import infra.app.InfraApplication;
55

66
@InfraApplication
77
public class BenchmarkApplication {

frameworks/Java/today/src/main/java/cn/taketoday/benchmark/http/BenchmarkHttpHandler.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
import java.util.Comparator;
44
import java.util.List;
5-
import java.util.Map;
65
import java.util.Objects;
76
import java.util.concurrent.ThreadLocalRandom;
87
import java.util.stream.IntStream;
98

109
import cn.taketoday.benchmark.model.Fortune;
10+
import cn.taketoday.benchmark.model.Message;
1111
import cn.taketoday.benchmark.model.World;
12-
import cn.taketoday.http.MediaType;
13-
import cn.taketoday.http.ResponseEntity;
14-
import cn.taketoday.jdbc.persistence.EntityManager;
15-
import cn.taketoday.lang.Nullable;
16-
import cn.taketoday.ui.Model;
17-
import cn.taketoday.web.annotation.GET;
18-
import cn.taketoday.web.annotation.RestController;
19-
import cn.taketoday.web.view.ViewRef;
12+
import infra.http.MediaType;
13+
import infra.http.ResponseEntity;
14+
import infra.lang.Nullable;
15+
import infra.persistence.EntityManager;
16+
import infra.ui.Model;
17+
import infra.util.concurrent.Future;
18+
import infra.web.annotation.GET;
19+
import infra.web.annotation.RestController;
20+
import infra.web.view.ViewRef;
2021

2122
/**
2223
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
@@ -30,15 +31,18 @@ final class BenchmarkHttpHandler {
3031

3132
private final EntityManager entityManager;
3233

34+
private final WorldCache worldCache;
35+
3336
BenchmarkHttpHandler(EntityManager entityManager) {
3437
this.entityManager = entityManager;
38+
this.worldCache = new WorldCache(entityManager.find(World.class));
3539
}
3640

3741
@GET("/json")
38-
public ResponseEntity<Map<String, String>> json() {
42+
public ResponseEntity<Message> json() {
3943
return ResponseEntity.ok()
4044
.contentType(MediaType.APPLICATION_JSON)
41-
.body(Map.of("message", "Hello, World!"));
45+
.body(new Message("Hello, World!"));
4246
}
4347

4448
@GET("/plaintext")
@@ -59,17 +63,22 @@ public List<World> queries(@Nullable String queries) {
5963
.toList();
6064
}
6165

66+
@GET("/cached")
67+
public List<World> cachedQueries(@Nullable String count) {
68+
return worldCache.getCachedWorld(parseQueryCount(count));
69+
}
70+
6271
@GET("/updates")
63-
public List<World> updates(@Nullable String queries) {
64-
return randomNumbers()
72+
public Future<List<World>> updates(@Nullable String queries) {
73+
return Future.run(() -> randomNumbers()
6574
.mapToObj(this::findWorldById)
6675
.filter(Objects::nonNull)
6776
.peek(world -> {
6877
world.setRandomNumber(nextInt());
6978
entityManager.updateById(world);
7079
})
7180
.limit(parseQueryCount(queries))
72-
.toList();
81+
.toList());
7382
}
7483

7584
@GET("/fortunes")
@@ -84,7 +93,7 @@ public ViewRef fortunes(Model model) {
8493

8594
@Nullable
8695
private World findWorldById(int id) {
87-
return entityManager.findById(World.class, boxed[id]);
96+
return entityManager.findById(World.class, id);
8897
}
8998

9099
//
@@ -95,12 +104,8 @@ private static IntStream randomNumbers() {
95104
.distinct();
96105
}
97106

98-
private static final Integer[] boxed = IntStream.range(MIN_WORLD_NUMBER, MAX_WORLD_NUMBER + 1)
99-
.boxed()
100-
.toArray(Integer[]::new);
101-
102-
private static Integer nextInt() {
103-
return boxed[ThreadLocalRandom.current().nextInt(MIN_WORLD_NUMBER, MAX_WORLD_NUMBER)];
107+
private static int nextInt() {
108+
return ThreadLocalRandom.current().nextInt(MIN_WORLD_NUMBER, MAX_WORLD_NUMBER);
104109
}
105110

106111
private static int parseQueryCount(@Nullable String textValue) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.taketoday.benchmark.http;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ThreadLocalRandom;
6+
7+
import cn.taketoday.benchmark.model.World;
8+
9+
public class WorldCache {
10+
11+
private final World[] cache;
12+
13+
public WorldCache(List<World> worlds) {
14+
this.cache = worlds.toArray(new World[0]);
15+
}
16+
17+
public List<World> getCachedWorld(int count) {
18+
World[] worlds = this.cache;
19+
int length = worlds.length;
20+
ArrayList<World> ret = new ArrayList<>(count);
21+
ThreadLocalRandom current = ThreadLocalRandom.current();
22+
for (int i = 0; i < count; i++) {
23+
ret.add(worlds[current.nextInt(length - 1)]);
24+
}
25+
return ret;
26+
}
27+
28+
}

frameworks/Java/today/src/main/java/cn/taketoday/benchmark/model/Fortune.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import java.util.Objects;
44

5-
import cn.taketoday.jdbc.persistence.Id;
6-
import cn.taketoday.jdbc.persistence.Table;
5+
import infra.persistence.Id;
6+
import infra.persistence.Table;
77

88
@Table("fortune")
99
public class Fortune {
@@ -13,7 +13,8 @@ public class Fortune {
1313

1414
private String message;
1515

16-
public Fortune() { }
16+
public Fortune() {
17+
}
1718

1819
public Fortune(Integer id, String message) {
1920
this.id = id;

0 commit comments

Comments
 (0)