Skip to content

Commit

Permalink
Merge pull request #174 from qutheory/travis-wrk
Browse files Browse the repository at this point in the history
wrk improvements
  • Loading branch information
tanner0101 committed Apr 9, 2016
2 parents ae65c9b + 47170c2 commit 58ad22c
Show file tree
Hide file tree
Showing 22 changed files with 3,095 additions and 2,825 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -12,4 +12,4 @@ script:
- rm -rf Packages/*/Tests
- swift build
- swift build --configuration release
- swift test
- swift test
11 changes: 9 additions & 2 deletions Package.swift
Expand Up @@ -13,14 +13,15 @@ let package = Package(
.Package(url: "https://github.com/Zewo/JSON.git", majorVersion: 0, minor: 4),

//Swift wrapper around Sockets, used for built-in HTTP server
.Package(url: "https://github.com/ketzusaka/Hummingbird.git", majorVersion: 1, minor: 1),
.Package(url: "https://github.com/qutheory/Hummingbird.git", majorVersion: 2, minor: 0),

//SHA2 + HMAC hashing. Used by the core to create session identifiers.
.Package(url: "https://github.com/CryptoKitten/HMAC.git", majorVersion: 0, minor: 3),
.Package(url: "https://github.com/CryptoKitten/SHA2.git", majorVersion: 0, minor: 1)
],
exclude: [
"XcodeProject",
"Sources/Generator"
],
targets: [
Target(
Expand All @@ -30,7 +31,13 @@ let package = Package(
]
),
Target(
name: "VaporDev",
name: "Development",
dependencies: [
.Target(name: "Vapor")
]
),
Target(
name: "Performance",
dependencies: [
.Target(name: "Vapor")
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions Sources/Performance/main.swift
@@ -0,0 +1,9 @@
import Vapor

let app = Application()

app.get("plaintext") { request in
return "Hello, world!"
}

app.start()
30 changes: 20 additions & 10 deletions Sources/Vapor/Server/HTTPStream.swift
Expand Up @@ -10,7 +10,7 @@ protocol HTTPStream: Stream {

func listen() throws

func receiveByte() throws -> Byte?
func receiveByte() throws -> Byte
func receiveLine() throws -> String

func sendHeaderEndOfLine() throws
Expand All @@ -26,22 +26,32 @@ protocol HTTPStream: Stream {
}

extension HTTPStream {
func receiveByte() throws -> Byte? {
return try receive(max: 1).first
func receiveByte() throws -> Byte {
repeat {
if let byte = try receive(max: 1).first {
return byte
}
} while true
}

func receiveLine() throws -> String {
var line: String = ""
func append(byte: Byte) {
guard byte >= minimumValidAsciiCharacter else { return }

func append(byte: Byte?) {
guard let byte = byte else {
return
}
guard byte >= minimumValidAsciiCharacter else {
return
}

line.append(Character(byte))
}

while
let next = try receiveByte()
where next != newLine
&& !closed {
append(next)
var byte: Byte?
while byte != newLine && !closed {
byte = try receiveByte()
append(byte)
}

return line
Expand Down
62 changes: 28 additions & 34 deletions Sources/Vapor/Server/HTTPStreamServer.swift
Expand Up @@ -18,7 +18,6 @@ class HTTPStreamServer<StreamType: HTTPStream>: Server {
var delegate: Responder!

func serve(responder: Responder, on host: String, at port: Int) throws {
halt()
self.delegate = responder

try stream.bind(to: host, on: port)
Expand All @@ -40,40 +39,35 @@ class HTTPStreamServer<StreamType: HTTPStream>: Server {
}

private func handle(socket: HTTPStream) {
do {
try Background {
var keepAlive = false
repeat {
let request: Request
do {
request = try socket.receive()
} catch {
Log.error("Error receiving request: \(error)")
return
}

let response: Response
do {
response = try self.delegate.respond(request)
} catch {
Log.error("Error parsing response: \(error)")
return
}

do {
try socket.send(response, keepAlive: keepAlive)
} catch {
Log.error("Error sending response: \(error)")
}

keepAlive = request.supportsKeepAlive
} while keepAlive && !socket.closed

socket.close()
var keepAlive = false

repeat {
let request: Request
do {
request = try socket.receive()
} catch {
Log.error("Error receiving request: \(error)")
return
}
} catch {
Log.error("Error accepting request in background: \(error)")
}

let response: Response
do {
response = try self.delegate.respond(request)
} catch {
Log.error("Error parsing response: \(error)")
return
}

do {
try socket.send(response, keepAlive: keepAlive)
} catch {
Log.error("Error sending response: \(error)")
}

keepAlive = request.supportsKeepAlive
} while keepAlive && !socket.closed

socket.close()
}

}
Expand Down
25 changes: 0 additions & 25 deletions Sources/Vapor/Server/Vapor+Hummingbird.swift
Expand Up @@ -20,29 +20,4 @@ extension Hummingbird.Socket: HTTPStream {
try listen(pendingConnectionBacklog: 100)
}

enum Error: ErrorProtocol {
case Unsupported
}

public var closed: Bool {
return false
}

public func close() -> Bool {
return false
}

public func receive(max maxBytes: Int) throws -> Data {
let bytes: [Byte] = try self.receive(maximumBytes: maxBytes) ?? []
return Data(bytes)
}

public func send(data: Data) throws {
try self.send(data.bytes)
}

public func flush() throws {
throw Error.Unsupported
}

}
6 changes: 4 additions & 2 deletions Tests/Vapor/ConfigTests.swift
Expand Up @@ -13,9 +13,11 @@ class ConfigTests: XCTestCase {
}

#if Xcode
let workDir = "/Users/tanner/Developer/vapor/vapor/Sources/VaporDev/"
//Xcode doesn't allow a working directory to be set, so this needs to be
//hardcoded unfortunately.
let workDir = "/Users/tanner/Developer/vapor/vapor/Sources/Development/"
#else
let workDir = "Sources/VaporDev/"
let workDir = "Sources/Development/"
#endif

func testSimple() {
Expand Down

0 comments on commit 58ad22c

Please sign in to comment.