A simple Swift package for measuring and reporting the time taken for operations

Related tags

Logging Duration
Overview

Build Status

Duration

A simple Swift package for measuring and reporting the time taken for operations. It is derived from a version for Playgrounds that I blogged about some time ago, but kept needing to use in various projects.

License

Duration is distributed under the Apache 2.0 license

Usage

Duration supports iOS, OS X, tvOS, and Linux.

Installation

Install via the Swift Package Manager

Add the following to your Package.swift

	.Package(url: "https://github.com/SwiftStudies/Duration.git", majorVersion: 1)

Simple Measurements

There are two methods for simple timing measurements. You can insert calls before and after the section of code you wish to measure, for example

	Duration.startMeasurement("Tough Math")
	
	yourToughMathStuff()
	
	Duration.stopMeasurement()

Or you can use a block

	Duration.measure("Tough Math"){
		yourToughMathStuff()
	}

Or even

	Duration.measure("Tough Math", block: yourToughMathStuff)

In all cases (by default) you will get the output (assuming it took 243 milliseconds)

Tough Math took: 243ms

If measurements are nested, they will be appropriately indented in the output, for example if yourToughMath() made a measurement of part of its code you would see

Measuring Tough Math:
	Part 1 took: 100ms
	Part 2 took: 143ms
Tough Math took: 243ms

Understanding Performance Deviations

In order to better understand how your code is impacted by other things the system is doing you can get average times and standard deviations for block based measurements by supplying a number of iterations for the block, so

	Duration.measure("Tough Math", iterations: 10, forBlock:myToughMath)

Would run the block 10 times, taking and reporting the 10 individual measurements and then the average time taken for the block, together with the standard deviation

Measuring Tough Math
	Iteration 1 took: 243ms
	Iteration 2 took: 242ms
	...
	Iteration 10 took: 243ms
Tough Math Average: 243ms
Tough Math STD Dev.: 1ms

Stopping Report Generation

Because you may want to stop reporting of measurements in release builds, you can set the logStyle variable in order to control the logging behavior

	Duration.logStyle = .None

Will disable measurement logging. In the future I will extend this library to support logging to a data-structure for subsequent analysis, but at this point there are two valid values .None and .Print

If you are using Duration within a Package of your own that you are distributing, rather than just over-writing the log style, you can push your desired style, then pop it to restore it to what a consuming package would want. For example

	public func myMethod(){
		//Because this is a release of your package
		//don't log measurements
		pushLogStyle(.None)
		
		// Do stuff that is instrumented
		
		//Restore the logging style to whatever it was
		//before
		popLogStyle()
	}

Reporting Issues

Please report issues using GitHub's standard system

You might also like...
Simple logging for simples needs.

Simple logging for simples needs.

A Simple iOS Testing Framework

LumberMill Stupidly Simple Logging for iOS. LumberMill is a simple logging library for iOS 10 and Swift 3.0. It allows users to Log files with 5 diffe

Simple network activity logger for iOS
Simple network activity logger for iOS

Reqres is a simple library for logging all requests and responses in your app. It supports Alamofire and also requests made via native NSURLSession. ⬆

A simple, straightforward logging API.

