EventCleanupPerfTest.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import GoogleDataTransport
  18. import os.signpost
  19. /// The test actions to run under the profiler to measure performance of `GDTCORFlatFileStorage.checkForExpirations()` method.
  20. @available(iOS 12.0, *)
  21. class EventCleanupPerfTest {
  22. static let log = OSLog(subsystem: "GoogleDataTransport-TestApp", category: "EventCleanupPerfTest")
  23. static func run(completion: @escaping () -> Void) {
  24. let signpostID = OSSignpostID(log: log)
  25. os_signpost(.begin, log: log, name: "checkForExpirations", signpostID: signpostID)
  26. GDTCORFlatFileStorage.sharedInstance().checkForExpirations()
  27. GDTCORFlatFileStorage.sharedInstance().storageQueue.async {
  28. os_signpost(.end, log: log, name: "checkForExpirations", signpostID: signpostID)
  29. completion()
  30. }
  31. }
  32. static func generateTestEvents(count: Int, _ completion: @escaping () -> Void) {
  33. let signpostID = OSSignpostID(log: log)
  34. let group = DispatchGroup()
  35. os_signpost(.begin, log: log, name: "generateTestEvents", signpostID: signpostID)
  36. _ = (0 ..< count).compactMap { (_) -> GDTCOREvent? in
  37. group.enter()
  38. let event = GDTCOREventGenerator.generateEvent(for: .test, qosTier: nil, mappingID: nil)
  39. GDTCORFlatFileStorage.sharedInstance().store(event) { _, _ in
  40. group.leave()
  41. }
  42. return event
  43. }
  44. group.notify(queue: .main) {
  45. os_signpost(.end, log: log, name: "generateTestEvents", signpostID: signpostID)
  46. completion()
  47. }
  48. }
  49. }