RCTPerformanceLogger.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import <QuartzCore/QuartzCore.h>
  8. #import "RCTLog.h"
  9. #import "RCTPerformanceLogger.h"
  10. #import "RCTProfile.h"
  11. #import "RCTRootView.h"
  12. @interface RCTPerformanceLogger () {
  13. int64_t _data[RCTPLSize][2];
  14. NSUInteger _cookies[RCTPLSize];
  15. }
  16. @property (nonatomic, copy) NSArray<NSString *> *labelsForTags;
  17. @end
  18. @implementation RCTPerformanceLogger
  19. - (instancetype)init
  20. {
  21. if (self = [super init]) {
  22. // Keep this in sync with RCTPLTag
  23. _labelsForTags = @[
  24. @"ScriptDownload",
  25. @"ScriptExecution",
  26. @"RAMBundleLoad",
  27. @"RAMStartupCodeSize",
  28. @"RAMStartupNativeRequires",
  29. @"RAMStartupNativeRequiresCount",
  30. @"RAMNativeRequires",
  31. @"RAMNativeRequiresCount",
  32. @"NativeModuleInit",
  33. @"NativeModuleMainThread",
  34. @"NativeModulePrepareConfig",
  35. @"NativeModuleMainThreadUsesCount",
  36. @"NativeModuleSetup",
  37. @"TurboModuleSetup",
  38. @"JSCWrapperOpenLibrary",
  39. @"BridgeStartup",
  40. @"RootViewTTI",
  41. @"BundleSize",
  42. ];
  43. }
  44. return self;
  45. }
  46. - (void)markStartForTag:(RCTPLTag)tag
  47. {
  48. #if RCT_PROFILE
  49. if (RCTProfileIsProfiling()) {
  50. NSString *label = _labelsForTags[tag];
  51. _cookies[tag] = RCTProfileBeginAsyncEvent(RCTProfileTagAlways, label, nil);
  52. }
  53. #endif
  54. _data[tag][0] = CACurrentMediaTime() * 1000;
  55. _data[tag][1] = 0;
  56. }
  57. - (void)markStopForTag:(RCTPLTag)tag
  58. {
  59. #if RCT_PROFILE
  60. if (RCTProfileIsProfiling()) {
  61. NSString *label = _labelsForTags[tag];
  62. RCTProfileEndAsyncEvent(RCTProfileTagAlways, @"native", _cookies[tag], label, @"RCTPerformanceLogger");
  63. }
  64. #endif
  65. if (_data[tag][0] != 0 && _data[tag][1] == 0) {
  66. _data[tag][1] = CACurrentMediaTime() * 1000;
  67. } else {
  68. RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
  69. }
  70. }
  71. - (void)setValue:(int64_t)value forTag:(RCTPLTag)tag
  72. {
  73. _data[tag][0] = 0;
  74. _data[tag][1] = value;
  75. }
  76. - (void)addValue:(int64_t)value forTag:(RCTPLTag)tag
  77. {
  78. _data[tag][0] = 0;
  79. _data[tag][1] += value;
  80. }
  81. - (void)appendStartForTag:(RCTPLTag)tag
  82. {
  83. _data[tag][0] = CACurrentMediaTime() * 1000;
  84. }
  85. - (void)appendStopForTag:(RCTPLTag)tag
  86. {
  87. if (_data[tag][0] != 0) {
  88. _data[tag][1] += CACurrentMediaTime() * 1000 - _data[tag][0];
  89. _data[tag][0] = 0;
  90. } else {
  91. RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
  92. }
  93. }
  94. - (NSArray<NSNumber *> *)valuesForTags
  95. {
  96. NSMutableArray *result = [NSMutableArray array];
  97. for (NSUInteger index = 0; index < RCTPLSize; index++) {
  98. [result addObject:@(_data[index][0])];
  99. [result addObject:@(_data[index][1])];
  100. }
  101. return result;
  102. }
  103. - (int64_t)durationForTag:(RCTPLTag)tag
  104. {
  105. return _data[tag][1] - _data[tag][0];
  106. }
  107. - (int64_t)valueForTag:(RCTPLTag)tag
  108. {
  109. return _data[tag][1];
  110. }
  111. @end