Quellcode durchsuchen

Add new dropwizard test using mongodb

danielt vor 11 Jahren
Ursprung
Commit
d57e656fcf

+ 0 - 0
dropwizard-mongodb/__init__.py


+ 25 - 0
dropwizard-mongodb/benchmark_config

@@ -0,0 +1,25 @@
+{
+  "framework": "dropwizard",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 9000,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MongoDB",
+      "framework": "dropwizard",
+      "language": "Java",
+      "orm": "Full",
+      "platform": "Jetty",
+      "webserver": "Jetty",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "dropwizard-mongodb",
+      "notes": "mongodb implementation of dropwizard example",
+      "versus": ""
+    }
+  }]
+}

+ 20 - 0
dropwizard-mongodb/hello-world.yml

@@ -0,0 +1,20 @@
+http:
+  port: 9000
+
+  requestLog:
+
+    # Settings for logging to stdout.
+    console:
+      # If true, log requests to stdout.
+      enabled: false
+
+logging:
+
+  # The default level of all loggers. Can be OFF, ERROR, WARN, INFO, DEBUG, TRACE, or ALL.
+  level: OFF
+
+  console:
+
+    # If true, write log statements to stdout.
+    enabled: false
+

+ 84 - 0
dropwizard-mongodb/pom.xml

@@ -0,0 +1,84 @@
+<?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>com.danielt</groupId>
+    <artifactId>dropwizard-mongodb</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.yammer.dropwizard</groupId>
+            <artifactId>dropwizard-core</artifactId>
+            <version>0.6.2</version>
+        </dependency>
+		<dependency>
+			<groupId>org.mongojack</groupId>
+			<artifactId>mongojack</artifactId>
+			<version>2.0.0</version>
+		</dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <source>1.7</source>
+                    <target>1.7</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>2.3.2</version>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>1.6</version>
+                <configuration>
+                    <createDependencyReducedPom>true</createDependencyReducedPom>
+                    <filters>
+                        <filter>
+                            <artifact>*:*</artifact>
+                            <excludes>
+                                <exclude>META-INF/*.SF</exclude>
+                                <exclude>META-INF/*.DSA</exclude>
+                                <exclude>META-INF/*.RSA</exclude>
+                            </excludes>
+                        </filter>
+                    </filters>
+                </configuration>
+                <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>com.example.helloworld.HelloWorldService</mainClass>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 23 - 0
dropwizard-mongodb/setup.py

@@ -0,0 +1,23 @@
+import subprocess
+import sys
+import setup_util
+from os.path import expanduser
+import os
+
+home = expanduser("~")
+
+def start(args, logfile, errfile):
+    try:
+        subprocess.check_call("mvn clean package;", shell=True, cwd="dropwizard", stderr=errfile, stdout=logfile)
+        subprocess.Popen("java -jar target/dropwizard-mongodb-0.0.1-SNAPSHOT.jar server hello-world.yml", shell=True, cwd="dropwizard", stderr=errfile, stdout=logfile)
+        return 0
+    except subprocess.CalledProcessError:
+        return 1
+def stop(logfile, errfile):
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'hello-world' in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 15)
+  return 0

+ 11 - 0
dropwizard-mongodb/source_code

@@ -0,0 +1,11 @@
+./dropwizard-mongodb/src/main/java/com/example/helloworld/
+./dropwizard-mongodb/src/main/java/com/example/helloworld/db
+./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/resources
+./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/core
+./dropwizard-mongodb/src/main/java/com/example/helloworld/core/World.java
+./dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldConfiguration.java

+ 27 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldConfiguration.java

@@ -0,0 +1,27 @@
+
+package com.example.helloworld;
+
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+
+import org.hibernate.validator.constraints.NotEmpty;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.yammer.dropwizard.config.Configuration;
+
+public class HelloWorldConfiguration
+    extends Configuration
+{
+	@JsonProperty
+    @NotEmpty
+    public String mongohost = "localhost";
+ 
+    @JsonProperty
+    @Min(1)
+    @Max(65535)
+    public int mongoport = 27017;
+ 
+    @JsonProperty
+    @NotEmpty
+    public String mongodb = "hello_world";
+}

+ 47 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/HelloWorldService.java

@@ -0,0 +1,47 @@
+
+package com.example.helloworld;
+
+import java.net.UnknownHostException;
+
+import org.mongojack.JacksonDBCollection;
+
+import com.example.helloworld.core.World;
+import com.example.helloworld.db.MongoHealthCheck;
+import com.example.helloworld.db.MongoManaged;
+import com.example.helloworld.resources.JsonResource;
+import com.example.helloworld.resources.WorldResource;
+import com.mongodb.DB;
+import com.mongodb.Mongo;
+import com.yammer.dropwizard.Service;
+import com.yammer.dropwizard.config.Bootstrap;
+import com.yammer.dropwizard.config.Environment;
+
+public class HelloWorldService
+    extends Service<HelloWorldConfiguration>
+{
+
+  public static void main(String[] args) throws Exception
+  {
+    new HelloWorldService().run(args);
+  }
+
+  @Override
+  public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap)
+  {
+    bootstrap.setName("hello-world");
+  }
+
+  @Override
+  public void run(HelloWorldConfiguration config, Environment environment) throws UnknownHostException
+  {
+    Mongo mongo = new Mongo(config.mongohost, config.mongoport);
+    MongoManaged mongoManaged = new MongoManaged(mongo);
+    environment.manage(mongoManaged);
+    environment.addHealthCheck(new MongoHealthCheck(mongo));
+    DB db = mongo.getDB(config.mongodb);
+    JacksonDBCollection<World, String> worlds = JacksonDBCollection.wrap(db.getCollection("world"), World.class, String.class);
+    environment.addResource(new WorldResource(worlds));
+    environment.addResource(new JsonResource());
+  }
+
+}

+ 40 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/core/World.java

@@ -0,0 +1,40 @@
+
+package com.example.helloworld.core;
+
+import javax.persistence.*;
+
+import org.mongojack.Id;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+@Entity
+@Table(name = "World")
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class World
+{
+  @Id
+  private long id;
+
+  @Column(name = "randomNumber", nullable = false)
+  private long randomNumber;
+
+  public long getId()
+  {
+    return id;
+  }
+
+  public void setId(long id)
+  {
+    this.id = id;
+  }
+
+  public long getRandomNumber()
+  {
+    return this.randomNumber;
+  }
+
+  public void setRandomNumber(long randomNumber)
+  {
+    this.randomNumber = randomNumber;
+  }
+}

+ 21 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoHealthCheck.java

@@ -0,0 +1,21 @@
+package com.example.helloworld.db;
+
+import com.mongodb.Mongo;
+import com.yammer.metrics.core.HealthCheck;
+ 
+public class MongoHealthCheck extends HealthCheck {
+ 
+    private Mongo mongo;
+ 
+    public MongoHealthCheck(Mongo mongo) {
+        super("MongoDBHealthCheck");
+        this.mongo = mongo;
+    }
+ 
+    @Override
+    protected Result check() throws Exception {
+        mongo.getDatabaseNames();
+        return Result.healthy();
+    }
+ 
+}

+ 23 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/db/MongoManaged.java

@@ -0,0 +1,23 @@
+package com.example.helloworld.db;
+
+import com.mongodb.Mongo;
+import com.yammer.dropwizard.lifecycle.Managed;
+ 
+public class MongoManaged implements Managed {
+ 
+    private Mongo mongo;
+ 
+    public MongoManaged(Mongo mongo) {
+        this.mongo = mongo;
+    }
+ 
+    @Override
+    public void start() throws Exception {
+    }
+ 
+    @Override
+    public void stop() throws Exception {
+        mongo.close();
+    }
+ 
+}

+ 30 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/resources/JsonResource.java

@@ -0,0 +1,30 @@
+
+package com.example.helloworld.resources;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+@Path("/json")
+@Produces(MediaType.APPLICATION_JSON)
+public class JsonResource
+{
+  // Response message class (copied from 'servlet' test)
+  public final static class HelloMessage {
+    public final String message;
+
+    public HelloMessage(String m) { message = m; }
+  }
+
+  public JsonResource() { }
+
+  @GET
+  public HelloMessage sayHello()
+  {
+    return new HelloMessage("Hello, World!");
+  }
+}

+ 47 - 0
dropwizard-mongodb/src/main/java/com/example/helloworld/resources/WorldResource.java

@@ -0,0 +1,47 @@
+package com.example.helloworld.resources;
+
+import java.util.Random;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+
+import org.mongojack.DBCursor;
+import org.mongojack.JacksonDBCollection;
+
+import com.example.helloworld.core.World;
+import com.google.common.base.Optional;
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBObject;
+
+@Path("/db")
+@Produces(MediaType.APPLICATION_JSON)
+public class WorldResource
+{
+
+  private JacksonDBCollection<World, String> collection;
+ 
+  public WorldResource(JacksonDBCollection<World, String> collection)
+  {
+    this.collection = collection;
+  }
+
+  @GET
+  public World[] dbTest(@QueryParam("queries") Optional<Integer> queries)
+  {
+    final int totalQueries = queries.or(1);
+    final World[] worlds = new World[queries.or(1)];
+    final Random random = new Random(System.currentTimeMillis());
+
+    for (int i = 0; i < totalQueries; i++)
+    {
+    	DBObject query = new BasicDBObject();
+    	query.put("_id", (random.nextInt(10000) + 1));
+    	DBCursor<World> dbCursor = collection.find(query);
+        worlds[i] = (dbCursor.hasNext()) ? dbCursor.next() : null;
+    }
+    return worlds;
+  }
+}