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

Related tags

Tools 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...
Soulful docs for Swift & Objective-C
Soulful docs for Swift & Objective-C

jazzy is a command-line utility that generates documentation for Swift or Objective-C About Both Swift and Objective-C projects are supported. Instead

Laurine - Localization code generator written in Swift. Sweet!
Laurine - Localization code generator written in Swift. Sweet!

Author's note: Thanks everyone for making Laurine the TOP trending Swift repository in the world - this is amazing and very heart-warming! But this is

Script to support easily using Xcode Asset Catalog in Swift.
Script to support easily using Xcode Asset Catalog in Swift.

Misen Misen is a script to support using Xcode Asset Catalog in Swift. Features Misen scans sub-directories in the specified Asset Catalog and creates

An Xcode Plugin to convert Objective-C to Swift
An Xcode Plugin to convert Objective-C to Swift

XCSwiftr Convert Objective-C code into Swift from within Xcode. This plugin uses the Java applet of objc2swift to do the conversion. Noticed that the

Swift autocompleter for Sublime Text, via the adorable SourceKitten framework
Swift autocompleter for Sublime Text, via the adorable SourceKitten framework

SwiftKitten SwiftKitten is a Swift autocompleter for Sublime Text, via the adorable SourceKitten framework. Faster than XCode ! This package is new an

🔥 🔥 🔥Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

🔥 🔥 🔥Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

NV_MVVM-C is a template file generator. This can reduce the time taken to write the boilerplate code and create the files.
NV_MVVM-C is a template file generator. This can reduce the time taken to write the boilerplate code and create the files.

NV_MVVM-C Template, is an MVVM-C Boilerplate generator which will help you generate all the necessary files for your project architected in MVVM-C.

Measuring the progress with annotations 🔱
Measuring the progress with annotations 🔱

Description Displaying the progress in a meter control. ProgressMeter lets you create your custom annotations that display either on top or bottom of

In-App iOS Debugging Tool With Enhanced Logging, Networking Info, Crash reporting And More.
In-App iOS Debugging Tool With Enhanced Logging, Networking Info, Crash reporting And More.

The debugger tool for iOS developer. Display logs, network request, device informations, crash logs while using the app. Easy accessible with its bubble head button 🔘 . Easy to integrate in any apps, to handle development or testing apps easier. First version, there is plenty of room for improvement.

Dotzu  In-App iOS Debugging Tool With Enhanced Logging, Networking Info, Crash reporting And More.
Dotzu In-App iOS Debugging Tool With Enhanced Logging, Networking Info, Crash reporting And More.

Dotzu In-App iOS Debugging Tool With Enhanced Logging, Networking Info, Crash reporting And More. The debugger tool for iOS developer. Display logs, n

In-app feedback and bug reporting tool for apps.

Instabug iOS SDK Instabug is an in-app feedback and bug reporting tool for mobile apps. With just a simple shake, your users or beta testers can repor

Each step you take reveals a new horizon. You have taken the first step today.
Each step you take reveals a new horizon. You have taken the first step today.

The story Seeing the animations behind Paper, or the transitions behind Mail, being in a world of flat design and transitions, user interaction, app b

Awesome bug reporting for iOS apps
Awesome bug reporting for iOS apps

Buglife is an awesome bug reporting SDK & web platform for iOS apps. Here's how it works: User takes a screenshot, or stops screen recording User anno

Appstore-Review-Guidelines - A curated list of guideline which has to be taken care before submitting your application to Appstore.

Appstore Review Guidelines The App Review Guidelines provide rules and examples across a range of topics, including user interface design, functionali

Buglife-iOS - Awesome bug reporting for iOS apps
Buglife-iOS - Awesome bug reporting for iOS apps

Buglife is an awesome bug reporting SDK & web platform for iOS apps. Here's how it works: User takes a screenshot, or stops screen recording User anno

Sandwhich is a mobile app built to solve the sandwich debate by determining if the picture you've taken is a sandwich using machine learning
Sandwhich is a mobile app built to solve the sandwich debate by determining if the picture you've taken is a sandwich using machine learning

Sandwhich Website | Twitter Sandwhich is a mobile app built to solve the sandwich debate by determining if the picture you've taken is a sandwich usin

I made this in the same afternoon it was given to me, hopefully that is taken into consideration
I made this in the same afternoon it was given to me, hopefully that is taken into consideration

AlbumShowcase I made this in the same afternoon it was given to me, hopefully th

Bootcamp1000 - Practical application of a design taken from the Dribble website - For our group in WhatsApp

Bootcamp1000 New Challenge Practical application of a design taken from the Drib

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
Simple PBXProj Verifier

Kin Kin is a minimalistic tool to check whether your project.pbxproj file is correct. What does it do? Have you ever found yourself undoing a merge du

Sergio Gutiérrez 857 Dec 29, 2022
Automatically build and rebuild Xcode image catalogs for app icons, universal images, and more

Better asset workflow for iOS developers. Generate Xcode image catalogs for iOS / OSX app icons, universal images, and more.

Dotan J. Nahum 822 Dec 21, 2022
AVXCAssets Generator takes path for your assets images and creates appiconset and imageset for you in just one click

AVXCAssets Generator Often while developing an app, We ran into a condition when we need to scale images to each and every aspect ratios for icons and

Angel Vasa 339 Dec 6, 2022
Build native iOS, Android, and Web apps with Capacitor and Remix.run 💿

This repository holds production ready Capacitor templates for building native mobile applications using Remix. Using Capacitor, you can quickly build out a native mobile application for iOS and Android using web technology, such as Remix.

Ionic 70 Dec 30, 2022
Swift CLI for strong-typing images, colors, storyboards, fonts and localizations

Shark Shark is a Swift command line tool that generates type safe enums for your images, colors, storyboards, fonts and localizations. Because Shark r

Kaan Dedeoglu 377 Dec 1, 2022
Strong typed, autocompleted resources like images, fonts and segues in Swift projects

R.swift Get strong typed, autocompleted resources like images, fonts and segues in Swift projects Why use this? It makes your code that uses resources

Mathijs Kadijk 8.9k Jan 6, 2023
swiftenv allows you to easily install, and switch between multiple versions of Swift.

Swift Version Manager swiftenv allows you to easily install, and switch between multiple versions of Swift. This project was heavily inspired by pyenv

Kyle Fuller 1.9k Dec 27, 2022
Xcode storyboards diff and merge tool.

StoryboardMerge Storyboard diff and merge tool which: compares and merges two storyboard files, provides an automatic merge-facility, The storyboardin

null 238 Sep 12, 2022
An adorable little framework and command line tool for interacting with SourceKit.

SourceKitten An adorable little framework and command line tool for interacting with SourceKit. SourceKitten links and communicates with sourcekitd.fr

JP Simard 2.1k Jan 5, 2023
SwiftGen is a tool to automatically generate Swift code for resources of your projects

SwiftGen SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them ty

null 8.3k Jan 5, 2023