GULRuntimeStateHelper.m 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2018 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 "GULRuntimeStateHelper.h"
  15. #import <objc/runtime.h>
  16. #import "GULRuntimeSnapshot.h"
  17. @implementation GULRuntimeStateHelper
  18. /** Initializes and returns the snapshot cache.
  19. *
  20. * @return A singleton snapshot cache.
  21. */
  22. + (NSMutableArray<GULRuntimeSnapshot *> *)snapshotCache {
  23. static NSMutableArray<GULRuntimeSnapshot *> *snapshots;
  24. static dispatch_once_t onceToken;
  25. dispatch_once(&onceToken, ^{
  26. snapshots = [[NSMutableArray<GULRuntimeSnapshot *> alloc] init];
  27. });
  28. return snapshots;
  29. }
  30. + (NSUInteger)captureRuntimeState {
  31. GULRuntimeSnapshot *snapshot = [[GULRuntimeSnapshot alloc] init];
  32. [snapshot capture];
  33. [[self snapshotCache] addObject:snapshot];
  34. return [self snapshotCache].count - 1;
  35. }
  36. + (NSUInteger)captureRuntimeStateOfClasses:(NSSet<Class> *)classes {
  37. GULRuntimeSnapshot *snapshot = [[GULRuntimeSnapshot alloc] initWithClasses:classes];
  38. [snapshot capture];
  39. [[self snapshotCache] addObject:snapshot];
  40. return [self snapshotCache].count - 1;
  41. }
  42. + (GULRuntimeDiff *)diffBetween:(NSUInteger)firstSnapshot
  43. secondSnapshot:(NSUInteger)secondSnapshot {
  44. NSArray *snapshotCache = [self snapshotCache];
  45. return [snapshotCache[firstSnapshot] diff:snapshotCache[secondSnapshot]];
  46. }
  47. @end