Skip to content

Instantly share code, notes, and snippets.

@sonsongithub
Created September 17, 2015 18:09
Show Gist options
  • Select an option

  • Save sonsongithub/eccb65a94c93238c6cc4 to your computer and use it in GitHub Desktop.

Select an option

Save sonsongithub/eccb65a94c93238c6cc4 to your computer and use it in GitHub Desktop.
Speed performance test for URL percent escaping.
//
// escapeTests.swift
// escapeTests
//
// Created by sonson on 2015/09/18.
// Copyright © 2015年 sonson. All rights reserved.
//
import XCTest
//@testable import escape
let allowedCharacterSet = NSMutableCharacterSet.alphanumericCharacterSet()
extension String {
var percentEncodedURLString: String? {
return dataUsingEncoding(NSUTF8StringEncoding).flatMap {
NSURL(dataRepresentation: $0, relativeToURL: nil).relativeString
}
}
var stringByAddingPercentEscapesUsingUTF8 : String? {
let raw: NSString = self
return raw.stringByAddingPercentEncodingWithAllowedCharacters(allowedCharacterSet)
}
}
class escapeTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
allowedCharacterSet.addCharactersInString("-._:/~")
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
var count = 0
for i in 1...10000 {
let input = "https://github.com/sonsongithub/reddift/blob/master/reddift/OAuth/OAuth2Authorizer.swift"
let a = input.percentEncodedURLString!
count = a.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
}
}
}
func testPerformanceExample2() {
// This is an example of a performance test case.
self.measureBlock {
var count = 0
for i in 1...10000 {
let input = "https://github.com/sonsongithub/reddift/blob/master/reddift/OAuth/OAuth2Authorizer.swift"
let a = input.stringByAddingPercentEscapesUsingUTF8!
count = a.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment