Slide 1

Slide 1 text

Bringing Swift to the Cloud Ian Partridge 7th September 2016

Slide 2

Slide 2 text

2 Ian Partridge • Senior developer in Swift@IBM • Background in runtime systems • Java garbage collection • Runtime performance analysis • Full-stack debugging • Swift@IBM @ianpartridge @alfa

Slide 3

Slide 3 text

3 Today • Swift’s amazing open source journey • Bringing Swift to the Cloud • Getting started with server-side Swift • The future of Swift on Linux

Slide 4

Slide 4 text

Swift’s Amazing Journey 4 Q3 2014 Q1 2016

Slide 5

Slide 5 text

Open Source Swift 5 www.swift.org www.github.com/apple/swift

Slide 6

Slide 6 text

Open, inclusive development community 6 GitHub Satellite 2016 https://youtu.be/DoQiiE5oNOU?t=9m

Slide 7

Slide 7 text

Trusted, permissive licence • Apache Software Licence v2.0 • Third most popular OSS licence • After MIT, GPLv2 • Includes patent licence requirements • Trusted by community and industry 7

Slide 8

Slide 8 text

Building on the shoulders of giants 8

Slide 9

Slide 9 text

Language evolution process 9

Slide 10

Slide 10 text

IBM Swift Sandbox • Interactive sandbox for exploring Swift • Saves your work • Supports multiple versions of Swift • Deploys your Swift code in a Linux Docker container 10 https://swiftlang.ng.bluemix.net/

Slide 11

Slide 11 text

Why Swift in the cloud? • Runtime characteristics • Performance • Memory usage • Developer experience • Fun! 11

Slide 12

Slide 12 text

Swift Performance

Slide 13

Slide 13 text

Swift Memory Usage

Slide 14

Slide 14 text

Swift Performance by Memory

Slide 15

Slide 15 text

Why does this matter? GB/hr 15

Slide 16

Slide 16 text

Apple Client Deployment Server/Cloud Deployment Application-Specific Cloud Services Client Facing App Bringing Swift to the Server Foundation Swift Swift Standard Library Core Foundation Dispatch PWQ C libs GLibc Foundation Swift Swift Standard Library Core Foundation Dispatch Darwin C libs Client-specific Libraries App Libraries Server-specific Libraries App Libraries Driving Towards Consistent Runtime across Clients/Servers Server-side Environments (Built with Foundation & Libdispatch)

Slide 17

Slide 17 text

Growing server-side ecosystem 17

Slide 18

Slide 18 text

Kitura Web Framework http://github.com/IBM-Swift/Kitura 18 • Open source web framework for Swift on macOS and Linux • Inspired by Express for Node.js • Flexible routing • Pluggable middlewares • Easy to deploy

Slide 19

Slide 19 text

Kitura Architecture 19 Web Framework Web Framework Source C Helpers & Interfaces Swift Libraries SwiftyJSON SwiftMongoDB Swift Binary Foundation Package Manager C Libraries Dispatch HttpParser HiRedis CURL PCRE2 Pluggable Components

Slide 20

Slide 20 text

Developing a Kitura web application 20 myProject ├── Package.swift ├── Sources │ └── main.swift └── Tests $ mkdir myProject && cd myProject 2. Next, initialize this project as a new Swift package 1. First, we create a new project directory $ swift package init --type executable Basic Swift package directory structure:

Slide 21

Slide 21 text

Developing a Kitura web application 21 import PackageDescription let package = Package( name: "myProject", dependencies: [ .Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 0, minor: 28) ] ) 3. Add Kitura as a dependency for your project (Package.swift):

Slide 22

Slide 22 text

Developing a Kitura web application 22 Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() let router = Router() router.get("/hello") { request, response, next in response.status(.OK).send("

Hello, World!

").end() } 6. Create and start an HTTPServer: 4. Add a router and a path: router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } 5. Add a JSON data route

Slide 23

Slide 23 text

Developing a Kitura web application 23 import Kitura let router = Router() router.get("/hello") { request, response, next in response.status(.OK).send("

Hello, World!

").end() } router.get("/hello.json") { request, response, next in response.status(.OK).send(json: JSON(["Hello": "World!"])).end() } Kitura.addHTTPServer(onPort: 8090, with: router) Kitura.run() 7. Sources/main.swift should now look like this:

Slide 24

Slide 24 text

Developing a Kitura web application 24 $ swift build 8. Compile: $ .build/debug/myFirstProject & 9. Run: 10. Open your browser: http://localhost:8090/hello http://localhost:8090/ http://localhost:8090/hello.json

Slide 25

Slide 25 text

Get started quickly with Kitura • http://github.com/IBM-Swift/Kitura-Sample • `make run` • http://github.com/IBM-Swift/Kitura-Starter-Bluemix • `cf push` • http://github.com/IBM-Swift/swift-watson-sdk 25

Slide 26

Slide 26 text

Adding a backend Apps need persistence – we can help you! • Apache Cassandra • https://github.com/IBM-Swift/Kassandra • Redis • https://github.com/IBM-Swift/Kitura-redis • CouchDB • https://github.com/IBM-Swift/Kitura-CouchDB 26

Slide 27

Slide 27 text

Adding authentication via OAuth 2.0 • HTTP Basic Auth • https://github.com/IBM-Swift/Kitura-CredentialsHTTP • Facebook • https://github.com/IBM-Swift/Kitura-CredentialsFacebook • Google • https://github.com/IBM-Swift/Kitura-CredentialsGoogle • GitHub • https://github.com/IBM-Swift/Kitura-CredentialsGitHub 27

Slide 28

Slide 28 text

0 10 20 30 40 50 60 70 80 90 100 0 5 10 15 20 25 30 35 0.22.0 0.24.0 0.26.0 0.28.0 0.28.1 0.29.0 CPU (% usage) Throughput (k req/sec) Throughput and CPU - macOS Throughput CPU Kitura performance - macOS 28 Steady improvements – as well as improvement attributed to the runtime 1. HTTP keepalive implementation, removal of unnecessary closures 2. 06-20 to 07-25 snapshot migration 3. Avoid reallocation of dictionaries for HTTP Headers 4. Faster HTTP date construction, Replace some internal uses of Data with UnsafeRawPointer + length offsets, Use multiple Dispatch queues for socket I/O Hardware: MacBook Pro mid-2015, 2.5ghz Core i7 Benchmark: simple ‘Hello World’ plaintext response 1 2 3 4

Slide 29

Slide 29 text

40 50 60 70 80 90 100 0 10 20 30 40 50 60 0.10.0 0.12.0 0.14.0 0.16.0 0.20.0 0.22.0 0.24.0 0.26.0 0.28.0 0.28.1 0.29.0 CPU (% usage) Throughput (k req/sec) Throughput and CPU - Linux Throughput CPU Kitura performance - Linux 29 Steady improvements, but also some challenges 1. Reduce network fragmentation 2. Avoid self-referencing closures 3. Avoid CFGetConverter bottleneck 4. Loss of whole-module-optimization 5. HTTP keepalive implementation 6. 06-20 to 07-25 snapshot migration 7. Avoid reallocation of dictionaries for HTTP Headers 8. Faster HTTP date construction, Replace some internal uses of Data with UnsafeRawPointer + length offsets, Use multiple Dispatch queues for socket I/O Hardware: Lenovo x3550 M5, Haswell 2.6GHz (using 4 cores) Benchmark: simple ‘Hello World’ plaintext response 1 2 3 4 5 6 7 8

Slide 30

Slide 30 text

BluePic demo app https://github.com/IBM-Swift/BluePic 30

Slide 31

Slide 31 text

TodoList demo app • A shared example to showcase backend tech stacks • http://todobackend.com/ • An example using Kitura to develop a Todo-Backend • https://github.com/IBM-Swift/Kitura-TodoList 31

Slide 32

Slide 32 text

IBM Cloud Tools for Swift • Deployment made easy • Clone, code, push • Demo projects to try 32 http://cloudtools.bluemix.net

Slide 33

Slide 33 text

IBM Swift Package Catalog 33 http://swiftpkgs.ng.bluemix.net • Create, share and discover new Swift packages • Explore dependencies • Check licenses • Star your favourite packages

Slide 34

Slide 34 text

The future of Kitura? • Next-gen transport protocols • WebSockets / socket.io • HTTP/2 • TLS • Backend abstraction through ORM • More performance • Monitoring • Clustering 34

Slide 35

Slide 35 text

The future of Swift on Linux? • Completing the implementation of Foundation • Libdispatch performance and scalability improvements • Building a vibrant Swift on Linux community • Server-side packages in the rapidly-growing SPM ecosystem • IDE support • More cloud integration • Exploring the Swift “sweet-spot” • Linux-driven enhancements to Swift itself? 35

Slide 36

Slide 36 text

Find out more 36 https://developer.ibm.com/swift/ The Swift@IBM devCenter Join IBM Swift Engineering and learn the latest news