Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bringing Swift to the Cloud

Ian Partridge
September 07, 2016

Bringing Swift to the Cloud

Ian Partridge

September 07, 2016
Tweet

More Decks by Ian Partridge

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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/
  5. Why Swift in the cloud? • Runtime characteristics • Performance

    • Memory usage • Developer experience • Fun! 11
  6. 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)
  7. 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
  8. 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
  9. 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:
  10. 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):
  11. 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("<h1>Hello, World!</h1>").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
  12. Developing a Kitura web application 23 import Kitura let router

    = Router() router.get("/hello") { request, response, next in response.status(.OK).send("<h1>Hello, World!</h1>").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:
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. IBM Cloud Tools for Swift • Deployment made easy •

    Clone, code, push • Demo projects to try 32 http://cloudtools.bluemix.net
  21. 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
  22. The future of Kitura? • Next-gen transport protocols • WebSockets

    / socket.io • HTTP/2 • TLS • Backend abstraction through ORM • More performance • Monitoring • Clustering 34
  23. 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