瀏覽代碼

Kotlin ktor framework (#2669)

* Add kotlin ktor framework

* ktor: add netty server, start, apply shade plugin

* ktor: implement json test

* ktor: implement mysql query test

* ktor: multiple queries

* ktor: add updates test

* ktor: upgrade to latest Kotlin and ktor

* ktor: tune shade plugin

* ktor: setup mysql

* ktor: fix jetty to netty

* ktor: add to travis config

* ktor: use ThreadLocalRandom

* ktor: downgrade mysql driver

* ktor: use hikari pool instead of old c3p0

* ktor: README.md
Sergey Mashkov 8 年之前
父節點
當前提交
54a847e2b6

+ 1 - 0
.travis.yml

@@ -112,6 +112,7 @@ env:
     - "TESTDIR=JavaScript/ringojs"
     - "TESTDIR=JavaScript/sailsjs"
     - "TESTDIR=Kotlin/hexagon"
+    - "TESTDIR=Kotlin/ktor"
     - "TESTDIR=Lua/lapis"
     - "TESTDIR=Lua/octopus"
     - "TESTDIR=Lua/openresty"

二進制
frameworks/Kotlin/ktor/.mvn/wrapper/maven-wrapper.jar


+ 1 - 0
frameworks/Kotlin/ktor/.mvn/wrapper/maven-wrapper.properties

@@ -0,0 +1 @@
+distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip

+ 41 - 0
frameworks/Kotlin/ktor/README.md

@@ -0,0 +1,41 @@
+# Setup
+
+* Java 8
+* MySQL server
+
+# Requirements
+
+* Maven 3
+* JDK 8
+* Kotlin
+* ktor
+* netty 
+* hikariCP
+
+Maven is downloaded automatically via Maven Wrapper script (`mvnw`), add dependencies are specified in `pom.xml` so will be downloaded automatically from maven central and jcenter repositories.
+
+# Deployment
+
+Run maven to build a bundle
+
+```bash
+./mvnw package
+```
+
+Once bundle build complete and mysql server is running you can launch the application
+
+```bash
+java -jar target/tech-empower-framework-benchmark-1.0-SNAPSHOT.jar
+```
+
+Please note that the server holds tty so you may need nohup. See `setup.sh` for details.
+
+# Contact
+
+[Sergey Mashkov](https://github.com/cy6erGn0m)
+
+[Ilya Ryzhenkov](https://github.com/orangy) 
+
+Slack ktor channel https://kotlinlang.slack.com/messages/ktor (you need an [invite](http://slack.kotlinlang.org/) to join)
+
+

+ 30 - 0
frameworks/Kotlin/ktor/benchmark_config.json

@@ -0,0 +1,30 @@
+{
+  "framework": "ktor",
+  "tests": [
+    {
+      "default": {
+        "plaintext_url": "/plaintext",
+        "json_url": "/json",
+        "db_url": "/db",
+        "query_url": "/db?queries=",
+        "update_url": "/updates?queries=",
+
+        "port": 9090,
+        "setup_file": "setup",
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "MySQL",
+        "framework": "ktor",
+        "language": "Kotlin",
+        "orm": "Raw",
+        "platform": "Netty",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "ktor jetty",
+        "notes": "http://github.com/Kotlin/ktor",
+        "versus": "servlet"
+      }
+    }
+  ]
+}

+ 234 - 0
frameworks/Kotlin/ktor/mvnw

@@ -0,0 +1,234 @@
+#!/bin/sh
+# ----------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+# ----------------------------------------------------------------------------
+
+# ----------------------------------------------------------------------------
+# Maven2 Start Up Batch script
+#
+# Required ENV vars:
+# ------------------
+#   JAVA_HOME - location of a JDK home dir
+#
+# Optional ENV vars
+# -----------------
+#   M2_HOME - location of maven2's installed home dir
+#   MAVEN_OPTS - parameters passed to the Java VM when running Maven
+#     e.g. to debug Maven itself, use
+#       set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
+#   MAVEN_SKIP_RC - flag to disable loading of mavenrc files
+# ----------------------------------------------------------------------------
+
+if [ -z "$MAVEN_SKIP_RC" ] ; then
+
+  if [ -f /etc/mavenrc ] ; then
+    . /etc/mavenrc
+  fi
+
+  if [ -f "$HOME/.mavenrc" ] ; then
+    . "$HOME/.mavenrc"
+  fi
+
+fi
+
+# OS specific support.  $var _must_ be set to either true or false.
+cygwin=false;
+darwin=false;
+mingw=false
+case "`uname`" in
+  CYGWIN*) cygwin=true ;;
+  MINGW*) mingw=true;;
+  Darwin*) darwin=true
+           #
+           # Look for the Apple JDKs first to preserve the existing behaviour, and then look
+           # for the new JDKs provided by Oracle.
+           # 
+           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then
+             #
+             # Apple JDKs
+             #
+             export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
+           fi
+           
+           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then
+             #
+             # Apple JDKs
+             #
+             export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
+           fi
+             
+           if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then
+             #
+             # Oracle JDKs
+             #
+             export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
+           fi           
+
+           if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
+             #
+             # Apple JDKs
+             #
+             export JAVA_HOME=`/usr/libexec/java_home`
+           fi
+           ;;
+esac
+
+if [ -z "$JAVA_HOME" ] ; then
+  if [ -r /etc/gentoo-release ] ; then
+    JAVA_HOME=`java-config --jre-home`
+  fi
+fi
+
+if [ -z "$M2_HOME" ] ; then
+  ## resolve links - $0 may be a link to maven's home
+  PRG="$0"
+
+  # need this for relative symlinks
+  while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+      PRG="$link"
+    else
+      PRG="`dirname "$PRG"`/$link"
+    fi
+  done
+
+  saveddir=`pwd`
+
+  M2_HOME=`dirname "$PRG"`/..
+
+  # make it fully qualified
+  M2_HOME=`cd "$M2_HOME" && pwd`
+
+  cd "$saveddir"
+  # echo Using m2 at $M2_HOME
+fi
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched
+if $cygwin ; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME=`cygpath --unix "$M2_HOME"`
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
+fi
+
+# For Migwn, ensure paths are in UNIX format before anything is touched
+if $mingw ; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME="`(cd "$M2_HOME"; pwd)`"
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
+  # TODO classpath?
+fi
+
+if [ -z "$JAVA_HOME" ]; then
+  javaExecutable="`which javac`"
+  if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
+    # readlink(1) is not available as standard on Solaris 10.
+    readLink=`which readlink`
+    if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
+      if $darwin ; then
+        javaHome="`dirname \"$javaExecutable\"`"
+        javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
+      else
+        javaExecutable="`readlink -f \"$javaExecutable\"`"
+      fi
+      javaHome="`dirname \"$javaExecutable\"`"
+      javaHome=`expr "$javaHome" : '\(.*\)/bin'`
+      JAVA_HOME="$javaHome"
+      export JAVA_HOME
+    fi
+  fi
+fi
+
+if [ -z "$JAVACMD" ] ; then
+  if [ -n "$JAVA_HOME"  ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+      # IBM's JDK on AIX uses strange locations for the executables
+      JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+      JAVACMD="$JAVA_HOME/bin/java"
+    fi
+  else
+    JAVACMD="`which java`"
+  fi
+fi
+
+if [ ! -x "$JAVACMD" ] ; then
+  echo "Error: JAVA_HOME is not defined correctly." >&2
+  echo "  We cannot execute $JAVACMD" >&2
+  exit 1
+fi
+
+if [ -z "$JAVA_HOME" ] ; then
+  echo "Warning: JAVA_HOME environment variable is not set."
+fi
+
+CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME=`cygpath --path --windows "$M2_HOME"`
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
+fi
+
+# traverses directory structure from process work directory to filesystem root
+# first directory with .mvn subdirectory is considered project base directory
+find_maven_basedir() {
+  local basedir=$(pwd)
+  local wdir=$(pwd)
+  while [ "$wdir" != '/' ] ; do
+    if [ -d "$wdir"/.mvn ] ; then
+      basedir=$wdir
+      break
+    fi
+    wdir=$(cd "$wdir/.."; pwd)
+  done
+  echo "${basedir}"
+}
+
+# concatenates all lines of a file
+concat_lines() {
+  if [ -f "$1" ]; then
+    echo "$(tr -s '\n' ' ' < "$1")"
+  fi
+}
+
+export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-$(find_maven_basedir)}
+MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
+
+# Provide a "standardized" way to retrieve the CLI args that will 
+# work with both Windows and non-Windows executions.
+MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
+export MAVEN_CMD_LINE_ARGS
+
+WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
+
+exec "$JAVACMD" \
+  $MAVEN_OPTS \
+  -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
+  "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
+  ${WRAPPER_LAUNCHER} $MAVEN_CMD_LINE_ARGS
+

+ 148 - 0
frameworks/Kotlin/ktor/pom.xml

@@ -0,0 +1,148 @@
+<?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/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.jetbrains.ktor</groupId>
+    <artifactId>tech-empower-framework-benchmark</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>org.jetbrains.ktor tech-empower-framework-benchmark</name>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <kotlin.version>1.1.1</kotlin.version>
+        <junit.version>4.12</junit.version>
+
+        <ktor.version>0.3.1</ktor.version>
+        <app.main.class>org.jetbrains.ktor.benchmarks.HelloKt</app.main.class>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.jetbrains.kotlin</groupId>
+            <artifactId>kotlin-stdlib-jre8</artifactId>
+            <version>${kotlin.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains.kotlin</groupId>
+            <artifactId>kotlin-test-junit</artifactId>
+            <version>${kotlin.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains.ktor</groupId>
+            <artifactId>ktor-netty</artifactId>
+            <version>${ktor.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.7</version>
+        </dependency>
+        <dependency>
+            <groupId>com.zaxxer</groupId>
+            <artifactId>HikariCP</artifactId>
+            <version>2.6.1</version>
+        </dependency>
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+            <version>5.1.41</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <sourceDirectory>src/main/kotlin</sourceDirectory>
+        <testSourceDirectory>src/test/kotlin</testSourceDirectory>
+
+        <plugins>
+            <plugin>
+                <groupId>org.jetbrains.kotlin</groupId>
+                <artifactId>kotlin-maven-plugin</artifactId>
+                <version>${kotlin.version}</version>
+                <executions>
+                    <execution>
+                        <id>compile</id>
+                        <phase>compile</phase>
+                        <goals>
+                            <goal>compile</goal>
+                        </goals>
+                        <configuration>
+                            <languageVersion>1.1</languageVersion>
+                            <apiVersion>1.1</apiVersion>
+                            <args>
+                                <arg>-Xcoroutines=enable</arg>
+                            </args>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>test-compile</id>
+                        <phase>test-compile</phase>
+                        <goals>
+                            <goal>test-compile</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>2.4.3</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <minimizeJar>false</minimizeJar>
+                            <createDependencyReducedPom>false</createDependencyReducedPom>
+                            <transformers>
+                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <manifestEntries>
+                                        <Main-Class>${app.main.class}</Main-Class>
+                                        <X-Compile-Source-JDK>1.8</X-Compile-Source-JDK>
+                                        <X-Compile-Target-JDK>1.8</X-Compile-Target-JDK>
+                                    </manifestEntries>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+    <repositories>
+        <repository>
+            <id>ktor-repo</id>
+            <url>https://dl.bintray.com/kotlin/ktor</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+        <repository>
+            <id>jcenter</id>
+            <url>https://jcenter.bintray.com/</url>
+            <releases>
+                <enabled>true</enabled>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
+
+</project>

+ 7 - 0
frameworks/Kotlin/ktor/setup.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+fw_depends java mysql
+
+./mvnw clean package -DskipTests=true
+nohup java -jar target/tech-empower-framework-benchmark-1.0-SNAPSHOT.jar &
+

+ 120 - 0
frameworks/Kotlin/ktor/src/main/kotlin/org/jetbrains/ktor/benchmarks/Hello.kt

@@ -0,0 +1,120 @@
+package org.jetbrains.ktor.benchmarks
+
+import com.google.gson.*
+import com.mysql.jdbc.*
+import com.zaxxer.hikari.*
+import org.jetbrains.ktor.application.*
+import org.jetbrains.ktor.content.*
+import org.jetbrains.ktor.features.*
+import org.jetbrains.ktor.host.*
+import org.jetbrains.ktor.http.*
+import org.jetbrains.ktor.netty.*
+import org.jetbrains.ktor.routing.*
+import org.jetbrains.ktor.util.*
+import java.sql.ResultSet.*
+import java.util.concurrent.*
+import javax.sql.*
+
+
+data class Message(val message: String = "Hello, World!")
+data class World(val id: Int, var randomNumber: Int)
+
+fun main(args: Array<String>) {
+    val (a, b) = hex("726f6f743a736563726574").toString(Charsets.ISO_8859_1).split(":")
+    val gson = GsonBuilder().create()
+    val DbRows = 10000
+    val dbHost = System.getenv("DBHOST")
+    Driver::class.java.newInstance()
+    val pool = hikari(dbHost, a, b)
+
+    embeddedServer(Netty, 9090) {
+        install(DefaultHeaders)
+        routing {
+            get("/plaintext") { call ->
+                call.respond(TextContent("Hello, World!", ContentType.Text.Plain, HttpStatusCode.OK))
+            }
+            get("/json") { call ->
+                call.respond(TextContent(gson.toJson(Message()), ContentType.Application.Json, HttpStatusCode.OK))
+            }
+            get("/db") { call ->
+                pool.connection.use { connection ->
+                    val random = ThreadLocalRandom.current()
+                    val queries = call.queries()
+                    val result = mutableListOf<World>()
+
+                    connection.prepareStatement("SELECT * FROM World WHERE id = ?", TYPE_FORWARD_ONLY, CONCUR_READ_ONLY).use { statement ->
+                        for (i in 1..(queries ?: 1)) {
+                            statement.setInt(1, random.nextInt(DbRows) + 1)
+
+                            statement.executeQuery().use { rs ->
+                                while (rs.next()) {
+                                    result += World(rs.getInt("id"), rs.getInt("randomNumber"))
+                                }
+                            }
+                        }
+
+                        call.respond(TextContent(gson.toJson(when (queries) {
+                            null -> result.single()
+                            else -> result
+                        }), ContentType.Application.Json, HttpStatusCode.OK))
+                    }
+                }
+            }
+            get("/updates") {
+                pool.connection.use { connection ->
+                    val queries = call.queries()
+                    val random = ThreadLocalRandom.current()
+                    val result = mutableListOf<World>()
+
+                    connection.prepareStatement("SELECT * FROM World WHERE id = ?", TYPE_FORWARD_ONLY, CONCUR_READ_ONLY).use { statement ->
+                        for (i in 1..(queries ?: 1)) {
+                            statement.setInt(1, random.nextInt(DbRows) + 1)
+
+                            statement.executeQuery().use { rs ->
+                                while (rs.next()) {
+                                    result += World(rs.getInt("id"), rs.getInt("randomNumber"))
+                                }
+                            }
+                        }
+
+                    }
+
+                    result.forEach { it.randomNumber = random.nextInt(DbRows) + 1 }
+
+                    connection.prepareStatement("UPDATE World SET randomNumber = ? WHERE id = ?").use { updateStatement ->
+                        for ((id, randomNumber) in result) {
+                            updateStatement.setInt(1, randomNumber)
+                            updateStatement.setInt(2, id)
+
+                            updateStatement.executeUpdate()
+                        }
+                    }
+
+                    call.respond(TextContent(gson.toJson(when (queries) {
+                        null -> result.single()
+                        else -> result
+                    }), ContentType.Application.Json, HttpStatusCode.OK))
+                }
+            }
+        }
+    }.start(true)
+}
+
+fun ApplicationCall.queries() = try {
+    request.queryParameters["queries"]?.toInt()?.coerceIn(1, 500)
+} catch (nfe: NumberFormatException) {
+    1
+}
+
+private fun hikari(dbHost: String, a: String, b: String): DataSource {
+    val config = HikariConfig()
+    config.jdbcUrl = "jdbc:mysql://$dbHost:3306/hello_world"
+    config.username = a
+    config.password = b
+    config.addDataSourceProperty("cachePrepStmts", "true")
+    config.addDataSourceProperty("prepStmtCacheSize", "250")
+    config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048")
+    config.driverClassName = Driver::class.java.name
+
+    return HikariDataSource(config)
+}

+ 8 - 0
frameworks/Kotlin/ktor/src/test/kotlin/org/jetbrains/ktor/benchmarks/HelloTest.kt

@@ -0,0 +1,8 @@
+package org.jetbrains.ktor.benchmarks
+
+import org.junit.Test
+import kotlin.test.assertEquals
+
+class HelloTest {
+
+}