Measuring of Swift by looking at a simple web service

I ran across a colleague’s article recently and figured that the Swift programming language would be a nice addition to his comparison. In order to remedy this I implemented the admittedly very simple web service in Swift and measured both its performance and size. Then I followed the given structure of the article in terms of how to present my relevant results. You may find these subsequently.

 Implementation effort

A simple web service shall be implemented that adds two numbers and returns the result in some way. The service shall be available via HTTP at a certain address. In order to make this happen with Swift I used the Kitura HTTP server which is available here. The code that implements the web service looks like this:

import Kitura

let router = Router()
router.get("/add/:summand1/and/:summand2") { request, response, next in
    if let summand1 = Int(request.parameters["summand1"] ?? "0") {
        if let summand2 = Int(request.parameters["summand2"] ?? "0") {
            response.status(.OK).send("The sum of \(summand1) and \(summand2) equals \(summand1 + summand2).\n")
            next()
            return
        }
    }
    response.status(.badRequest).send("ERROR: Cannot calculate with non integer values.\n")
    next()
}

Kitura.addHTTPServer(onPort: 8080, with: router)
Kitura.run()

The code is also available on Github.

Measurements

If you read my colleague’s blog post, you may be familiar with the following graphs that I supplemented with my own results.

Lines of code

As you can see below, the Swift package comes very cheap in terms of LOC. The overall count of all self-implemented code lines is 27. That also includes the manifest file (Package.swift) which is not displayed above but can be verified on Github.

loc

 Size

To me there are two interesting values in terms of used disc space:

  1. Size of the resulting artifact
  2. Size of the Docker image the artifact is running in

Concerning 1. the Swift web service finishes second with a total size of 3.426 megabytes right behind the Java implementation for Wildfly.

sizeExecutable

When it comes to the total size of the Docker image (2.) Swift is sorely afflicted with the fact that right now Swift apps (outside iOS) are only supported on MacOS and Ubuntu Linux. There is a Docker image for Kitura (Ubuntu) but unfortunately this is a real heavyweight with a total size of 1.24 gigabytes.

sizeDocker

As a result Swift suffers a crushing defeat in this category. However, this may change in the future, e.g. if Swift could run in an Alpine based Docker container.

Time

In order to assure comparability any time measurements should be done on the same environment. I cannot ensure this right now, so I decided to deliver the time measurement results in a subsequent blog post.

Summary

Pros

  • Swift provides a lightweight alternative for web services.
  • very little memory consumption (artifact only).

Cons

  • Running it inside a Docker container may heavily increase disc space consumption due to the limited platform support.
  • Platform independency is not provided, so you have to run the build on the target system.
Posted in All

3 thoughts on “Measuring of Swift by looking at a simple web service

  1. I had some spare time and adapted the project to use the ubuntu-runtime. It was straight forward and the image weighs now only 283MB.

    Thank you for the hint!

  2. Hi Stefan,

    thanks for your reply. I will have a look into this soon and report my results.

    Best regards,
    /Martin

  3. Have a look on the IBM swift-ubuntu-runtime image ~300 MB not so small as an alpine Image but better than GB 😉

    https://github.com/IBM-Swift/swift-ubuntu-docker

    The ibmcom/swift-ubuntu-runtime image contains only those libraries (.so files) provided by the Swift 3.1.1 RELEASE toolchain that are required to run Swift applications. Note that this image does not contain SwiftPM or any of the build tools used when compiling and linking Swift applications. Hence, the size for the ibmcom/swift-ubuntu-runtime image (~300 MB) is much smaller than that of the ibmcom/swift-ubuntu image. The ibmcom/swift-ubuntu-runtime image is ideal for provisioning your Swift application as an IBM Container on Bluemix.

Comments are closed.