GPBPerfTests.m 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Protocol Buffers - Google's data interchange format
// Copyright 2013 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import "GPBTestUtilities.h"
#import "google/protobuf/Unittest.pbobjc.h"
33
#import "google/protobuf/UnittestImport.pbobjc.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#import "google/protobuf/UnittestObjc.pbobjc.h"

//
// This file really just uses the unittests framework as a testbed to
// run some simple performance tests. The data can then be used to help
// evaluate changes to the runtime.
//

static const uint32_t kRepeatedCount = 100;

@interface PerfTests : GPBTestCase
@end

@implementation PerfTests

- (void)setUp {
  // A convenient place to put a break point if you want to connect instruments.
  [super setUp];
}

- (void)testMessagePerformance {
  [self measureBlock:^{
    for (int i = 0; i < 200; ++i) {
      TestAllTypes* message = [[TestAllTypes alloc] init];
      [self setAllFields:message repeatedCount:kRepeatedCount];
      NSData* rawBytes = [message data];
      [message release];
61
      message = [[TestAllTypes alloc] initWithData:rawBytes error:NULL];
62 63 64 65 66
      [message release];
    }
  }];
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
- (void)testMessageSerialParsingPerformance {
  // This and the next test are meant to monitor that the parsing functionality of protos does not
  // lock across threads when parsing different instances. The Serial version of the test should run
  // around ~2 times slower than the Parallel version since it's parsing the protos in the same
  // thread.
  TestAllTypes *allTypesMessage = [TestAllTypes message];
  [self setAllFields:allTypesMessage repeatedCount:2];
  NSData *allTypesData = allTypesMessage.data;

  [self measureBlock:^{
    for (int i = 0; i < 500; ++i) {
      [TestAllTypes parseFromData:allTypesData error:NULL];
      [TestAllTypes parseFromData:allTypesData error:NULL];
    }
  }];
}

- (void)testMessageParallelParsingPerformance {
  // This and the previous test are meant to monitor that the parsing functionality of protos does
  // not lock across threads when parsing different instances. The Serial version of the test should
  // run around ~2 times slower than the Parallel version since it's parsing the protos in the same
  // thread.
  TestAllTypes *allTypesMessage = [TestAllTypes message];
  [self setAllFields:allTypesMessage repeatedCount:2];
  NSData *allTypesData = allTypesMessage.data;

  dispatch_queue_t concurrentQueue = dispatch_queue_create("perfQueue", DISPATCH_QUEUE_CONCURRENT);

  [self measureBlock:^{
    for (int i = 0; i < 500; ++i) {
      dispatch_group_t group = dispatch_group_create();

      dispatch_group_async(group, concurrentQueue, ^{
        [TestAllTypes parseFromData:allTypesData error:NULL];
      });

      dispatch_group_async(group, concurrentQueue, ^{
        [TestAllTypes parseFromData:allTypesData error:NULL];
      });

      dispatch_group_notify(group, concurrentQueue, ^{});

      dispatch_release(group);
    }
  }];

  dispatch_release(concurrentQueue);
}

- (void)testMessageSerialExtensionsParsingPerformance {
  // This and the next test are meant to monitor that the parsing functionality of protos does not
  // lock across threads when parsing different instances when using extensions. The Serial version
  // of the test should run around ~2 times slower than the Parallel version since it's parsing the
  // protos in the same thread.
  TestAllExtensions *allExtensionsMessage = [TestAllExtensions message];
  [self setAllExtensions:allExtensionsMessage repeatedCount:2];
  NSData *allExtensionsData = allExtensionsMessage.data;

  [self measureBlock:^{
    for (int i = 0; i < 500; ++i) {
      [TestAllExtensions parseFromData:allExtensionsData
                     extensionRegistry:[self extensionRegistry]
                                 error:NULL];
      [TestAllExtensions parseFromData:allExtensionsData
                     extensionRegistry:[self extensionRegistry]
                                 error:NULL];
    }
  }];
}

- (void)testMessageParallelExtensionsParsingPerformance {
  // This and the previous test are meant to monitor that the parsing functionality of protos does
  // not lock across threads when parsing different instances when using extensions. The Serial
  // version of the test should run around ~2 times slower than the Parallel version since it's
  // parsing the protos in the same thread.
  TestAllExtensions *allExtensionsMessage = [TestAllExtensions message];
  [self setAllExtensions:allExtensionsMessage repeatedCount:2];
  NSData *allExtensionsData = allExtensionsMessage.data;

  dispatch_queue_t concurrentQueue = dispatch_queue_create("perfQueue", DISPATCH_QUEUE_CONCURRENT);

  [self measureBlock:^{
    for (int i = 0; i < 500; ++i) {
      dispatch_group_t group = dispatch_group_create();

      dispatch_group_async(group, concurrentQueue, ^{
        [TestAllExtensions parseFromData:allExtensionsData
                       extensionRegistry:[UnittestRoot extensionRegistry]
                                   error:NULL];
      });

      dispatch_group_async(group, concurrentQueue, ^{
        [TestAllExtensions parseFromData:allExtensionsData
                       extensionRegistry:[UnittestRoot extensionRegistry]
                                   error:NULL];
      });

      dispatch_group_notify(group, concurrentQueue, ^{});

      dispatch_release(group);
    }
  }];

  dispatch_release(concurrentQueue);
}

173 174 175 176 177 178 179 180
- (void)testExtensionsPerformance {
  [self measureBlock:^{
    for (int i = 0; i < 200; ++i) {
      TestAllExtensions* message = [[TestAllExtensions alloc] init];
      [self setAllExtensions:message repeatedCount:kRepeatedCount];
      NSData* rawBytes = [message data];
      [message release];
      TestAllExtensions* message2 =
181
          [[TestAllExtensions alloc] initWithData:rawBytes error:NULL];
182 183 184 185 186 187 188 189 190 191 192 193
      [message2 release];
    }
  }];
}

- (void)testPackedTypesPerformance {
  [self measureBlock:^{
    for (int i = 0; i < 1000; ++i) {
      TestPackedTypes* message = [[TestPackedTypes alloc] init];
      [self setPackedFields:message repeatedCount:kRepeatedCount];
      NSData* rawBytes = [message data];
      [message release];
194
      message = [[TestPackedTypes alloc] initWithData:rawBytes error:NULL];
195 196 197 198 199 200 201 202 203 204 205 206 207
      [message release];
    }
  }];
}

- (void)testPackedExtensionsPerformance {
  [self measureBlock:^{
    for (int i = 0; i < 1000; ++i) {
      TestPackedExtensions* message = [[TestPackedExtensions alloc] init];
      [self setPackedExtensions:message repeatedCount:kRepeatedCount];
      NSData* rawBytes = [message data];
      [message release];
      TestPackedExtensions* message2 =
208
          [[TestPackedExtensions alloc] initWithData:rawBytes error:NULL];
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
      [message2 release];
    }
  }];
}

- (void)testHas {
  TestAllTypes* message = [self allSetRepeatedCount:1];
  [self measureBlock:^{
    for (int i = 0; i < 10000; ++i) {
      [message hasOptionalInt32];
      message.hasOptionalInt32 = NO;
      [message hasOptionalInt32];

      [message hasOptionalInt64];
      message.hasOptionalInt64 = NO;
      [message hasOptionalInt64];

      [message hasOptionalUint32];
      message.hasOptionalUint32 = NO;
      [message hasOptionalUint32];

      [message hasOptionalUint64];
      message.hasOptionalUint64 = NO;
      [message hasOptionalUint64];

      [message hasOptionalSint32];
      message.hasOptionalSint32 = NO;
      [message hasOptionalSint32];

      [message hasOptionalSint64];
      message.hasOptionalSint64 = NO;
      [message hasOptionalSint64];

      [message hasOptionalFixed32];
      message.hasOptionalFixed32 = NO;
      [message hasOptionalFixed32];

      [message hasOptionalFixed64];
      message.hasOptionalFixed64 = NO;
      [message hasOptionalFixed64];

      [message hasOptionalSfixed32];
      message.hasOptionalSfixed32 = NO;
      [message hasOptionalSfixed32];

      [message hasOptionalSfixed64];
      message.hasOptionalSfixed64 = NO;
      [message hasOptionalSfixed64];

      [message hasOptionalFloat];
      message.hasOptionalFloat = NO;
      [message hasOptionalFloat];

      [message hasOptionalDouble];
      message.hasOptionalDouble = NO;
      [message hasOptionalDouble];

      [message hasOptionalBool];
      message.hasOptionalBool = NO;
      [message hasOptionalBool];

      [message hasOptionalString];
      message.hasOptionalString = NO;
      [message hasOptionalString];

      [message hasOptionalBytes];
      message.hasOptionalBytes = NO;
      [message hasOptionalBytes];

      [message hasOptionalGroup];
      message.hasOptionalGroup = NO;
      [message hasOptionalGroup];

      [message hasOptionalNestedMessage];
      message.hasOptionalNestedMessage = NO;
      [message hasOptionalNestedMessage];

      [message hasOptionalForeignMessage];
      message.hasOptionalForeignMessage = NO;
      [message hasOptionalForeignMessage];

      [message hasOptionalImportMessage];
      message.hasOptionalImportMessage = NO;
      [message hasOptionalImportMessage];

      [message.optionalGroup hasA];
      message.optionalGroup.hasA = NO;
      [message.optionalGroup hasA];

      [message.optionalNestedMessage hasBb];
      message.optionalNestedMessage.hasBb = NO;
      [message.optionalNestedMessage hasBb];

      [message.optionalForeignMessage hasC];
      message.optionalForeignMessage.hasC = NO;
      [message.optionalForeignMessage hasC];

      [message.optionalImportMessage hasD];
      message.optionalImportMessage.hasD = NO;
      [message.optionalImportMessage hasD];

      [message hasOptionalNestedEnum];
      message.hasOptionalNestedEnum = NO;
      [message hasOptionalNestedEnum];

      [message hasOptionalForeignEnum];
      message.hasOptionalForeignEnum = NO;
      [message hasOptionalForeignEnum];

      [message hasOptionalImportEnum];
      message.hasOptionalImportEnum = NO;
      [message hasOptionalImportEnum];

      [message hasOptionalStringPiece];
      message.hasOptionalStringPiece = NO;
      [message hasOptionalStringPiece];

      [message hasOptionalCord];
      message.hasOptionalCord = NO;
      [message hasOptionalCord];

      [message hasDefaultInt32];
      message.hasDefaultInt32 = NO;
      [message hasDefaultInt32];

      [message hasDefaultInt64];
      message.hasDefaultInt64 = NO;
      [message hasDefaultInt64];

      [message hasDefaultUint32];
      message.hasDefaultUint32 = NO;
      [message hasDefaultUint32];

      [message hasDefaultUint64];
      message.hasDefaultUint64 = NO;
      [message hasDefaultUint64];

      [message hasDefaultSint32];
      message.hasDefaultSint32 = NO;
      [message hasDefaultSint32];

      [message hasDefaultSint64];
      message.hasDefaultSint64 = NO;
      [message hasDefaultSint64];

      [message hasDefaultFixed32];
      message.hasDefaultFixed32 = NO;
      [message hasDefaultFixed32];

      [message hasDefaultFixed64];
      message.hasDefaultFixed64 = NO;
      [message hasDefaultFixed64];

      [message hasDefaultSfixed32];
      message.hasDefaultSfixed32 = NO;
      [message hasDefaultSfixed32];

      [message hasDefaultSfixed64];
      message.hasDefaultSfixed64 = NO;
      [message hasDefaultSfixed64];

      [message hasDefaultFloat];
      message.hasDefaultFloat = NO;
      [message hasDefaultFloat];

      [message hasDefaultDouble];
      message.hasDefaultDouble = NO;
      [message hasDefaultDouble];

      [message hasDefaultBool];
      message.hasDefaultBool = NO;
      [message hasDefaultBool];

      [message hasDefaultString];
      message.hasDefaultString = NO;
      [message hasDefaultString];

      [message hasDefaultBytes];
      message.hasDefaultBytes = NO;
      [message hasDefaultBytes];

      [message hasDefaultNestedEnum];
      message.hasDefaultNestedEnum = NO;
      [message hasDefaultNestedEnum];

      [message hasDefaultForeignEnum];
      message.hasDefaultForeignEnum = NO;
      [message hasDefaultForeignEnum];

      [message hasDefaultImportEnum];
      message.hasDefaultImportEnum = NO;
      [message hasDefaultImportEnum];

      [message hasDefaultStringPiece];
      message.hasDefaultStringPiece = NO;
      [message hasDefaultStringPiece];

      [message hasDefaultCord];
      message.hasDefaultCord = NO;
      [message hasDefaultCord];
    }
  }];
}

@end