Browse Source

Added finagle

Falmarri 12 years ago
parent
commit
1f0aeaf72b

BIN
finagle/.cache


+ 31 - 0
finagle/.classpath

@@ -0,0 +1,31 @@
+<classpath>
+  <classpathentry output="target/scala-2.10.0/classes" path="src/main/scala" kind="src"></classpathentry>
+  <classpathentry output="target/scala-2.10.0/classes" path="src/main/java" kind="src"></classpathentry>
+  <classpathentry output="target/scala-2.10.0/test-classes" path="src/test/scala" kind="src"></classpathentry>
+  <classpathentry output="target/scala-2.10.0/test-classes" path="src/test/java" kind="src"></classpathentry>
+  <classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/finagle-http_2.10/jars/finagle-http_2.10-6.2.1.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/finagle-core_2.10/jars/finagle-core_2.10-6.2.1.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/io.netty/netty/bundles/netty-3.5.12.Final.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-core_2.10/jars/util-core_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-collection_2.10/jars/util-collection_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/commons-collections/commons-collections/jars/commons-collections-3.2.1.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-hashing_2.10/jars/util-hashing_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-jvm_2.10/jars/util-jvm_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-logging_2.10/jars/util-logging_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-app_2.10/jars/util-app_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.twitter/util-codec_2.10/jars/util-codec_2.10-6.2.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/commons-codec/commons-codec/jars/commons-codec-1.5.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/commons-lang/commons-lang/jars/commons-lang-2.6.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.fasterxml.jackson.module/jackson-module-scala_2.10/jars/jackson-module-scala_2.10-2.1.3.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.fasterxml.jackson.core/jackson-core/jars/jackson-core-2.1.2.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.fasterxml.jackson.core/jackson-annotations/jars/jackson-annotations-2.1.2.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.fasterxml.jackson.core/jackson-databind/jars/jackson-databind-2.1.2.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.thoughtworks.paranamer/paranamer/jars/paranamer-2.3.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.google.guava/guava/jars/guava-13.0.1.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.10.0.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/com.typesafe.slick/slick_2.10/jars/slick_2.10-1.0.0.jar" kind="lib"></classpathentry>
+  <classpathentry path="/home/dknapp/.ivy2/cache/org.slf4j/slf4j-api/jars/slf4j-api-1.6.4.jar" kind="lib"></classpathentry>
+  <classpathentry path="org.eclipse.jdt.launching.JRE_CONTAINER" kind="con"></classpathentry>
+  <classpathentry path="bin" kind="output"></classpathentry>
+</classpath>

+ 3 - 0
finagle/.gitignore

@@ -0,0 +1,3 @@
+/project/.*
+/project/target
+/target

+ 12 - 0
finagle/.project

@@ -0,0 +1,12 @@
+<projectDescription>
+  <name>finagle</name>
+  <buildSpec>
+    <buildCommand>
+      <name>org.scala-ide.sdt.core.scalabuilder</name>
+    </buildCommand>
+  </buildSpec>
+  <natures>
+    <nature>org.scala-ide.sdt.core.scalanature</nature>
+    <nature>org.eclipse.jdt.core.javanature</nature>
+  </natures>
+</projectDescription>

+ 0 - 0
finagle/__init__.py


+ 15 - 0
finagle/benchmark_config

@@ -0,0 +1,15 @@
+{
+    "framework" : "finagle",
+    "tests" : [{
+        "default" : {
+            "setup_file" : "setup.py",
+            "json_url" : "/json",
+            "db_url" : "/db",
+            "query_url" : "/db?queries=",
+            "port": 8080,
+            "sort": 42
+            }
+        }
+        ]
+
+}

+ 15 - 0
finagle/build.sbt

@@ -0,0 +1,15 @@
+name := "finagle"
+
+organization := "com.falmarri"
+
+scalaVersion := "2.10.0"
+
+version := "1.0"
+
+libraryDependencies += "com.twitter" % "finagle-http_2.10" % "6.+"
+
+libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.10" % "2.+"
+
+libraryDependencies += "com.typesafe.slick" % "slick_2.10" % "1.0.0"
+
+libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.24"

+ 1 - 0
finagle/project/build.properties

@@ -0,0 +1 @@
+sbt.version=0.12.1

+ 1 - 0
finagle/sbt

