FPRMemoryGaugeCollectorTests.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations+Private.h"
  16. #import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector+Private.h"
  17. #import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.h"
  18. #import <OCMock/OCMock.h>
  19. #import "FirebasePerformance/Tests/Unit/Fakes/FPRFakeConfigurations.h"
  20. extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
  21. @interface FPRMemoryGaugeCollectorTests : XCTestCase <FPRMemoryGaugeCollectorDelegate>
  22. /** Tracker for the number of times a delegate is called. */
  23. @property(nonatomic) NSInteger delegateCalled;
  24. /** Fake configurations flags. */
  25. @property(nonatomic) FPRFakeConfigurations *configurations;
  26. @end
  27. @implementation FPRMemoryGaugeCollectorTests
  28. - (void)setUp {
  29. [super setUp];
  30. self.delegateCalled = 0;
  31. self.configurations =
  32. [[FPRFakeConfigurations alloc] initWithSources:FPRConfigurationSourceRemoteConfig];
  33. [self.configurations setDataCollectionEnabled:YES];
  34. [self.configurations setInstrumentationEnabled:YES];
  35. [self.configurations setMemorySamplingFrequencyInForegroundInMS:100];
  36. [self.configurations setMemorySamplingFrequencyInBackgroundInMS:0];
  37. }
  38. /** Validates if the instance creation is successful. */
  39. - (void)testInstanceCreation {
  40. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  41. XCTAssertNotNil(collector);
  42. XCTAssertNotNil(collector.delegate);
  43. }
  44. /** Validates that the delegate call back works after measuring memory metric. */
  45. - (void)testDelegateCallback {
  46. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  47. XCTAssertFalse(self.delegateCalled);
  48. [collector collectMetric];
  49. XCTAssertTrue(self.delegateCalled == 1);
  50. }
  51. /** Validate if the memory gauge metric collection has necessary fields */
  52. - (void)testMemoryMetricData {
  53. FPRMemoryGaugeData *gaugeData = fprCollectMemoryMetric();
  54. XCTAssertNotNil(gaugeData);
  55. XCTAssertNotNil(gaugeData.collectionTime);
  56. }
  57. /** Validates the performance of the memory measurement. */
  58. - (void)testPerformanceMemoryGaugeCollection {
  59. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  60. uint64_t processTimeNs = dispatch_benchmark(1000, ^{
  61. [collector collectMetric];
  62. });
  63. // Ensure that memory measurement is less than 500 microseconds every run.
  64. XCTAssertTrue(processTimeNs < 500000);
  65. }
  66. /** Validate zero value of the frequency of memory data sampling. */
  67. - (void)testZeroFrequencyOfMemoryCollection {
  68. [self.configurations setMemorySamplingFrequencyInForegroundInMS:0];
  69. [self.configurations setMemorySamplingFrequencyInBackgroundInMS:0];
  70. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  71. collector.configurations = self.configurations;
  72. id mock = [OCMockObject partialMockForObject:collector];
  73. OCMStub([mock collectMetric]);
  74. [[mock reject] collectMetric];
  75. [collector updateSamplingFrequencyForApplicationState:FPRApplicationStateBackground];
  76. [collector updateSamplingFrequencyForApplicationState:FPRApplicationStateForeground];
  77. [mock verify];
  78. }
  79. /** Validate if the change in application state honors the right frequency. */
  80. - (void)testChangingApplicationStateHonorsUpdatedFrequency {
  81. [self.configurations setMemorySamplingFrequencyInForegroundInMS:100];
  82. [self.configurations setMemorySamplingFrequencyInBackgroundInMS:0];
  83. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  84. collector.configurations = self.configurations;
  85. [collector updateSamplingFrequencyForApplicationState:FPRApplicationStateForeground];
  86. XCTestExpectation *expectation =
  87. [self expectationWithDescription:@"Dummy expectation to wait 3 seconds"];
  88. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)),
  89. dispatch_get_main_queue(), ^{
  90. [expectation fulfill];
  91. XCTAssertTrue(self.delegateCalled > 15);
  92. });
  93. [self waitForExpectationsWithTimeout:3.0 handler:nil];
  94. }
  95. /** Validate if the change in application state is honored. */
  96. - (void)testChangingApplicationStateHonorsChangeInApplicationState {
  97. [self.configurations setMemorySamplingFrequencyInForegroundInMS:100];
  98. [self.configurations setMemorySamplingFrequencyInBackgroundInMS:200];
  99. FPRMemoryGaugeCollector *collector = [[FPRMemoryGaugeCollector alloc] initWithDelegate:self];
  100. collector.configurations = self.configurations;
  101. [collector updateSamplingFrequencyForApplicationState:FPRApplicationStateForeground];
  102. __block NSInteger numberOfTimesDelegateCalled = 0;
  103. XCTestExpectation *expectation =
  104. [self expectationWithDescription:@"Dummy expectation to wait 3 seconds"];
  105. dispatch_after(
  106. dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  107. numberOfTimesDelegateCalled = self.delegateCalled;
  108. [collector updateSamplingFrequencyForApplicationState:FPRApplicationStateBackground];
  109. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)),
  110. dispatch_get_main_queue(), ^{
  111. [expectation fulfill];
  112. NSLog(@"Delegate called %d times", (int)self.delegateCalled);
  113. XCTAssertTrue(self.delegateCalled - numberOfTimesDelegateCalled > 3);
  114. });
  115. });
  116. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  117. }
  118. #pragma mark - FPRMemoryGaugeCollectorDelegate methods
  119. - (void)memoryGaugeCollector:(FPRMemoryGaugeCollector *)collector
  120. gaugeData:(FPRMemoryGaugeData *)gaugeData {
  121. self.delegateCalled++;
  122. }
  123. @end