Skip to content

Instantly share code, notes, and snippets.

@arkaitzj
Created March 2, 2020 16:33
Show Gist options
  • Save arkaitzj/cd07ee44873fe9c1a73883888c37da65 to your computer and use it in GitHub Desktop.
Save arkaitzj/cd07ee44873fe9c1a73883888c37da65 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>JerseyTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<jaxrs.version>2.1</jaxrs.version>
<slf4j.version>1.7.26</slf4j.version>
<jersey2.version>2.26</jersey2.version>
<log4j2.version>2.11.2</log4j2.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey2.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.test-framework.providers/jersey-test-framework-provider-inmemory -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>${jersey2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Application;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTestNg;
import org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ProofOfConcept extends JerseyTestNg.ContainerPerClassTest {
@BeforeClass
public static void classSetUp() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
loggerConfig.setLevel(Level.DEBUG);
ctx.updateLoggers();
System.setProperty("jersey.config.test.container.port", "5555");
}
@Override
public TestContainerFactory getTestContainerFactory() {
return new InMemoryTestContainerFactory();
}
@Path("/v2")
public static class TestService {
@ToString
public static class Params {
@QueryParam("instring")
String instring;
@QueryParam("inint")
String inint;
@QueryParam("inint2")
int inint2;
@QueryParam("inint3")
int inint3;
@QueryParam("inint4")
String inint4;
@QueryParam("inint5")
String inint5;
@QueryParam("inint6")
String inint6;
@QueryParam("inint7")
String inint7;
}
@GET
@Path("/test1")
public void test1(
@QueryParam("instring") String instring,
@QueryParam("inint") int inint,
@QueryParam("inint2") int inint2,
@QueryParam("inint3") int inint3,
@QueryParam("inint4") int inint4,
@QueryParam("inint5") int inint5,
@QueryParam("inint6") int inint6,
@QueryParam("inint7") int inint7
) throws InterruptedException {
Thread.sleep(1);
}
@GET
@Path("/test2")
public void test2(@BeanParam Params params) throws InterruptedException {
Thread.sleep(1);
}
}
@Override
protected Application configure() {
ResourceConfig resourceConfig = new ResourceConfig(TestService.class);
return resourceConfig;
}
@Test
public void testFetchAll() {
Builder request1 = target("/v2/test1")
.queryParam("instring","6")
.queryParam( "inint", "3")
.queryParam("inint2", "3")
.queryParam("inint3", "3")
.queryParam("inint4", "3")
.queryParam("inint5", "3")
.queryParam("inint6", "3")
.queryParam("inint7", "3")
.request();
Builder request2 = target("/v2/test2")
.queryParam("instring", "6")
.queryParam("inint", "3")
.queryParam("inint2", "3")
.queryParam("inint3", "3")
.queryParam("inint4", "3")
.queryParam("inint5", "3")
.queryParam("inint6", "3")
.queryParam("inint7", "3")
.request();
long start = System.currentTimeMillis();
log.info("Warming up");
for (int i=0; i<10_000; i++) {
request1.get();
request2.get();
}
log.info("Warmed up");
long times = 10_000;
long reqs = 0;
start = System.currentTimeMillis();
while (reqs < times) {
request1.get();
reqs+=1;
}
float time1 = (System.currentTimeMillis() - start)/1000f;
log.info("Reqs1: {} {} in {}", reqs/time1, reqs, String.format("%.2f",time1));
reqs = 0;
start = System.currentTimeMillis();
while (reqs < times) {
request2.get();
reqs+=1;
}
float time2 = (System.currentTimeMillis() - start)/1000f;
log.info("Reqs2: {} {} in {}", reqs/time2, reqs, String.format("%.2f",time2));
log.info("Req2/Req1 {}", time2/time1);
}
@Test
public void testFetch1() {
Builder request1 = target("/v2/test1").queryParam("instring","6").queryParam( "inint", "3").request();
long start = System.currentTimeMillis();
for (int i=0; i<10_000; i++) {
request1.get();
}
}
@Test
public void testFetch2() {
Builder request2 = target("/v2/test2").queryParam("instring","6").queryParam( "inint", "3").request();
long start = System.currentTimeMillis();
for (int i=0; i<10_000; i++) {
request2.get();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment