Authored by 于良

增加卡顿监控,review by 戴强

@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 Pod::Spec.new do |s| 9 Pod::Spec.new do |s|
10 10
11 s.name = "YHEventReport" 11 s.name = "YHEventReport"
12 - s.version = "0.5.8" 12 + s.version = "0.6.0"
13 s.summary = "YHEventReport." 13 s.summary = "YHEventReport."
14 s.description = "无埋点SDK" 14 s.description = "无埋点SDK"
15 s.homepage = "http://git.yoho.cn/mobile/YH_EventReport" 15 s.homepage = "http://git.yoho.cn/mobile/YH_EventReport"
@@ -34,11 +34,55 @@ Pod::Spec.new do |s| @@ -34,11 +34,55 @@ Pod::Spec.new do |s|
34 s.default_subspec = 'classes' 34 s.default_subspec = 'classes'
35 35
36 s.subspec 'Core' do |ss| 36 s.subspec 'Core' do |ss|
37 - ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/**/*.{h,m}' 37 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_Core/*.{h,m}'
38 ss.xcconfig = { "HEADER_SEARCH_PATHS" => [ 38 ss.xcconfig = { "HEADER_SEARCH_PATHS" => [
39 "${PODS_ROOT}/../YH_Mall/Dependences/PowerfulBannerView", 39 "${PODS_ROOT}/../YH_Mall/Dependences/PowerfulBannerView",
40 ].join(' ') 40 ].join(' ')
41 } 41 }
42 end 42 end
43 43
  44 + s.subspec "AOP" do |ss|
  45 + ss.dependency "YHEventReport/Core"
  46 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_AOP/*.{h,m}'
  47 + end
  48 +
  49 + s.subspec "EventCollector" do |ss|
  50 + ss.dependency "YHEventReport/Core"
  51 + ss.dependency "YHEventReport/AOP"
  52 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_EventCollector/*.{h,m}'
  53 + end
  54 +
  55 + s.subspec "EventCache" do |ss|
  56 + ss.dependency "YHEventReport/Core"
  57 + ss.dependency "YHEventReport/AOP"
  58 + ss.dependency "YHEventReport/EventCollector"
  59 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_EventCache/*.{h,m}'
  60 + end
  61 +
  62 + s.subspec "EventDataSession" do |ss|
  63 + ss.dependency "YHEventReport/Core"
  64 + ss.dependency "YHEventReport/AOP"
  65 + ss.dependency "YHEventReport/EventCollector"
  66 + ss.dependency "YHEventReport/EventCache"
  67 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_EventDataSession/*.{h,m}'
  68 + end
  69 +
  70 + s.subspec "CrashReporter" do |ss|
  71 + ss.dependency "YHEventReport/Core"
  72 + ss.dependency "YHEventReport/AOP"
  73 + ss.dependency "YHEventReport/EventCollector"
  74 + ss.dependency "YHEventReport/EventCache"
  75 + ss.dependency "YHEventReport/EventDataSession"
  76 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_CrashReporter/*.{h,m}'
  77 + end
  78 +
  79 + s.subspec "LagHunter" do |ss|
  80 + ss.dependency "YHEventReport/Core"
  81 + ss.dependency "YHEventReport/AOP"
  82 + ss.dependency "YHEventReport/EventCollector"
  83 + ss.dependency "YHEventReport/EventCache"
  84 + ss.dependency "YHEventReport/EventDataSession"
  85 + ss.source_files = 'YHEventReport/YHEventReport/YHEventReport/YH_LagHunter/*.{h,m}'
  86 + end
  87 +
44 end 88 end
@@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
7 objects = { 7 objects = {
8 8
9 /* Begin PBXBuildFile section */ 9 /* Begin PBXBuildFile section */
  10 + 2906173F1FEB8E8B00BD569F /* YHEventReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 2906173E1FEB8E8B00BD569F /* YHEventReport.m */; };
  11 + 290617431FEB951300BD569F /* YH_LagHunter.m in Sources */ = {isa = PBXBuildFile; fileRef = 290617421FEB951300BD569F /* YH_LagHunter.m */; };
10 2955040E1F66B4A1006A3F14 /* UIApplication+AutoTrack.m in Sources */ = {isa = PBXBuildFile; fileRef = 2955040D1F66B4A1006A3F14 /* UIApplication+AutoTrack.m */; }; 12 2955040E1F66B4A1006A3F14 /* UIApplication+AutoTrack.m in Sources */ = {isa = PBXBuildFile; fileRef = 2955040D1F66B4A1006A3F14 /* UIApplication+AutoTrack.m */; };
11 295504111F67B78F006A3F14 /* UITableViewCell+AutoTrack.m in Sources */ = {isa = PBXBuildFile; fileRef = 295504101F67B78F006A3F14 /* UITableViewCell+AutoTrack.m */; }; 13 295504111F67B78F006A3F14 /* UITableViewCell+AutoTrack.m in Sources */ = {isa = PBXBuildFile; fileRef = 295504101F67B78F006A3F14 /* UITableViewCell+AutoTrack.m */; };
12 29CDB4AD1F6FA57900AAD350 /* InfiniteScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = 29CDB4AA1F6FA57900AAD350 /* InfiniteScrollView.m */; }; 14 29CDB4AD1F6FA57900AAD350 /* InfiniteScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = 29CDB4AA1F6FA57900AAD350 /* InfiniteScrollView.m */; };
@@ -30,7 +32,6 @@ @@ -30,7 +32,6 @@
30 5255C2BE1F5D25F400A8FC5F /* YH_EventUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B11F5D25F400A8FC5F /* YH_EventUtil.m */; }; 32 5255C2BE1F5D25F400A8FC5F /* YH_EventUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B11F5D25F400A8FC5F /* YH_EventUtil.m */; };
31 5255C2BF1F5D25F400A8FC5F /* YH_EventDataSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B41F5D25F400A8FC5F /* YH_EventDataSession.m */; }; 33 5255C2BF1F5D25F400A8FC5F /* YH_EventDataSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B41F5D25F400A8FC5F /* YH_EventDataSession.m */; };
32 5255C2C01F5D25F400A8FC5F /* YH_Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B61F5D25F400A8FC5F /* YH_Reachability.m */; }; 34 5255C2C01F5D25F400A8FC5F /* YH_Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B61F5D25F400A8FC5F /* YH_Reachability.m */; };
33 - 5255C2C11F5D25F400A8FC5F /* YHEventReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 5255C2B81F5D25F400A8FC5F /* YHEventReport.m */; };  
34 52878BFB1F5D44F7000A1597 /* YH_EventCollector.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878BFA1F5D44F7000A1597 /* YH_EventCollector.m */; }; 35 52878BFB1F5D44F7000A1597 /* YH_EventCollector.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878BFA1F5D44F7000A1597 /* YH_EventCollector.m */; };
35 52878C041F5E8904000A1597 /* YHLog.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878C031F5E8904000A1597 /* YHLog.m */; }; 36 52878C041F5E8904000A1597 /* YHLog.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878C031F5E8904000A1597 /* YHLog.m */; };
36 52878C071F5F8D82000A1597 /* UIView+Yoho.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878C061F5F8D82000A1597 /* UIView+Yoho.m */; }; 37 52878C071F5F8D82000A1597 /* UIView+Yoho.m in Sources */ = {isa = PBXBuildFile; fileRef = 52878C061F5F8D82000A1597 /* UIView+Yoho.m */; };
@@ -94,6 +95,10 @@ @@ -94,6 +95,10 @@
94 /* End PBXBuildFile section */ 95 /* End PBXBuildFile section */
95 96
96 /* Begin PBXFileReference section */ 97 /* Begin PBXFileReference section */
  98 + 2906173D1FEB8E8B00BD569F /* YHEventReport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHEventReport.h; sourceTree = "<group>"; };
  99 + 2906173E1FEB8E8B00BD569F /* YHEventReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YHEventReport.m; sourceTree = "<group>"; };
  100 + 290617411FEB951300BD569F /* YH_LagHunter.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = YH_LagHunter.h; sourceTree = "<group>"; };
  101 + 290617421FEB951300BD569F /* YH_LagHunter.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = YH_LagHunter.m; sourceTree = "<group>"; };
97 2955040C1F66B4A1006A3F14 /* UIApplication+AutoTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+AutoTrack.h"; sourceTree = "<group>"; }; 102 2955040C1F66B4A1006A3F14 /* UIApplication+AutoTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIApplication+AutoTrack.h"; sourceTree = "<group>"; };
98 2955040D1F66B4A1006A3F14 /* UIApplication+AutoTrack.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+AutoTrack.m"; sourceTree = "<group>"; }; 103 2955040D1F66B4A1006A3F14 /* UIApplication+AutoTrack.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+AutoTrack.m"; sourceTree = "<group>"; };
99 2955040F1F67B78F006A3F14 /* UITableViewCell+AutoTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableViewCell+AutoTrack.h"; sourceTree = "<group>"; }; 104 2955040F1F67B78F006A3F14 /* UITableViewCell+AutoTrack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UITableViewCell+AutoTrack.h"; sourceTree = "<group>"; };
@@ -140,8 +145,6 @@ @@ -140,8 +145,6 @@
140 5255C2B41F5D25F400A8FC5F /* YH_EventDataSession.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_EventDataSession.m; sourceTree = "<group>"; }; 145 5255C2B41F5D25F400A8FC5F /* YH_EventDataSession.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_EventDataSession.m; sourceTree = "<group>"; };
141 5255C2B51F5D25F400A8FC5F /* YH_Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YH_Reachability.h; sourceTree = "<group>"; }; 146 5255C2B51F5D25F400A8FC5F /* YH_Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YH_Reachability.h; sourceTree = "<group>"; };
142 5255C2B61F5D25F400A8FC5F /* YH_Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_Reachability.m; sourceTree = "<group>"; }; 147 5255C2B61F5D25F400A8FC5F /* YH_Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_Reachability.m; sourceTree = "<group>"; };
143 - 5255C2B71F5D25F400A8FC5F /* YHEventReport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHEventReport.h; sourceTree = "<group>"; };  
144 - 5255C2B81F5D25F400A8FC5F /* YHEventReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YHEventReport.m; sourceTree = "<group>"; };  
145 5255C2C41F5D402900A8FC5F /* YHEventReportMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHEventReportMacros.h; sourceTree = "<group>"; }; 148 5255C2C41F5D402900A8FC5F /* YHEventReportMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YHEventReportMacros.h; sourceTree = "<group>"; };
146 52878BF91F5D44F7000A1597 /* YH_EventCollector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YH_EventCollector.h; sourceTree = "<group>"; }; 149 52878BF91F5D44F7000A1597 /* YH_EventCollector.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YH_EventCollector.h; sourceTree = "<group>"; };
147 52878BFA1F5D44F7000A1597 /* YH_EventCollector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_EventCollector.m; sourceTree = "<group>"; }; 150 52878BFA1F5D44F7000A1597 /* YH_EventCollector.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YH_EventCollector.m; sourceTree = "<group>"; };
@@ -275,6 +278,24 @@ @@ -275,6 +278,24 @@
275 /* End PBXFrameworksBuildPhase section */ 278 /* End PBXFrameworksBuildPhase section */
276 279
277 /* Begin PBXGroup section */ 280 /* Begin PBXGroup section */
  281 + 2906173C1FEB8E8B00BD569F /* YH_Core */ = {
  282 + isa = PBXGroup;
  283 + children = (
  284 + 2906173D1FEB8E8B00BD569F /* YHEventReport.h */,
  285 + 2906173E1FEB8E8B00BD569F /* YHEventReport.m */,
  286 + );
  287 + path = YH_Core;
  288 + sourceTree = "<group>";
  289 + };
  290 + 290617401FEB94FB00BD569F /* YH_LagHunter */ = {
  291 + isa = PBXGroup;
  292 + children = (
  293 + 290617411FEB951300BD569F /* YH_LagHunter.h */,
  294 + 290617421FEB951300BD569F /* YH_LagHunter.m */,
  295 + );
  296 + path = YH_LagHunter;
  297 + sourceTree = "<group>";
  298 + };
278 29CDB4A81F6FA57900AAD350 /* PowerfulBannerView */ = { 299 29CDB4A81F6FA57900AAD350 /* PowerfulBannerView */ = {
279 isa = PBXGroup; 300 isa = PBXGroup;
280 children = ( 301 children = (
@@ -300,8 +321,8 @@ @@ -300,8 +321,8 @@
300 5255C2A31F5D25F400A8FC5F /* YHEventReport */ = { 321 5255C2A31F5D25F400A8FC5F /* YHEventReport */ = {
301 isa = PBXGroup; 322 isa = PBXGroup;
302 children = ( 323 children = (
303 - 5255C2B71F5D25F400A8FC5F /* YHEventReport.h */,  
304 - 5255C2B81F5D25F400A8FC5F /* YHEventReport.m */, 324 + 290617401FEB94FB00BD569F /* YH_LagHunter */,
  325 + 2906173C1FEB8E8B00BD569F /* YH_Core */,
305 5255C2A41F5D25F400A8FC5F /* YH_AOP */, 326 5255C2A41F5D25F400A8FC5F /* YH_AOP */,
306 5223D9CE1F8F50FF00EB698F /* YH_CrashReporter */, 327 5223D9CE1F8F50FF00EB698F /* YH_CrashReporter */,
307 5255C2C21F5D265000A8FC5F /* YH_EventCollector */, 328 5255C2C21F5D265000A8FC5F /* YH_EventCollector */,
@@ -744,10 +765,12 @@ @@ -744,10 +765,12 @@
744 52D204EB1F61331900CB7F1A /* UIAlertView+AutoTrack.m in Sources */, 765 52D204EB1F61331900CB7F1A /* UIAlertView+AutoTrack.m in Sources */,
745 B1B6C3A21F74AD3100627DF3 /* UITableView+YHExposure.m in Sources */, 766 B1B6C3A21F74AD3100627DF3 /* UITableView+YHExposure.m in Sources */,
746 B1D7749F1F62A37E00BA89C3 /* Aspects.m in Sources */, 767 B1D7749F1F62A37E00BA89C3 /* Aspects.m in Sources */,
  768 + 290617431FEB951300BD569F /* YH_LagHunter.m in Sources */,
747 B1C419011F616187005E0729 /* JTSHardwareInfo.m in Sources */, 769 B1C419011F616187005E0729 /* JTSHardwareInfo.m in Sources */,
748 B1B6C3A31F74AD3100627DF3 /* UIView+YHExposure.m in Sources */, 770 B1B6C3A31F74AD3100627DF3 /* UIView+YHExposure.m in Sources */,
749 5255C2BD1F5D25F400A8FC5F /* YH_EventCacheManager.m in Sources */, 771 5255C2BD1F5D25F400A8FC5F /* YH_EventCacheManager.m in Sources */,
750 5255C2B91F5D25F400A8FC5F /* UICollectionView+AutoTrack.m in Sources */, 772 5255C2B91F5D25F400A8FC5F /* UICollectionView+AutoTrack.m in Sources */,
  773 + 2906173F1FEB8E8B00BD569F /* YHEventReport.m in Sources */,
751 29D3B97C1F73A81E00B07F47 /* YH_EventCollector+UIActionSheet.m in Sources */, 774 29D3B97C1F73A81E00B07F47 /* YH_EventCollector+UIActionSheet.m in Sources */,
752 529A004E1F564E8500A83F63 /* AppDelegate.m in Sources */, 775 529A004E1F564E8500A83F63 /* AppDelegate.m in Sources */,
753 B1B6C3A11F74AD3100627DF3 /* UIScrollView+YHExposure.m in Sources */, 776 B1B6C3A11F74AD3100627DF3 /* UIScrollView+YHExposure.m in Sources */,
@@ -765,7 +788,6 @@ @@ -765,7 +788,6 @@
765 B1D774A21F62A37E00BA89C3 /* SDImageCache.m in Sources */, 788 B1D774A21F62A37E00BA89C3 /* SDImageCache.m in Sources */,
766 B1C418CD1F5FFBAA005E0729 /* YH_PerformanceAppInfo.m in Sources */, 789 B1C418CD1F5FFBAA005E0729 /* YH_PerformanceAppInfo.m in Sources */,
767 5255C2BC1F5D25F400A8FC5F /* YH_EventCacheFile.m in Sources */, 790 5255C2BC1F5D25F400A8FC5F /* YH_EventCacheFile.m in Sources */,
768 - 5255C2C11F5D25F400A8FC5F /* YHEventReport.m in Sources */,  
769 529A004B1F564E8500A83F63 /* main.m in Sources */, 791 529A004B1F564E8500A83F63 /* main.m in Sources */,
770 B1C419021F616187005E0729 /* OpenUDID.m in Sources */, 792 B1C419021F616187005E0729 /* OpenUDID.m in Sources */,
771 B1D774A51F62A37E00BA89C3 /* SDWebImageDownloader.m in Sources */, 793 B1D774A51F62A37E00BA89C3 /* SDWebImageDownloader.m in Sources */,
@@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
18 - (void)timeEventStartWithViewController:(UIViewController *)viewController; 18 - (void)timeEventStartWithViewController:(UIViewController *)viewController;
19 - (void)timeEventEndWithViewController:(UIViewController *)viewController; 19 - (void)timeEventEndWithViewController:(UIViewController *)viewController;
20 20
21 -- (void)timeEventStartWithWebView:(UIWebView *)webView; 21 +//- (void)timeEventStartWithWebView:(UIWebView *)webView;
22 - (void)timeEventEndWithWebView:(UIWebView *)webView status:(YHEventLoadStatus)status; 22 - (void)timeEventEndWithWebView:(UIWebView *)webView status:(YHEventLoadStatus)status;
23 23
24 - (void)timeEventStartWithWebImageDownloadURL:(NSString *)imageURL; 24 - (void)timeEventStartWithWebImageDownloadURL:(NSString *)imageURL;
@@ -39,14 +39,6 @@ @@ -39,14 +39,6 @@
39 - (void)timeEventStartWithView:(id)targetView collectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath; 39 - (void)timeEventStartWithView:(id)targetView collectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath;
40 - (void)timeEventEndWithView:(id)targetView collectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath; 40 - (void)timeEventEndWithView:(id)targetView collectionView:(UICollectionView*)collectionView indexPath:(NSIndexPath*)indexPath;
41 41
42 -+ (void)trackAppClickWithView:(id)targetView UITableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;  
43 -  
44 -+ (void)trackAppClickWithView:(id)targetView UICollectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;  
45 -  
46 -+ (void)trackAppClickWithView:(id)targetView UITabbar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;  
47 -  
48 -+ (void)trackAppClickWithView:(id)targetView UIAlertView:(UIAlertView *)alertView didSelectIndex:(NSInteger )buttonIndex;  
49 -  
50 + (NSString *)viewPathOfPurposeView:(UIView *)purposeView; 42 + (NSString *)viewPathOfPurposeView:(UIView *)purposeView;
51 43
52 @end 44 @end
  1 +//
  2 +// YH_LagHunter.h
  3 +// YHEventReport
  4 +//
  5 +// Created by 于良 on 2017/12/21.
  6 +// Copyright © 2017年 YOHO. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +
  11 +@interface YH_LagHunter : NSObject
  12 +
  13 ++ (instancetype)sharedInstance;
  14 +
  15 +- (void)startMonitor;
  16 +- (void)stopMonitor;
  17 +
  18 +@end
  1 +//
  2 +// YH_LagHunter.m
  3 +// YHEventReport
  4 +//
  5 +// Created by 于良 on 2017/12/21.
  6 +// Copyright © 2017年 YOHO. All rights reserved.
  7 +//
  8 +
  9 +#import "YH_LagHunter.h"
  10 +
  11 +
  12 +@interface YH_LagHunter()
  13 +
  14 +@property (assign, nonatomic) NSInteger timeoutCount;
  15 +@property (assign, nonatomic) CFRunLoopObserverRef observer;
  16 +@property (strong, nonatomic) dispatch_semaphore_t semaphore;
  17 +@property (assign, nonatomic) CFRunLoopActivity activity;
  18 +
  19 +
  20 +@end
  21 +
  22 +
  23 +@implementation YH_LagHunter
  24 +
  25 ++ (instancetype)sharedInstance
  26 +{
  27 + static id instance = nil;
  28 + static dispatch_once_t onceToken;
  29 + dispatch_once(&onceToken, ^{
  30 + instance = [[self alloc] init];
  31 + });
  32 + return instance;
  33 +}
  34 +
  35 +static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info)
  36 +{
  37 + YH_LagHunter *moniotr = (__bridge YH_LagHunter*)info;
  38 +
  39 + moniotr.activity = activity;
  40 +
  41 + dispatch_semaphore_t semaphore = moniotr.semaphore;
  42 + dispatch_semaphore_signal(semaphore);
  43 +}
  44 +
  45 +- (void)stopMonitor
  46 +{
  47 + if (!_observer)
  48 + return;
  49 + CFRunLoopRemoveObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);
  50 + CFRelease(_observer);
  51 + _observer = NULL;
  52 +}
  53 +
  54 +- (void)startMonitor
  55 +{
  56 + if (_observer)
  57 + return;
  58 +
  59 + // 信号,Dispatch Semaphore保证同步
  60 + _semaphore = dispatch_semaphore_create(0);
  61 +
  62 + // 注册RunLoop状态观察
  63 + CFRunLoopObserverContext context = {0,(__bridge void*)self,NULL,NULL};
  64 + _observer = CFRunLoopObserverCreate(kCFAllocatorDefault,
  65 + kCFRunLoopAllActivities,
  66 + YES,
  67 + 0,
  68 + &runLoopObserverCallBack,
  69 + &context);
  70 + //将观察者添加到主线程runloop的common模式下的观察中
  71 + CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);
  72 +
  73 + // 在子线程监控时长 开启一个持续的loop用来进行监控
  74 + dispatch_async(dispatch_get_global_queue(0, 0), ^{
  75 + while (YES)
  76 + {
  77 + //假定连续5次超时50ms认为卡顿(当然也包含了单次超时250ms)
  78 + long st = dispatch_semaphore_wait(_semaphore, dispatch_time(DISPATCH_TIME_NOW, 50*NSEC_PER_MSEC));
  79 + if (st != 0)
  80 + {
  81 + if (!_observer)
  82 + {
  83 + _timeoutCount = 0;
  84 + _semaphore = 0;
  85 + _activity = 0;
  86 + return;
  87 + }
  88 + //两个runloop的状态,BeforeSources和AfterWaiting这两个状态区间时间能够检测到是否卡顿
  89 + if (_activity==kCFRunLoopBeforeSources || _activity==kCFRunLoopAfterWaiting)
  90 + {
  91 + if (++_timeoutCount < 5)
  92 + continue;
  93 +// PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType:PLCrashReporterSignalHandlerTypeBSD
  94 +// symbolicationStrategy:PLCrashReporterSymbolicationStrategyAll];
  95 +// PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration:config];
  96 +// NSData *data = [crashReporter generateLiveReport];
  97 +// PLCrashReport *reporter = [[PLCrashReport alloc] initWithData:data error:NULL];
  98 +// NSString *report = [PLCrashReportTextFormatter stringValueForCrashReport:reporter
  99 +// withTextFormat:PLCrashReportTextFormatiOS];
  100 +// //上传服务器
  101 +// NSLog(@"此处发生卡顿:---%@", report);
  102 +
  103 + NSLog(@"此处发生卡顿:-----------");
  104 + }//end activity
  105 + }// end semaphore wait
  106 + _timeoutCount = 0;
  107 + }// end while
  108 + });
  109 +}
  110 +
  111 +@end