DigestTestsPerf.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. // http://www.di-mgt.com.au/sha_testvectors.html (http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf)
  16. //
  17. import Foundation
  18. import XCTest
  19. @testable import CryptoSwift
  20. final class DigestTestsPerf: XCTestCase {
  21. func testMD5Performance() {
  22. measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: false) {
  23. let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
  24. self.startMeasuring()
  25. _ = Digest.md5(arr)
  26. self.stopMeasuring()
  27. }
  28. }
  29. func testSHA1Performance() {
  30. measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: false) {
  31. let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
  32. self.startMeasuring()
  33. _ = Digest.sha1(arr)
  34. self.stopMeasuring()
  35. }
  36. }
  37. // Keep it to compare
  38. /*
  39. func testSHA1PerformanceCC() {
  40. measureMetrics([XCTPerformanceMetric.wallClockTime], automaticallyStartMeasuring: false) {
  41. let arr = Array<UInt8>(repeating: 200, count: 1024 * 1024)
  42. self.startMeasuring()
  43. var digest = Array<UInt8>(repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
  44. CC_SHA1(arr, CC_LONG(arr.count), &digest)
  45. self.stopMeasuring()
  46. }
  47. }
  48. */
  49. }
  50. extension DigestTestsPerf {
  51. static func allTests() -> [(String, (DigestTestsPerf) -> () -> Void)] {
  52. [
  53. ("testMD5Performance", testMD5Performance),
  54. ("testSHA1Performance", testSHA1Performance)
  55. ]
  56. }
  57. }