Sequoia A simple, straightforward logging API. Install Add the following to your Package.swift file: import PackageDescription let package = Package(

A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number. A lightweight Swift logger, uses `print` in development and `NSLog` in production. Support colourful and formatted output.
A lightweight Swift logger, uses `print` in development and `NSLog` in production. Support colourful and formatted output.

Loggerithm A lightweight Swift logger, uses print in Debug and NSLog in Production with colourful output. Why In Swift, we usually use print to log in

Easy to use and lightweight logger for iOS, macOS, tvOS, watchOS and Linux in Swift.
Easy to use and lightweight logger for iOS, macOS, tvOS, watchOS and Linux in Swift.

Lighty Easy to use and lightweight logger for iOS, macOS, tvOS, watchOS and Linux in Swift. Screenshots Requirements Lighty Version Minimum iOS Target

Elegant and extensive logging facility for OS X & iOS (includes database, Telnet and HTTP servers)

Overview XLFacility, which stands for Extensive Logging Facility, is an elegant and powerful logging facility for OS X & iOS. It was written from scra

Logging utility for Swift and Objective C
Logging utility for Swift and Objective C

Swell - Swift Logging A logging utility for Swift and Objective C. ##Features Turn on logging during development, turn them off when building for the

Comments
  • Fix for Xcode 11

    Fix for Xcode 11

    Hello, I updated the repo for using Xcode 11 and Swift 5.2. I regenerate Swift Package using Xcode 11 and copy some files such as LICENSE, travis.yml and README.md. Please check and merge this PR if possible ;)

    opened by 0x0c 5
  • Installation section

    Installation section

    Hey @SwiftStudies, your library is really interesting.

    The only problem I found was the README.md, which is a little bit confusing. I created this iOS Open source Readme Template so you can take a look on how to better organize. If you want, I can help you to organize the lib.

    What are your thoughts?

    opened by lfarah 2
  • How do you solve the concurrency problem?

    How do you solve the concurrency problem?

    If multiple threads use the "startMeasurement" func at the same time, the corresponding tuple will not be obtained when the "timingStack.removeLast()" of "stopMeasurement" is executed

    opened by 547837 1
  • Swift 3.1

    Swift 3.1

    Updated to use swift 3.1

    Just because logStyle is none, doesn't mean we shouldn't calculate duration. Previously, if logStyle was .none, then all the measurements return 0. Which basically means that if you didn't want duration data to be printed automatically, then you couldn't use this package at all. That was stupid. Now, if logStyle is .none, it still calculates and returns the duration, it just doesn't print. Which is what I would expect to happen if I set logStyle to .none anyways.

    opened by Ponyboy47 0
Releases(1.0.3)
Owner
Swift Studies
Swift Studies
A simple logging package for Swift

OhMyLog OhMyLog is a simple logging package for Swift. It supports the following features: Six logging levels ( ?? , ?? , ?? , ⚠️ , ?? , ?? ) Display

Junhao Wang 1 Jan 10, 2022
Logger - This package is a thin wrapper for `os.Logger`

Logger This package is a thin wrapper for os.Logger. Usage Define os.Logger as an extension of Logger with the property name of the string that the sy

treastrain / Tanaka Ryoga 5 Jul 1, 2022
TraceLog is a highly configurable, flexible, portable, and simple to use debug logging system for Swift and Objective-C applications running on Linux, macOS, iOS, watchOS, and tvOS.

Please star this github repository to stay up to date. TraceLog Introduction TraceLog is a highly configurable, flexible, portable, and simple to use

Tony Stone 52 Oct 28, 2022
CleanroomLogger provides an extensible Swift-based logging API that is simple, lightweight and performant

CleanroomLogger CleanroomLogger provides an extensible Swift-based logging API that is simple, lightweight and performant. The API provided by Cleanro

null 1.3k Dec 8, 2022
Simple, lightweight and flexible debug logging framework written in Swift

AELog Simple, lightweight and flexible debug logging minion written in Swift If you find yourself in upcoming statements, then you probably want to us

Marko Tadić 28 Jul 6, 2022
Tracker - A simple location logger app written in Swift and MVVM architecture

Tracker - A simple location logger app written in Swift and MVVM architecture

Loay Ashraf 1 Mar 12, 2022
A fast & simple, yet powerful & flexible logging framework for Mac and iOS

CocoaLumberjack CocoaLumberjack is a fast & simple, yet powerful & flexible logging framework for macOS, iOS, tvOS and watchOS. How to get started Fir

null 12.9k Jan 9, 2023
A simple iOS app to simulate a laser level using built-in camera and gyroscope.

Laser Level A simple iOS app to simulate a laser level using built-in camera and gyroscope. Demo https://youtu.be/aB03EtQ5zsU Usage Download Open .xco

Pavel Trusov 2 Oct 30, 2022
A simple logger for your swift applications.

AHQSLogger A simple logging system. Usage import AHQSLogger Use the following methods for loggging. Logging an information / debug You can log a simp

André Henrique da Silva 0 Dec 29, 2021
Simple Design for Swift bridge with Javascript. Also can get javascript console.log.

SDBridgeOC is here. If your h5 partner confused about how to deal with iOS and Android. This Demo maybe help. YouTube video is here. bilibili Video is

null 20 Dec 28, 2022