浏览代码

Add Dlang Collie (#2627)

* add collie

* remove dub

* add readme

* can not find dub in travis-ci
setup.sh: line 10: dub: command not found

* 8085

* rm dub

* add date to header
渡世白玉 8 年之前
父节点
当前提交
1cd08d6262

+ 1 - 0
.travis.yml

@@ -41,6 +41,7 @@ env:
     - "TESTDIR=Crystal/crystal"
     - "TESTDIR=Crystal/crystal"
     - "TESTDIR=Crystal/kemal"
     - "TESTDIR=Crystal/kemal"
     - "TESTDIR=D/vibed"
     - "TESTDIR=D/vibed"
+    - "TESTDIR=D/collie"
     - "TESTDIR=Dart/dart-raw"
     - "TESTDIR=Dart/dart-raw"
     - "TESTDIR=Dart/redstone"
     - "TESTDIR=Dart/redstone"
     - "TESTDIR=Dart/start"
     - "TESTDIR=Dart/start"

+ 4 - 0
frameworks/D/collie/.gitignore

@@ -0,0 +1,4 @@
+ .directory
+ .dub
+ /collieHttp
+ dub.selections.json

+ 19 - 0
frameworks/D/collie/README.md

@@ -0,0 +1,19 @@
+# Collie Benchmarking Test
+
+This is the netty portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+
+### JSON Encoding Test
+* [JSON test source](collie/source/get.d)
+
+## Requirements
+* Dlang > 2.072
+
+## Test URLs
+
+### JSON Encoding Test
+
+    http://localhost:8080/json
+    
+### PlanText Test
+
+    http://localhost:8080/plaintext

+ 45 - 0
frameworks/D/collie/benchmark_config.json

@@ -0,0 +1,45 @@
+{
+  "framework": "Collie",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8085,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "None",
+      "framework": "Collie",
+      "language": "D",
+      "flavor": "DLang2",
+      "orm": "Raw",
+      "platform": "Collie",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Collie",
+      "notes": "",
+      "versus": "Collie"
+  },
+    "ldc": {
+      "setup_file": "setup_ldc",
+      "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "port": 8085,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "None",
+      "framework": "Collie",
+      "language": "D",
+      "flavor": "DLang2",
+      "orm": "Raw",
+      "platform": "Collie",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "Collie",
+      "notes": "",
+      "versus": "Collie"
+    }
+  }]
+}

+ 10 - 0
frameworks/D/collie/dub.json

@@ -0,0 +1,10 @@
+{
+	"name": "http",
+	"description": "A minimal D application.",
+	"copyright": "Copyright © 2016, dsby",
+	"authors": ["dsby"],
+	"targetType": "executable",
+	"dependencies": {
+            "collie" : "~>0.9.6"
+	}
+}

+ 12 - 0
frameworks/D/collie/setup.sh

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+fw_depends dlang
+
+# Clean any files from last run
+rm -f http
+rm -rf .dub
+rm -f dub.selections.json
+
+dub build -f -b release
+
+./http &

+ 12 - 0
frameworks/D/collie/setup_ldc.sh

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+fw_depends dlang
+
+# Clean any files from last run
+rm -f http
+rm -rf .dub
+rm -f dub.selections.json
+
+dub build -f -b release --compiler=ldc2
+
+./http &

+ 58 - 0
frameworks/D/collie/source/app.d

@@ -0,0 +1,58 @@
+/*
+ * Collie - An asynchronous event-driven network framework using Dlang development
+ *
+ * Copyright (C) 2015-2016  Shanghai Putao Technology Co., Ltd 
+ *
+ * Developer: putao's Dlang team
+ *
+ * Licensed under the Apache-2.0 License.
+ *
+ */
+import std.stdio;
+import std.experimental.logger;
+import std.exception;
+import std.typecons;
+import std.functional;
+import std.parallelism;
+
+import collie.socket;
+import collie.codec.http;
+import collie.codec.http.server;
+
+debug { 
+        extern(C) __gshared string[] rt_options = [ "gcopt=profile:1"];// maxPoolSize:50" ];
+}
+
+
+
+void main()
+{
+	RequestHandler newHandler(RequestHandler,HTTPMessage msg)
+	{
+		import get;
+		import post;
+
+		if(msg.method == HTTPMethod.HTTP_GET)
+			return new GetHandler();
+		if(msg.method == HTTPMethod.HTTP_POST)
+			return new PostHandler();
+		return null;
+	}
+    
+    writeln("Edit source/app.d to start your project.");
+    globalLogLevel(LogLevel.warning);
+    HTTPServerOptions option = new HTTPServerOptions();
+    option.handlerFactories.insertBack(&newHandler);
+    option.threads = totalCPUs;
+    HttpServer server = new HttpServer(option);
+
+    HTTPServerOptions.IPConfig ipconfig ;
+    ipconfig.address = new InternetAddress("0.0.0.0", 8085);
+
+    HTTPServerOptions.IPConfig ipconfig2 ;
+    ipconfig2.address = new Internet6Address("0::0", 8085);
+   
+    server.addBind(ipconfig);
+    server.addBind(ipconfig2);
+    server.start();
+}

