Pārlūkot izejas kodu

Add a JSON test for using Jersey together with the Grizzly server (note that this should not be taken as representative of using Grizzly directly)

Chris Vest 12 gadi atpakaļ
vecāks
revīzija
c31a1300b9

+ 18 - 0
grizzly-jersey/README.md

@@ -0,0 +1,18 @@
+# Netty Benchmarking Test
+
+This is the Grizzly+Jersey portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+
+### JSON Encoding Test
+* [JSON test source](src/main/java/hello/HelloServerHandler.java)
+
+## Versions
+
+* [Java OpenJDK 1.7.0_09](http://openjdk.java.net/)
+* [Jersey 1.17.1](http://jersey.java.net/)
+* [Grizzly 2.2.16](http://grizzly.java.net/)
+
+## Test URLs
+
+### JSON Encoding Test
+
+    http://localhost:8080/json

+ 0 - 0
grizzly-jersey/__init__.py


+ 11 - 0
grizzly-jersey/benchmark_config

@@ -0,0 +1,11 @@
+{
+  "framework": "grizzly-jersey",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/",
+      "port": 8080,
+      "sort": 30
+    }
+  }]
+}

+ 67 - 0
grizzly-jersey/pom.xml

@@ -0,0 +1,67 @@
+<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/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.techempower</groupId>
+  <artifactId>grizzly-jersey-example</artifactId>
+  <version>0.1</version>
+
+  <packaging>jar</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-server</artifactId>
+      <version>1.17.1</version>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-grizzly2</artifactId>
+      <version>1.17.1</version>
+    </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.core</groupId>
+      <artifactId>jackson-databind</artifactId>
+      <version>2.1.4</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <inherited>true</inherited>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+          <optimize>true</optimize>
+          <debug>false</debug>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-shade-plugin</artifactId>
+        <version>1.5</version>
+        <executions>
+          <execution>
+            <phase>package</phase>
+            <goals>
+              <goal>shade</goal>
+            </goals>
+            <configuration>
+              <transformers>
+                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
+                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                  <mainClass>hello.HelloWebServer</mainClass>
+                </transformer>
+              </transformers>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

+ 21 - 0
grizzly-jersey/setup.py

@@ -0,0 +1,21 @@
+import subprocess
+import sys
+import setup_util
+import os
+
+def start(args):
+  try:
+    subprocess.check_call("mvn clean package shade:shade", shell=True, cwd="grizzly-jersey")
+    subprocess.Popen("java -jar target/grizzly-jersey-example-0.1.jar".rsplit(" "), cwd="grizzly-jersey")
+    return 0
+  except subprocess.CalledProcessError:
+    return 1
+
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'grizzly-jersey' in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 9)
+  return 0

+ 24 - 0
grizzly-jersey/src/main/java/hello/HelloResource.java

@@ -0,0 +1,24 @@
+package hello;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+
+import com.sun.jersey.spi.resource.Singleton;
+
+@Singleton
+@Path("/json")
+public class HelloResource {
+  @GET
+  @Produces(APPLICATION_JSON)
+  public Object hello() {
+    Map<String, String> data = new HashMap<String, String>();
+    data.put("message", "Hello, World!");
+    return data;
+  }
+}

+ 41 - 0
grizzly-jersey/src/main/java/hello/HelloWebServer.java

@@ -0,0 +1,41 @@
+package hello;
+
+import java.net.URI;
+
+import javax.ws.rs.core.UriBuilder;
+
+import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
+import com.sun.jersey.api.core.PackagesResourceConfig;
+import com.sun.jersey.api.core.ResourceConfig;
+
+public class HelloWebServer {
+
+  private final int port;
+
+  public HelloWebServer(int port) {
+    this.port = port;
+  }
+
+  public void run() throws Exception {
+    URI baseUri = getBaseUrl(port);
+    ResourceConfig rc = new PackagesResourceConfig("hello");
+    GrizzlyServerFactory.createHttpServer(baseUri, rc);
+    
+    System.err.print("Server started. Press ENTER to stop.");
+    System.in.read();
+  }
+
+  private static URI getBaseUrl(int port) {
+    return UriBuilder.fromUri("http://localhost/").port(port).build();
+  }
+
+  public static void main(String[] args) throws Exception {
+    int port;
+    if (args.length > 0) {
+      port = Integer.parseInt(args[0]);
+    } else {
+      port = 8080;
+    }
+    new HelloWebServer(port).run();
+  }
+}

+ 55 - 0
grizzly-jersey/src/main/java/hello/JsonMessageBodyWriter.java

@@ -0,0 +1,55 @@
+package hello;
+
+import static javax.ws.rs.core.MediaType.*;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+@Provider
+@Produces(APPLICATION_JSON)
+public class JsonMessageBodyWriter implements MessageBodyWriter<Object> {
+  private static final ObjectMapper mapper = new ObjectMapper();
+
+  @Override
+  public boolean isWriteable(
+      Class<?> type,
+      Type genericType,
+      Annotation[] annotations,
+      MediaType mediaType) {
+    return APPLICATION_JSON_TYPE.equals(mediaType);
+  }
+
+  @Override
+  public long getSize(
+      Object t,
+      Class<?> type,
+      Type genericType,
+      Annotation[] annotations,
+      MediaType mediaType) {
+    return -1; // We can't predict the output size at this point
+  }
+
+  @Override
+  public void writeTo(
+      Object t,
+      Class<?> type,
+      Type genericType,
+      Annotation[] annotations,
+      MediaType mediaType,
+      MultivaluedMap<String, Object> httpHeaders,
+      OutputStream entityStream)
+      throws IOException, WebApplicationException {
+    mapper.writeValue(entityStream, t);
+  }
+}