PerfTraceMaker.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "PerfTraceMaker.h"
  15. #import "FirebasePerformance/FIRPerformance.h"
  16. #import "PerfE2EUtils.h"
  17. static const NSInteger kTraceMaxCounters = 32;
  18. @implementation PerfTraceMaker
  19. + (FIRTrace *)createTraceWithName:(NSString *)name
  20. duration:(NSTimeInterval)duration
  21. delegate:(id<PerfTraceDelegate>)delegate {
  22. FIRTrace *trace = [FIRPerformance startTraceWithName:name];
  23. [delegate traceStarted];
  24. NSDate *startTime = [NSDate date];
  25. NSInteger traceCount = [[name substringFromIndex:1] integerValue];
  26. // Increment metric.
  27. for (int i = 0; i < kTraceMaxCounters; i++) {
  28. NSString *counterName = [NSString stringWithFormat:@"%@c%02d", trace.name, i];
  29. NSInteger counterMeanValue = traceCount + i + 5;
  30. NSInteger counterValue = (int)randomGaussianValueWithMeanAndDeviation(counterMeanValue, 1);
  31. [trace incrementMetric:counterName byInt:counterValue];
  32. }
  33. // Set custom attributes.
  34. for (int i = 0; i < 5; i++) {
  35. NSString *attributeName = [NSString stringWithFormat:@"d%d", i];
  36. NSString *attributeValue = [NSString stringWithFormat:@"t%ld_d%d", traceCount, i];
  37. [trace setValue:attributeValue forAttribute:attributeName];
  38. }
  39. NSTimeInterval processingDuration = ABS([startTime timeIntervalSinceNow]);
  40. // Wait for the duration specified.
  41. [NSThread sleepForTimeInterval:(duration - processingDuration)];
  42. [trace stop];
  43. [delegate traceCompleted];
  44. return trace;
  45. }
  46. @end