+ 65 - 0
frameworks/D/collie/source/get.d

@@ -0,0 +1,65 @@
+module get;
+
+import collie.codec.http;
+import collie.codec.http.server;
+import collie.utils.vector;
+import std.json;
+import std.typecons;
+import std.exception;
+
+import request;
+
+final class GetHandler : BaseHandler
+{
+	override void onEOM() nothrow
+	{
+		collectException({
+				switch(_header.getPath)
+				{
+					case "/json":
+						json();
+						break;
+					case "/plaintext":
+						plaintext();
+						break;
+					default:
+						index();
+						break;
+				}
+			}());
+	}
+
+	void index()
+	{
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])"Hello, World!");
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"text/plain");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+
+	void json()
+	{
+		JSONValue js;
+		js["message"] = "Hello, World!";
+
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])(js.toString));
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"application/json");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+
+	void plaintext()
+	{
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])"Hello, World!");
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"text/plain");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+}
+

+ 65 - 0
frameworks/D/collie/source/post.d

@@ -0,0 +1,65 @@
+module post;
+
+
+import collie.codec.http;
+import collie.codec.http.server;
+import collie.utils.vector;
+import request;
+import std.json;
+import std.typecons;
+import std.exception;
+
+final class PostHandler : BaseHandler
+{
+	override void onEOM() nothrow
+	{
+		collectException({
+				switch(_header.getPath)
+				{
+					case "/json":
+						json();
+						break;
+					case "/plaintext":
+						plaintext();
+						break;
+					default:
+						index();
+						break;
+				}
+			}());
+	}
+	
+	void index()
+	{
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])"Hello, World!");
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"text/plain");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+	
+	void json()
+	{
+		JSONValue js;
+		js["message"] = "Hello, World!";
+		
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])(js.toString));
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"application/json");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+	
+	void plaintext()
+	{
+		auto build = scoped!ResponseBuilder(_downstream);
+		build.status(200,HTTPMessage.statusText(200));
+		build.setBody(cast(ubyte[])"Hello, World!");
+		build.header(HTTPHeaderCode.CONTENT_TYPE,"text/plain");
+		build.header(HTTPHeaderCode.DATE, printDate);
+		build.sendWithEOM();
+	}
+}
+

+ 65 - 0
frameworks/D/collie/source/request.d

@@ -0,0 +1,65 @@
+module request;
+
+import collie.codec.http;
+import collie.codec.http.server;
+import collie.utils.vector;
+import std.exception;
+import std.datetime;
+import std.conv;
+import std.string;
+
+abstract class BaseHandler : RequestHandler
+{
+    alias Buffer = Vector!(ubyte);
+protected:
+	final override void onResquest(HTTPMessage headers) nothrow
+	{
+		_header = headers;
+	}
+
+	final override void onBody(const ubyte[] data) nothrow
+	{
+        collectException(_buffer.put(data));
+	}
+
+	final override void onError(HTTPErrorCode code) nothrow {
+		_erroCode = code;
+		collectException({
+				import collie.utils.memory;
+				if(_header)gcFree(_header);
+				gcFree(this);
+			}());
+	}
+
+	final override void requestComplete() nothrow
+	{
+		_erroCode = HTTPErrorCode.REMOTE_CLOSED;
+		collectException({
+				import collie.utils.memory;
+				if(_header)gcFree(_header);
+				gcFree(this);
+			}());
+	}
+
+	final @property bool isVaild(){return _erroCode == HTTPErrorCode.NO_ERROR;}
+
+	pragma(inline,true)
+	final string printDate() {
+		DateTime date = cast(DateTime)Clock.currTime;
+		return format(
+			"%.3s, %02d %.3s %d %02d:%02d:%02d GMT", // could be UTC too
+			to!string(date.dayOfWeek).capitalize,
+			date.day,
+			to!string(date.month).capitalize,
+			date.year,
+			date.hour,
+			date.minute,
+			date.second);
+	}
+
+protected:
+	HTTPMessage _header;
+    Buffer _buffer;
+private:
+	HTTPErrorCode _erroCode = HTTPErrorCode.NO_ERROR;
+}