Skip to content

Instantly share code, notes, and snippets.

@akamensky
Last active August 29, 2015 14:05
Show Gist options
  • Save akamensky/9a2a0392e5940021410e to your computer and use it in GitHub Desktop.
Save akamensky/9a2a0392e5940021410e to your computer and use it in GitHub Desktop.
failing test
/*
* Copyright 2014 - Alexey Kamenskiy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xpressj;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import xpressj.route.*;
import xpressj.server.Request;
import xpressj.server.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by akamensky on 7/21/14.
*/
public class RouteMatchingTest {
private static final String[] routePaths = {
"*",
"/",
"/projects/*",
"/projects/*/todos",
"/projects/:projectId",
"/projects/:projectId/todos",
"/projects/:projectId/todos/:todoId",
"/projects/:projectId/files",
"/projects/:projectId/files/:fileid",
"/projects/:projectId/messages",
"/projects/:projectId/messages/:messageId",
"/projects/:projectId/messages/:messageId/replies",
"/projects/:projectId/messages/:messageId/replies/:replyId",
"/projects/:projectId/permissions",
"/projects/:projectId/permissions/:permissionId",
"/users",
"/users/*",
"/users/*/messages",
"/users/:userId",
"/users/:userId/messages",
"/users/:userId/messages/:messageId",
"/repos",
"/repos/:repoId",
"/repos/:repoId/pull_requests",
"/repos/:repoId/pull_requests/:pullRequestId",
"/repos/:repoId/pull_requests/:pullRequestId/comments",
"/repos/:repoId/pull_requests/:pullRequestId/comments/:commentId",
"/repos/:repoId/pull_requests/:pullRequestId/comments/:commentId/replies",
"/repos/:repoId/pull_requests/:pullRequestId/comments/:commentId/replies/:replyId"
};
private static final int COUNT = 100000;
private static final List<TestRequest> requests = new ArrayList<>();
private static PTARouteMatcherImpl ptaRouteMatcher;
private static RouteMatcherImpl routeMatcher;
@BeforeClass
public static void setup() {
routeMatcher = new RouteMatcherImpl(new Configuration());
ptaRouteMatcher = new PTARouteMatcherImpl(new Configuration());
for (String path : routePaths) {
routeMatcher.addRoute("GET", new RouteImplImpl("GET", path, new Route() {
@Override
public void handle(Request request, Response response) {
response.send("test");
}
}));
ptaRouteMatcher.addRoute("GET", path, new Route() {
@Override
public void handle(Request request, Response response) {
response.send("test");
}
});
}
final Random random = new Random();
for (int i = 0; i < COUNT; i++) {
TestRequest req = new TestRequest();
req.path = routePaths[random.nextInt(routePaths.length)].replaceAll("\\/\\*", "/123").replaceAll("\\/\\:\\w+", "/123");
requests.add(i, req);
}
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
@Test
public void doRouteMatcherBenchmark() {
try {
for (int i = 0; i < requests.size(); i++) {
List<RouteImpl> routes1 = routeMatcher.getMatchingRoutes("GET", requests.get(i).path);
List<RouteImpl> routes2 = ptaRouteMatcher.getMatchingRoutes("GET", requests.get(i).path);
Assert.assertEquals(routes1.size(), routes2.size());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static class TestRequest {
public String path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment