LoginSignup
14
7

More than 5 years have passed since last update.

めちゃめちゃ早いGoライクなServer Swift SwiftのライブラリProrsumを使ってみた

Last updated at Posted at 2017-01-27

GoライクなServer Swift SwiftのライブラリProrsumの作者のnoppoManさんに

Prorsumのインストールの仕方を教えてもらってサンプルを作ったので共有します。

使ったライブラリ

https://github.com/noppoMan/Prorsum

A Go like concurrent system + networking/http library for Swift that works on Linux and Mac

ベンチマーク結果は以下

benchmark

めっちゃ早くないですか?

3分でできるかもしれないので、以下の手順を試してみましょう!
(注)すでにインストールしていたライブラリのインストール手順が抜けてるかもしれませんん。

Terminal.appを開いて
以下のコマンドを打つ

$ mkdir ProrsumSample
$ cd ProrsumSample
$ swift package init

$ vim Package.swift

Package.swiftを以下のように編集

import PackageDescription

let package = Package(
    name: "ProrsumSample",
    dependencies: [
        .Package(url: "https://github.com/noppoMan/Prorsum.git", majorVersion: 0, minor: 1),
    ]
)

ESC押して、:wqのvimコマンドで、Package.swiftを保存して閉じる

$ swift build
$ vim Sources/main.swift

Sources/main.swiftを以下のように編集する

import Prorsum
import Foundation

let server = try! HTTPServer { (request, writer) in
    do {
        let response = Response(
            headers: ["Server": "Prorsum Micro HTTP Server"],
            body: .buffer("hello Server Side Swift".data)
        )

        try writer.serialize(response)

        writer.close()
    } catch {
        fatalError("\(error)")
    }
}

try! server.bind(host: "0.0.0.0", port: 3000)
print("Server listening at 0.0.0.0:3000")
try! server.listen()

RunLoop.main.run()

ESC押して、:wqのvimコマンドで、Sources/main.swiftを保存して閉じる

Terminal.appを開いて
以下のコマンドを打つ

$ swift build
$ .build/debug/ProrsumSample

最後の.build/debug/ProrsumSampleは、プロジェクト名に合わせて変えてくださいとのこと

Terminal.appで、サーバー立ち上げたまま、Command+Tでタブを開き、

ab -n 10000 -c 100 -g out.data http://127.0.0.1:3000/

ベンチマークを計測したら、Control+Cで、サーバーを終了して、gnuplotをインストールする
http://memorynotfound.com/using-gnuplot-to-plot-apache-benchmark-data/
これを参考にして

$ brew install gnuplot
$ vim apache-benchmark.p

apache-benchmark.p

# output as png image
set terminal png

# save file to "benchmark.png"
set output "benchmark.png"

# graph title
set title "Server Side Swift for Prorsum\nab -n 10000 -c 100 -g out.data http://127.0.0.1:3000/"

#nicer aspect ratio for image size
set size 1,0.7

# y-axis grid
set grid y

#x-axis label
set xlabel "request"

#y-axis label
set ylabel "response time (ms)"

#plot data from "out.data" using column 9 with smooth sbezier lines
plot "out.data" using 9 smooth sbezier with lines title "something"

ESC押して、:wqのvimコマンドで、apache-benchmark.pを保存して閉じる

$ gnuplot apache-benchmark.p
$ open benchmark.png

ソースをXcodeで編集するには

$ swift package generate-xcodeproj
$ ProrsumSample.xcodeproj 

ベンチマーク結果は以下

benchmark

計測したマシンとスペック

2017-01-28 3 58 48

noppoManさんのライブラリと、Tokyo Server Side Swiftの発表資料はこちら
A Go like concurrent system + networking/http library for Swift that works on Linux and Mac

https://github.com/noppoMan/Prorsum

今後に期待ですね!

以上、Tokyo Server Side Swift #6の参加レポートでした

14
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
7