forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterBenchmark.java
More file actions
173 lines (148 loc) · 5.46 KB
/
RouterBenchmark.java
File metadata and controls
173 lines (148 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package act.route;
import act.BenchmarkBase;
import act.TestBase;
import act.app.ActionContext;
import act.app.App;
import act.handler.RequestHandlerResolver;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.osgl.http.H;
import org.osgl.mvc.result.NotFound;
import org.osgl.util.*;
import play.Play;
import play.plugins.PluginCollection;
import play.vfs.VirtualFile;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import static org.mockito.Mockito.mock;
import static org.osgl.http.H.Method.GET;
import static org.osgl.http.H.Method.POST;
@BenchmarkOptions(warmupRounds = 1, benchmarkRounds = 10)
@Ignore
public class RouterBenchmark extends BenchmarkBase {
private Router router;
private RouteTableRouterBuilder builder;
private ActionContext ctx;
public RouterBenchmark() {
RequestHandlerResolver controllerLookup = new MockRequestHandlerResolver();
router = new Router(controllerLookup, Mockito.mock(App.class));
InputStream is = TestBase.class.getResourceAsStream("/routes");
String fc = IO.readContentAsString(is);
builder = new RouteTableRouterBuilder(fc.split("[\r\n]+"));
builder.build(router);
Play.pluginCollection = new PluginCollection();
URL url = TestBase.class.getResource("/routes");
Play.applicationPath = new File(FastStr.of(url.getPath()).beforeLast('/').toString());
Play.routes = VirtualFile.fromRelativePath("routes");
play.mvc.Router.load("");
}
@Before
public void prepare() {
ctx = ActionContext.create(mock(App.class), mock(H.Request.class), mock(H.Response.class));
}
void osgl(H.Method method, String url, Object... args) {
if (args.length > 0) {
url = S.fmt(url, args);
}
router.getInvoker(method, url, ctx);
}
void play(String method, String url, Object... args) {
if (args.length > 0) {
url = S.fmt(url, args);
}
for (play.mvc.Router.Route route : play.mvc.Router.routes) {
Map<String, String> actArgs = route.matches(method, url, null, null);
if (actArgs != null) {
if (route.action.indexOf("{") > -1) { // more optimization ?
for (String arg : actArgs.keySet()) {
route.action = route.action.replace("{" + arg + "}", actArgs.get(arg));
}
}
if (route.action.equals("404")) {
throw new NotFound(route.path);
}
return;
}
}
throw new NotFound(url);
}
@Test
public void osgl_InvokeBadUrl() {
runTest(true, true, GET, "/badUrl/whatever/%s/abc/136", S.random());
}
@Test
public void play_InvokeBadUrl() {
runTest(false, true, GET, "/badUrl/whatever/%s/abc/136", S.random());
}
@Test
public void osgl_HitAtBeginning() {
runTest(true, GET, "/yemian/%s/", S.random());
}
@Test
public void play_HitAtBeginning() {
runTest(false, GET, "/yemian/%s/", S.random());
}
@Test
public void osgl_HitAtEnding() {
runTest(true, GET, "/adm/weixinyingyong/%s/", S.random());
}
@Test
public void play_HitAtEnding() {
runTest(false, GET, "/adm/weixinyingyong/%s/", S.random());
}
@Test
public void osgl_longStaticUrl() {
runTest(true, POST, "/shuju/yonghu/guanliYonghu/shanchu");
}
@Test
public void play_longStaticUrl() {
runTest(false, POST, "/shuju/yonghu/guanliYonghu/shanchu");
}
@Test
public void osgl_longDynamicUrl() {
runTest(true, POST, "/shuju/tuiguang/%s/mubiao/%s/yemian/%s/remove", S.random(24), S.random(24), N.randInt(20));
}
@Test
public void play_longDynamicUrl() {
runTest(false, POST, "/shuju/tuiguang/%s/mubiao/%s/yemian/%s/remove", S.random(24), S.random(24), N.randInt(20));
}
@Test
public void osgl_badUrl2() {
runTest(true, true, POST, "/shuju/tuiguang/%s/mubiao/%s/yemian/%s/remove", S.random(24), S.random(21), N.randInt(20));
}
@Test
public void play_badUrl2() {
runTest(false, true, POST, "/shuju/tuiguang/%s/mubiao/%s/yemian/%s/remove", S.random(24), S.random(21), N.randInt(20));
}
private void runTest(boolean osgl, H.Method method, String url, Object... fmtArgs) {
runTest(osgl, false, method, url, fmtArgs);
}
private void runTest(boolean osgl, boolean notFoundExpected, H.Method method, String url, Object... fmtArgs) {
url = S.fmt(url, fmtArgs);
if (osgl) {
for (int i = 0; i < 1000 * 100; ++i) {
try {
osgl(method, url);
E.unexpectedIf(notFoundExpected, "should raise NotFound here");
} catch (NotFound e) {
E.unexpectedIf(!notFoundExpected, "should not raise NotFound here");
}
}
} else {
String sMethod = method.name();
for (int i = 0; i < 1000 * 100; ++i) {
try {
play(sMethod, url);
E.unexpectedIf(notFoundExpected, "should raise NotFound here");
} catch (NotFound e) {
E.unexpectedIf(!notFoundExpected, "should not raise NotFound here");
}
}
}
}
}