From 444d5d3a5c73140d800e13401f0521e4a01ba3af Mon Sep 17 00:00:00 2001 From: Fabian Guerra Date: Wed, 20 Feb 2019 16:17:43 -0800 Subject: [ios, macos] Add tile download performance event. --- platform/darwin/src/MGLNetworkConfiguration.m | 47 +++++++++++++++++++++- .../darwin/src/MGLNetworkConfiguration_Private.h | 25 ++++++++++++ platform/darwin/src/http_file_source.mm | 11 +++-- platform/ios/ios.xcodeproj/project.pbxproj | 6 +++ platform/ios/sdk-files.json | 1 + platform/ios/src/MGLMapView.mm | 14 ++++++- platform/macos/macos.xcodeproj/project.pbxproj | 4 ++ platform/macos/sdk-files.json | 1 + 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 platform/darwin/src/MGLNetworkConfiguration_Private.h diff --git a/platform/darwin/src/MGLNetworkConfiguration.m b/platform/darwin/src/MGLNetworkConfiguration.m index 8262e7eea1..6f5959e70f 100644 --- a/platform/darwin/src/MGLNetworkConfiguration.m +++ b/platform/darwin/src/MGLNetworkConfiguration.m @@ -1,8 +1,15 @@ -#import "MGLNetworkConfiguration.h" +#import "MGLNetworkConfiguration_Private.h" + + +NSString * const kMGLDownloadPerformanceEvent = @"mobile.performance_trace"; +static NSString * const MGLStartTime = @"start_time"; +static NSString * const MGLResourceType = @"resource_type"; @interface MGLNetworkConfiguration () @property (strong) NSURLSessionConfiguration *sessionConfig; +@property (nonatomic, strong) NSMutableDictionary *events; +@property (nonatomic, weak, nullable) id delegate; @end @@ -14,6 +21,7 @@ dispatch_once(&onceToken, ^{ _sharedManager = [[self alloc] init]; _sharedManager.sessionConfiguration = nil; + _sharedManager.events = [NSMutableDictionary dictionary]; }); return _sharedManager; @@ -48,4 +56,41 @@ return sessionConfiguration; } +- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType { + if (urlString && ![self.events objectForKey:urlString]) { + [self.events setObject:@{ MGLStartTime: [NSDate date], MGLResourceType: resourceType } forKey:urlString]; + } +} + +- (void)stopDownloadEvent:(NSString *)urlString { + if (urlString && [self.events objectForKey:urlString]) { + if ([self.delegate respondsToSelector:@selector(networkConfiguration:didReceiveNetworkEvent:)]) + { + NSDictionary *parameters = [self.events objectForKey:urlString]; + NSDate *startDate = [parameters objectForKey:MGLStartTime]; + NSDate *endDate = [NSDate date]; + NSTimeInterval elapsedTime = [endDate timeIntervalSinceDate:startDate]; + NSDateFormatter* iso8601Formatter = [[NSDateFormatter alloc] init]; + iso8601Formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ"; + + NSDictionary *eventAttributes = @{ + @"event" : kMGLDownloadPerformanceEvent, + @"created" : [iso8601Formatter stringFromDate:[NSDate date]], + @"sessionId" : [NSUUID UUID].UUIDString, + @"counters" : @[ @{ @"name" : @"elapsed_time" }, @{ @"value" : @(elapsedTime) } ], + @"attributes" : @[ @{ @"name" : @"resource" }, @{ @"value" : urlString }, ] + }; + + [self.delegate networkConfiguration:self didReceiveNetworkEvent:eventAttributes]; + } + [self.events removeObjectForKey:urlString]; + } +} + +- (void)cancelDownloadEvent:(NSString *)urlString { + if (urlString && [self.events objectForKey:urlString]) { + [self.events removeObjectForKey:urlString]; + } +} + @end diff --git a/platform/darwin/src/MGLNetworkConfiguration_Private.h b/platform/darwin/src/MGLNetworkConfiguration_Private.h new file mode 100644 index 0000000000..8d55aea220 --- /dev/null +++ b/platform/darwin/src/MGLNetworkConfiguration_Private.h @@ -0,0 +1,25 @@ +#import "MGLNetworkConfiguration.h" + +NS_ASSUME_NONNULL_BEGIN + +extern NSString * const kMGLDownloadPerformanceEvent; + +@protocol MGLNetworkEventDelegate + +@optional +- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didReceiveNetworkEvent:(NSDictionary *)event; + +@end + +@interface MGLNetworkConfiguration (Private) + +@property (nonatomic, strong) NSMutableDictionary *events; +@property (nonatomic, weak, nullable) id delegate; + +- (void)startDownloadEvent:(NSString *)urlString type:(NSString *)resourceType; +- (void)stopDownloadEvent:(NSString *)urlString; +- (void)cancelDownloadEvent:(NSString *)urlString; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index c3918ad62b..c26568d8bc 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -8,7 +8,7 @@ #import #import "MGLLoggingConfiguration_Private.h" -#import "MGLNetworkConfiguration.h" +#import "MGLNetworkConfiguration_Private.h" #include #include @@ -215,14 +215,19 @@ std::unique_ptr HTTPFileSource::request(const Resource& resource, } [req addValue:impl->userAgent forHTTPHeaderField:@"User-Agent"]; - + + if (resource.kind == mbgl::Resource::Kind::Tile) { + [[MGLNetworkConfiguration sharedManager] startDownloadEvent:url.relativePath type:@"tile"]; + } + request->task = [impl->session dataTaskWithRequest:req completionHandler:^(NSData* data, NSURLResponse* res, NSError* error) { if (error && [error code] == NSURLErrorCancelled) { + [[MGLNetworkConfiguration sharedManager] cancelDownloadEvent:res.URL.relativePath]; return; } - + [[MGLNetworkConfiguration sharedManager] stopDownloadEvent:res.URL.relativePath]; Response response; using Error = Response::Error; diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 5122a60b11..e5ac34d98c 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -35,6 +35,8 @@ 1F06668D1EC64F8E001C16D7 /* MGLLight.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F0666891EC64F8E001C16D7 /* MGLLight.mm */; }; 1F26B6C120E189C9007BCC21 /* MBXCustomLocationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F26B6C020E189C9007BCC21 /* MBXCustomLocationViewController.m */; }; 1F26B6C320E1A351007BCC21 /* simple_route.json in Resources */ = {isa = PBXBuildFile; fileRef = 1F26B6C220E1A351007BCC21 /* simple_route.json */; }; + 1F2B94C0221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */; }; + 1F2B94C1221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */; }; 1F6A82A221360F9D00BA5B41 /* MGLLoggingConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F6A82A321360F9D00BA5B41 /* MGLLoggingConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F6A82A421360F9D00BA5B41 /* MGLLoggingConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 1F6A82A121360F9C00BA5B41 /* MGLLoggingConfiguration.m */; }; @@ -870,6 +872,7 @@ 1F26B6BF20E189C9007BCC21 /* MBXCustomLocationViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MBXCustomLocationViewController.h; sourceTree = ""; }; 1F26B6C020E189C9007BCC21 /* MBXCustomLocationViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MBXCustomLocationViewController.m; sourceTree = ""; }; 1F26B6C220E1A351007BCC21 /* simple_route.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = simple_route.json; sourceTree = ""; }; + 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLNetworkConfiguration_Private.h; sourceTree = ""; }; 1F6A82A021360F9C00BA5B41 /* MGLLoggingConfiguration.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLLoggingConfiguration.h; sourceTree = ""; }; 1F6A82A121360F9C00BA5B41 /* MGLLoggingConfiguration.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MGLLoggingConfiguration.m; sourceTree = ""; }; 1F6A82A62138871900BA5B41 /* MGLLoggingConfiguration_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLLoggingConfiguration_Private.h; sourceTree = ""; }; @@ -2093,6 +2096,7 @@ 927FBCFD1F4DB05500F8BF1F /* MGLMapSnapshotter.h */, 927FBCFE1F4DB05500F8BF1F /* MGLMapSnapshotter.mm */, DD0902A41DB18F1B00C5BDCE /* MGLNetworkConfiguration.h */, + 1F2B94BF221636D800210640 /* MGLNetworkConfiguration_Private.h */, DD0902A21DB18DE700C5BDCE /* MGLNetworkConfiguration.m */, 3EA9337830C7738BF7F5493C /* MGLRendererConfiguration.h */, 3EA931BC4F087E166D538F21 /* MGLRendererConfiguration.mm */, @@ -2416,6 +2420,7 @@ 3510FFF01D6D9D8C00F413B2 /* NSExpression+MGLAdditions.h in Headers */, 74CB5EBF219B280400102936 /* MGLHeatmapStyleLayer_Private.h in Headers */, 1FC4817D2098CBC0000D09B4 /* NSPredicate+MGLPrivateAdditions.h in Headers */, + 1F2B94C0221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */, 353AFA141D65AB17005A69F4 /* NSDate+MGLAdditions.h in Headers */, DA8848531CBAFB9800AB86E3 /* MGLCompactCalloutView.h in Headers */, 74CB5EB1219B252C00102936 /* MGLStyleLayerManager.h in Headers */, @@ -2657,6 +2662,7 @@ 3510FFFA1D6DCC4700F413B2 /* NSCompoundPredicate+MGLAdditions.h in Headers */, 1FF48588223710BE00F19727 /* MGLAttributedExpression.h in Headers */, DA72620C1DEEE3480043BB89 /* MGLOpenGLStyleLayer.h in Headers */, + 1F2B94C1221636D900210640 /* MGLNetworkConfiguration_Private.h in Headers */, 35CE61831D4165D9004F2359 /* UIColor+MGLAdditions.h in Headers */, 96E516F32000597100A02306 /* NSDictionary+MGLAdditions.h in Headers */, 1FCAE2A920B88B3800C577DD /* MGLLocationManager_Private.h in Headers */, diff --git a/platform/ios/sdk-files.json b/platform/ios/sdk-files.json index 94f928b77e..f1955483a8 100644 --- a/platform/ios/sdk-files.json +++ b/platform/ios/sdk-files.json @@ -223,6 +223,7 @@ "MGLStyle_Private.h": "platform/darwin/src/MGLStyle_Private.h", "MGLHeatmapStyleLayer_Private.h": "platform/darwin/src/MGLHeatmapStyleLayer_Private.h", "NSPredicate+MGLPrivateAdditions.h": "platform/darwin/src/NSPredicate+MGLPrivateAdditions.h", + "MGLNetworkConfiguration_Private.h": "platform/darwin/src/MGLNetworkConfiguration_Private.h", "NSDate+MGLAdditions.h": "platform/darwin/src/NSDate+MGLAdditions.h", "MGLCompactCalloutView.h": "platform/ios/src/MGLCompactCalloutView.h", "MGLStyleLayerManager.h": "platform/darwin/src/MGLStyleLayerManager.h", diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index c94cf477ef..ebb94ef6e5 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -76,6 +76,7 @@ #import "MGLMapAccessibilityElement.h" #import "MGLLocationManager_Private.h" #import "MGLLoggingConfiguration_Private.h" +#import "MGLNetworkConfiguration_Private.h" #include #include @@ -197,7 +198,8 @@ public: MGLSMCalloutViewDelegate, MGLCalloutViewDelegate, MGLMultiPointDelegate, - MGLAnnotationImageDelegate> + MGLAnnotationImageDelegate, + MGLNetworkEventDelegate> @property (nonatomic, readwrite) EAGLContext *context; @property (nonatomic) GLKView *glView; @@ -512,6 +514,9 @@ public: // setup default location manager self.locationManager = nil; + + // setup the download events + [MGLNetworkConfiguration sharedManager].delegate = self; // Set up annotation management and selection state. _annotationImagesByIdentifier = [NSMutableDictionary dictionary]; @@ -6901,4 +6906,11 @@ private: self.showsUserHeadingIndicator = showsHeading; } +#pragma mark - MGLNetworkEventDelegate + +- (void)networkConfiguration:(MGLNetworkConfiguration *)networkConfiguration didReceiveNetworkEvent:(NSDictionary *)event +{ + [MGLMapboxEvents pushEvent:kMGLDownloadPerformanceEvent withAttributes:event]; +} + @end diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index 57c4f198f0..132a5f355d 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 170A82BF201BDD1B00943087 /* MGLHeatmapStyleLayerTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 170A82BE201BDD1B00943087 /* MGLHeatmapStyleLayerTests.mm */; }; 170A82C4201FB6EC00943087 /* MGLHeatmapColorTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 170A82C2201FAFF800943087 /* MGLHeatmapColorTests.mm */; }; 1753ED401E53CE6100A9FD90 /* MGLConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = 1753ED3F1E53CE5200A9FD90 /* MGLConversion.h */; }; + 1F2B94C3221E22E600210640 /* MGLNetworkConfiguration_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F2B94C2221E22E500210640 /* MGLNetworkConfiguration_Private.h */; }; 1F7454A31ECFB00300021D39 /* MGLLight_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F7454A01ECFB00300021D39 /* MGLLight_Private.h */; }; 1F7454A41ECFB00300021D39 /* MGLLight.h in Headers */ = {isa = PBXBuildFile; fileRef = 1F7454A11ECFB00300021D39 /* MGLLight.h */; settings = {ATTRIBUTES = (Public, ); }; }; 1F7454A51ECFB00300021D39 /* MGLLight.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1F7454A21ECFB00300021D39 /* MGLLight.mm */; }; @@ -332,6 +333,7 @@ 170A82BE201BDD1B00943087 /* MGLHeatmapStyleLayerTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLHeatmapStyleLayerTests.mm; sourceTree = ""; }; 170A82C2201FAFF800943087 /* MGLHeatmapColorTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLHeatmapColorTests.mm; sourceTree = ""; }; 1753ED3F1E53CE5200A9FD90 /* MGLConversion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLConversion.h; sourceTree = ""; }; + 1F2B94C2221E22E500210640 /* MGLNetworkConfiguration_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLNetworkConfiguration_Private.h; sourceTree = ""; }; 1F7454A01ECFB00300021D39 /* MGLLight_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLLight_Private.h; sourceTree = ""; }; 1F7454A11ECFB00300021D39 /* MGLLight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLLight.h; sourceTree = ""; }; 1F7454A21ECFB00300021D39 /* MGLLight.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLLight.mm; sourceTree = ""; }; @@ -1218,6 +1220,7 @@ 92092EEE1F5EB10E00AF5130 /* MGLMapSnapshotter.h */, 92092EEF1F5EB10E00AF5130 /* MGLMapSnapshotter.mm */, DD0902B01DB1AC6400C5BDCE /* MGLNetworkConfiguration.h */, + 1F2B94C2221E22E500210640 /* MGLNetworkConfiguration_Private.h */, DD0902AF1DB1AC6400C5BDCE /* MGLNetworkConfiguration.m */, 3EA9369A4C46957566058822 /* MGLRendererConfiguration.h */, 3EA93B1B0864609938506E12 /* MGLRendererConfiguration.mm */, @@ -1337,6 +1340,7 @@ DAE6C3C21CC31F4500DB3429 /* Mapbox.h in Headers */, DAE6C3641CC31E0400DB3429 /* MGLPolygon.h in Headers */, DA35A2BF1CCA9B1A00E826B2 /* MGLClockDirectionFormatter.h in Headers */, + 1F2B94C3221E22E600210640 /* MGLNetworkConfiguration_Private.h in Headers */, 35602BFA1D3EA99F0050646F /* MGLFillStyleLayer.h in Headers */, DA35A2A41CC9EB1A00E826B2 /* MGLCoordinateFormatter.h in Headers */, 35C5D8491D6DD66D00E95907 /* NSCompoundPredicate+MGLAdditions.h in Headers */, diff --git a/platform/macos/sdk-files.json b/platform/macos/sdk-files.json index 25987a5a0e..a58d170e5e 100644 --- a/platform/macos/sdk-files.json +++ b/platform/macos/sdk-files.json @@ -171,6 +171,7 @@ "NSException+MGLAdditions.h": "platform/darwin/src/NSException+MGLAdditions.h", "MGLTileSource_Private.h": "platform/darwin/src/MGLTileSource_Private.h", "NSExpression+MGLPrivateAdditions.h": "platform/darwin/src/NSExpression+MGLPrivateAdditions.h", + "MGLNetworkConfiguration_Private.h": "platform/darwin/src/MGLNetworkConfiguration_Private.h", "NSCompoundPredicate+MGLAdditions.h": "platform/darwin/src/NSCompoundPredicate+MGLAdditions.h", "MGLSymbolStyleLayer_Private.h": "platform/darwin/src/MGLSymbolStyleLayer_Private.h", "NSProcessInfo+MGLAdditions.h": "platform/darwin/src/NSProcessInfo+MGLAdditions.h", -- cgit v1.2.1