PerfNetworkRequestMaker.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 "PerfNetworkRequestMaker.h"
  15. @interface PerfNetworkRequestMaker () <NSURLSessionDelegate, NSURLSessionDataDelegate>
  16. @end
  17. @implementation PerfNetworkRequestMaker
  18. + (void)performNetworkRequest:(NSURLRequest *)URLRequest delegate:(id<PerfTraceDelegate>)delegate {
  19. static dispatch_once_t onceToken;
  20. static NSURLSession *session = nil;
  21. dispatch_once(&onceToken, ^{
  22. NSURLSessionConfiguration *configuration =
  23. [NSURLSessionConfiguration defaultSessionConfiguration];
  24. configuration.HTTPMaximumConnectionsPerHost = 50;
  25. session = [NSURLSession sessionWithConfiguration:configuration];
  26. });
  27. NSURLSessionDataTask *dataTask =
  28. [session dataTaskWithRequest:URLRequest
  29. completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  30. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  31. NSLog(@"Received response code - %ld for URL - %@.", httpResponse.statusCode,
  32. response.URL.absoluteString);
  33. if (httpResponse.statusCode == 404) {
  34. NSLog(@"ERROR: Something went wrong, received status code 404.");
  35. }
  36. [delegate traceCompleted];
  37. }];
  38. [delegate traceStarted];
  39. [dataTask resume];
  40. }
  41. @end