PerfFuzzNSURLSession.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Non-google3 relative import to support building with Xcode.
  15. #import "PerfFuzzNSURLSession.h"
  16. #import "PerfNetworkConnection+Protected.h"
  17. @interface PerfFuzzNSURLSession ()
  18. @property(nonatomic, copy) NSString *urlString;
  19. @end
  20. @implementation PerfFuzzNSURLSession
  21. #pragma mark - NetworkConnection
  22. - (void)makeNetworkRequestWithSuccessCallback:(SuccessNetworkCallback)success
  23. failureCallback:(FailureNetworkCallback)fail {
  24. [self logOperationStart];
  25. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
  26. NSURLSession *session = nil;
  27. int numberToFuzz = 1000;
  28. for (int i = 0; i < numberToFuzz; i++) {
  29. NSURLSessionConfiguration *configuration = nil;
  30. BOOL isShared = NO;
  31. switch (arc4random_uniform(5)) {
  32. case 0:
  33. configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  34. session = [NSURLSession sessionWithConfiguration:configuration];
  35. break;
  36. case 1:
  37. configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
  38. session = [NSURLSession sessionWithConfiguration:configuration];
  39. break;
  40. case 2:
  41. configuration = [NSURLSessionConfiguration
  42. backgroundSessionConfiguration:[NSString stringWithFormat:@"fpr_%d", i]];
  43. session = [NSURLSession sessionWithConfiguration:configuration];
  44. break;
  45. case 3:
  46. configuration = [NSURLSessionConfiguration
  47. backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"fpr_%d", i]];
  48. session = [NSURLSession sessionWithConfiguration:configuration];
  49. break;
  50. case 4:
  51. session = [NSURLSession sharedSession];
  52. isShared = YES;
  53. break;
  54. default:
  55. break;
  56. }
  57. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
  58. NSURLSessionDataTask *task = [session dataTaskWithRequest:request];
  59. // Intentionally only resume half. The SDK should gracefully handle non-resumed tasks.
  60. if (i % 2 == 0) {
  61. [task resume];
  62. } else {
  63. [task cancel];
  64. }
  65. if (!isShared) {
  66. [session finishTasksAndInvalidate];
  67. }
  68. });
  69. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
  70. }
  71. success();
  72. [self logOperationSuccess];
  73. }
  74. @end