@@ -0,0 +1 @@
+java  -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy -jar `dirname $0`/sbt-launch.jar "$@"

BIN
finagle/sbt-launch.jar


+ 28 - 0
finagle/setup.py

@@ -0,0 +1,28 @@
+
+import subprocess
+import sys
+import time
+
+def start(args=None):
+
+
+    
+    subprocess.check_call("./sbt update compile", shell=True, cwd="finagle")
+    subprocess.Popen("nohup ./sbt -Ddb.host=" + args.database_host + " run &", cwd="finagle", shell=True)
+    time.sleep(5)
+    return 0
+
+
+
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'sbt' in line:
+      try:
+        pid = int(line.split(None, 2)[1])
+        os.kill(pid, 9)
+      except OSError:
+        pass
+  
+  return 0

+ 101 - 0
finagle/src/main/scala/com/falmarri/finagle/Finagle.scala

@@ -0,0 +1,101 @@
+package com.falmarri.finagle
+
+import com.twitter.finagle.http.{ Request, Response, RichHttp, Http }
+import com.twitter.finagle.builder.{ Server, ServerBuilder }
+import java.net.InetSocketAddress
+import com.twitter.finagle.http.service.RoutingService
+import com.twitter.finagle.Service
+import com.twitter.util.Future
+import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
+import org.jboss.netty.handler.codec.http.DefaultHttpResponse
+import org.jboss.netty.handler.codec.http.HttpResponseStatus
+import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.module.scala.DefaultScalaModule
+import scala.language.experimental.macros
+import scala.reflect.macros.Context
+import java.io.StringWriter
+import org.jboss.netty.util.CharsetUtil.UTF_8
+import scala.slick.driver.MySQLDriver.simple._
+//import slick.session.Database.threadLocalSession
+import scala.util.Random
+
+
+case class World(id: Int, randomNumber: Int)
+
+object Worlds extends Table[World]("World") {
+  def id = column[Int]("id", O.PrimaryKey)
+  def randomNumber = column[Int]("randomNumber")
+  def * = id ~ randomNumber <> (World, World.unapply _)
+  
+}
+
+
+object FinagleBenchmark extends App {
+
+//  def serialize[T](value: T): String = macro serializeImpl[T]
+//
+//  def serializeImpl[T](c: Context)(value: c.Expr[T]): c.Expr[String] = {
+//    import java.io.StringWriter
+//    import c.universe._
+//    c.Expr[String](reify {
+//          val writer = new StringWriter()
+//          mapper.writeValue(writer, value.splice)
+//          writer.toString()
+//    }.tree)
+//  }
+  
+  
+  val database = Database.forURL("jdbc:mysql://" + System.getProperty("db.host", "localhost") + ":3306/hello_world", user = "root", driver="com.mysql.jdbc.Driver")
+  
+  def serialize(value: Any) = {
+    val writer = new StringWriter()
+    mapper.writeValue(writer, value)
+    writer.toString()
+  }
+
+  val mapper = new ObjectMapper()
+  mapper.registerModule(DefaultScalaModule)
+
+  val json = new Service[Request, Response] {
+    def apply(req: Request): Future[Response] ={
+      val resp = Response()
+      resp.setContent(copiedBuffer(serialize(Map("message" -> "Hello, World!")), UTF_8))
+      resp.setContentTypeJson
+      Future.value(resp)
+    }
+
+  }
+
+  val db = new Service[Request, Response] {
+    def apply(req: Request): Future[Response] = {
+      
+      val n = req.params.getIntOrElse("queries", 1)
+      val resp = Response()
+      database withSession {implicit session: Session =>
+        val rand = new Random()
+
+        val q = Query(Worlds).where(_.id inSet( for (i <- 0 to n) yield rand.nextInt(10000)))
+        
+        resp.setContent(copiedBuffer(serialize(if (n == 1) q.first else q.list), UTF_8))
+        
+        resp.setContentTypeJson
+      }
+      Future.value(resp)
+    }
+  }
+
+  val service =
+    RoutingService byPath {
+
+      case "/json" => json
+      case "/db" => db
+
+    }
+
+  val server: Server = ServerBuilder()
+    .codec(RichHttp[Request](Http()))
+    .bindTo(new InetSocketAddress(8080))
+    .name("finagle")
+    .build(service)
+
+}