Created
February 5, 2017 05:17
-
-
Save maximveksler/103124843051ba314dcf20f07b80f02f to your computer and use it in GitHub Desktop.
Benchmark extension for IBM Swift HeliumLogger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LoggerAPI.swift | |
// Collection | |
// | |
// Created by Maxim Veksler on 05/02/2017. | |
// | |
// | |
import Foundation | |
import LoggerAPI | |
import Darwin | |
extension Log { | |
// Thanks | |
// http://stackoverflow.com/questions/25006235/how-to-benchmark-swift-code-execution | |
// https://developer.apple.com/library/content/qa/qa1398/_index.html | |
// https://gist.github.com/jstn/d93c86f7bd2b6f22f0bf | |
// https://gist.github.com/dsheets/f316b6f7d0edbdedaa4007e2f04471df | |
private static func duration(_ tick: UInt64, _ tock: UInt64) -> UInt64 { | |
var info = mach_timebase_info(numer: 0, denom: 0) | |
mach_timebase_info(&info) | |
let numer = UInt64(info.numer) | |
let denom = UInt64(info.denom) | |
return ((tock-tick) * numer) / denom // nanoseconds | |
} | |
private static func log(nanoseconds: UInt64, | |
functionName: String, lineNum: Int, fileName: String) { | |
let milliseconds = nanoseconds / 1_000_000 | |
let seconds = Double(nanoseconds) / 1_000_000_000 | |
Log.info("Operation time: \(milliseconds)ms [\(String(format: "%.2f", seconds)) seconds]", | |
functionName: functionName, | |
lineNum: lineNum, | |
fileName: fileName) | |
} | |
static func benchmark<T>(functionName: String = #function, lineNum: Int = #line, fileName: String = #file, | |
operation: () throws -> T) rethrows -> T { | |
let tick = mach_absolute_time() | |
let t = try operation() | |
let tock = mach_absolute_time() | |
log(nanoseconds: duration(tick, tock), functionName: functionName, lineNum: lineNum, fileName: fileName) | |
return t | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hello World | |
Log.benchmark { | |
print("Hello World") | |
} | |
// [2017-02-05T07:05:03.569+02:00] [INFO] [Scaphold.swift:23 query(graph:variables:)] Operation time: 0ms [0.00 seconds] | |
// GraphQL | |
private static func query(graph: String, variables: String) throws -> JSON { | |
return try Log.benchmark { | |
let client = try! Client(url: "https://us-west-2.api.scaphold.io") | |
let body = "{\"query\": \"\(graph)\", \"variables\": \(variables)}" | |
let contentNegotiation = ContentNegotiationMiddleware(mediaTypes: [.json, .urlEncodedForm], mode: .client) | |
let response = try! client.post("/graphql/trustme-im-from-the-internet", headers: headers, body: Buffer(Array(body.utf8)), middleware: [contentNegotiation]) | |
guard let description = response.content?.description else { | |
Log.error("Did not receive network reply from server") | |
throw ParsingError.RuntimeError("Did not receive network reply from server") | |
} | |
let json = try! Jay.jsonFromString(description) | |
guard json.dictionary?["errors"] == nil else { | |
Log.error("Scaphold operation failed \(json)") | |
throw ParsingError.RuntimeError("Scaphold operation failed") | |
} | |
guard let data = json.dictionary?["data"] else { | |
Log.error("Scaphold operation failed graph:\n\(graph)\nvariables: \(variables)\nresult: \(json)") | |
throw ParsingError.RuntimeError("Scaphold operation failed") | |
} | |
Log.debug("Scaphold, for graph:\n\(graph)\nvariables: \(variables)\nsaid: \(json)") | |
return data | |
} | |
} | |
// [2017-02-05T07:05:04.997+02:00] [INFO] [Scaphold.swift:27 query(graph:variables:)] Operation time: 1428ms [1.43 seconds] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment