runner.c++ 21.1 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
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 33 34 35 36 37 38
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. 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.
//
// 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.

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <iomanip>

using namespace std;

39
namespace capnp {
Kenton Varda's avatar
Kenton Varda committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
namespace benchmark {
namespace runner {

struct Times {
  uint64_t real;
  uint64_t user;
  uint64_t sys;

  uint64_t cpu() { return user + sys; }

  Times operator-(const Times& other) {
    Times result;
    result.real = real - other.real;
    result.user = user - other.user;
    result.sys = sys - other.sys;
    return result;
  }
};

uint64_t asNanosecs(const struct timeval& tv) {
  return (uint64_t)tv.tv_sec * 1000000000 + (uint64_t)tv.tv_usec * 1000;
}

Times currentTimes() {
  Times result;

  struct rusage self, children;
  getrusage(RUSAGE_SELF, &self);
  getrusage(RUSAGE_CHILDREN, &children);

  struct timeval real;
  gettimeofday(&real, nullptr);

  result.real = asNanosecs(real);
  result.user = asNanosecs(self.ru_utime) + asNanosecs(children.ru_utime);
  result.sys = asNanosecs(self.ru_stime) + asNanosecs(children.ru_stime);

  return result;
}

struct TestResult {
81 82
  uint64_t objectSize;
  uint64_t messageSize;
Kenton Varda's avatar
Kenton Varda committed
83 84 85 86 87 88 89 90 91 92 93
  Times time;
};

enum class Product {
  CAPNPROTO,
  PROTOBUF,
  NULLCASE
};

enum class TestCase {
  EVAL,
Kenton Varda's avatar
Kenton Varda committed
94 95
  CATRANK,
  CARSALES
Kenton Varda's avatar
Kenton Varda committed
96 97
};

98 99 100 101 102 103 104 105 106 107 108 109 110
const char* testCaseName(TestCase testCase) {
  switch (testCase) {
    case TestCase::EVAL:
      return "eval";
    case TestCase::CATRANK:
      return "catrank";
    case TestCase::CARSALES:
      return "carsales";
  }
  // Can't get here.
  return nullptr;
}

Kenton Varda's avatar
Kenton Varda committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
enum class Mode {
  OBJECTS,
  OBJECT_SIZE,
  BYTES,
  PIPE_SYNC,
  PIPE_ASYNC
};

enum class Reuse {
  YES,
  NO
};

enum class Compression {
125 126 127
  NONE,
  PACKED,
  SNAPPY
Kenton Varda's avatar
Kenton Varda committed
128 129 130 131
};

TestResult runTest(Product product, TestCase testCase, Mode mode, Reuse reuse,
                   Compression compression, uint64_t iters) {
132 133 134
  char* argv[6];

  string progName;
Kenton Varda's avatar
Kenton Varda committed
135 136 137

  switch (product) {
    case Product::CAPNPROTO:
138
      progName = "capnproto-";
Kenton Varda's avatar
Kenton Varda committed
139 140
      break;
    case Product::PROTOBUF:
141
      progName = "protobuf-";
Kenton Varda's avatar
Kenton Varda committed
142 143
      break;
    case Product::NULLCASE:
144
      progName = "null-";
Kenton Varda's avatar
Kenton Varda committed
145 146 147
      break;
  }

148
  progName += testCaseName(testCase);
Kenton Varda's avatar
Kenton Varda committed
149
  argv[0] = strdup(progName.c_str());
Kenton Varda's avatar
Kenton Varda committed
150 151 152

  switch (mode) {
    case Mode::OBJECTS:
153
      argv[1] = strdup("object");
Kenton Varda's avatar
Kenton Varda committed
154 155
      break;
    case Mode::OBJECT_SIZE:
156
      argv[1] = strdup("object-size");
Kenton Varda's avatar
Kenton Varda committed
157 158
      break;
    case Mode::BYTES:
159
      argv[1] = strdup("bytes");
Kenton Varda's avatar
Kenton Varda committed
160 161
      break;
    case Mode::PIPE_SYNC:
162
      argv[1] = strdup("pipe");
Kenton Varda's avatar
Kenton Varda committed
163 164
      break;
    case Mode::PIPE_ASYNC:
165
      argv[1] = strdup("pipe-async");
Kenton Varda's avatar
Kenton Varda committed
166 167 168 169 170
      break;
  }

  switch (reuse) {
    case Reuse::YES:
171
      argv[2] = strdup("reuse");
Kenton Varda's avatar
Kenton Varda committed
172 173
      break;
    case Reuse::NO:
174
      argv[2] = strdup("no-reuse");
Kenton Varda's avatar
Kenton Varda committed
175 176 177 178 179
      break;
  }

  switch (compression) {
    case Compression::NONE:
180
      argv[3] = strdup("none");
Kenton Varda's avatar
Kenton Varda committed
181
      break;
182 183 184 185 186 187
    case Compression::PACKED:
      argv[3] = strdup("packed");
      break;
    case Compression::SNAPPY:
      argv[3] = strdup("snappy");
      break;
Kenton Varda's avatar
Kenton Varda committed
188 189 190 191
  }

  char itersStr[64];
  sprintf(itersStr, "%llu", (long long unsigned int)iters);
192
  argv[4] = itersStr;
Kenton Varda's avatar
Kenton Varda committed
193

194
  argv[5] = nullptr;
Kenton Varda's avatar
Kenton Varda committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

  // Make pipe for child to write throughput.
  int childPipe[2];
  if (pipe(childPipe) < 0) {
    perror("pipe");
    exit(1);
  }

  // Spawn the child process.
  struct timeval start, end;
  gettimeofday(&start, nullptr);
  pid_t child = fork();
  if (child == 0) {
    close(childPipe[0]);
    dup2(childPipe[1], STDOUT_FILENO);
    close(childPipe[1]);
    execv(argv[0], argv);
    exit(1);
  }

  close(childPipe[1]);
Kenton Varda's avatar
Kenton Varda committed
216
  for (int i = 0; i < 4; i++) {
Kenton Varda's avatar
Kenton Varda committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    free(argv[i]);
  }

  // Read throughput number written to child's stdout.
  FILE* input = fdopen(childPipe[0], "r");
  long long unsigned int throughput;
  if (fscanf(input, "%lld", &throughput) != 1) {
    fprintf(stderr, "Child didn't write throughput to stdout.");
  }
  char buffer[1024];
  while (fgets(buffer, sizeof(buffer), input) != nullptr) {
    // Loop until EOF.
  }
  fclose(input);

  // Wait for child exit.
  int status;
  struct rusage usage;
  wait4(child, &status, 0, &usage);
  gettimeofday(&end, nullptr);

  // Calculate results.

  TestResult result;
241 242
  result.objectSize = mode == Mode::OBJECT_SIZE ? throughput : 0;
  result.messageSize = mode == Mode::OBJECT_SIZE ? 0 : throughput;
Kenton Varda's avatar
Kenton Varda committed
243 244 245 246 247 248 249 250 251
  result.time.real = asNanosecs(end) - asNanosecs(start);
  result.time.user = asNanosecs(usage.ru_utime);
  result.time.sys = asNanosecs(usage.ru_stime);

  return result;
}

void reportTableHeader() {
  cout << setw(40) << left << "Test"
252
       << setw(10) << right << "obj size"
Kenton Varda's avatar
Kenton Varda committed
253 254 255 256 257
       << setw(10) << right << "I/O bytes"
       << setw(10) << right << "wall ns"
       << setw(10) << right << "user ns"
       << setw(10) << right << "sys ns"
       << endl;
258
  cout << setfill('=') << setw(90) << "" << setfill(' ') << endl;
Kenton Varda's avatar
Kenton Varda committed
259 260 261 262
}

void reportResults(const char* name, uint64_t iters, TestResult results) {
  cout << setw(40) << left << name
263 264
       << setw(10) << right << (results.objectSize / iters)
       << setw(10) << right << (results.messageSize / iters)
Kenton Varda's avatar
Kenton Varda committed
265 266 267 268 269 270 271
       << setw(10) << right << (results.time.real / iters)
       << setw(10) << right << (results.time.user / iters)
       << setw(10) << right << (results.time.sys / iters)
       << endl;
}

void reportComparisonHeader() {
272
  cout << setw(40) << left << "Measure"
Kenton Varda's avatar
Kenton Varda committed
273 274 275 276
       << setw(15) << right << "Protobuf"
       << setw(15) << right << "Cap'n Proto"
       << setw(15) << right << "Improvement"
       << endl;
277
  cout << setfill('=') << setw(85) << "" << setfill(' ') << endl;
Kenton Varda's avatar
Kenton Varda committed
278 279
}

280 281 282 283 284 285 286 287 288
void reportOldNewComparisonHeader() {
  cout << setw(40) << left << "Measure"
       << setw(15) << right << "Old"
       << setw(15) << right << "New"
       << setw(15) << right << "Improvement"
       << endl;
  cout << setfill('=') << setw(85) << "" << setfill(' ') << endl;
}

Kenton Varda's avatar
Kenton Varda committed
289 290 291
class Gain {
public:
  Gain(double oldValue, double newValue)
Kenton Varda's avatar
Kenton Varda committed
292
      : amount(newValue / oldValue) {}
Kenton Varda's avatar
Kenton Varda committed
293 294

  void writeTo(std::ostream& os) {
Kenton Varda's avatar
Kenton Varda committed
295 296
    if (amount < 2) {
      double percent = (amount - 1) * 100;
Kenton Varda's avatar
Kenton Varda committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
      os << (int)(percent + 0.5) << "%";
    } else {
      os << fixed << setprecision(2) << amount << "x";
    }
  }

private:
  double amount;
};

ostream& operator<<(ostream& os, Gain gain) {
  gain.writeTo(os);
  return os;
}

void reportComparison(const char* name, double base, double protobuf, double capnproto,
313
                      uint64_t iters) {
314
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
315 316 317 318 319 320 321 322
       << setw(14) << right << Gain(base, protobuf)
       << setw(14) << right << Gain(base, capnproto);

  // Since smaller is better, the "improvement" is the "gain" from capnproto to protobuf.
  cout << setw(14) << right << Gain(capnproto - base, protobuf - base) << endl;
}

void reportComparison(const char* name, const char* unit, double protobuf, double capnproto,
323
                      uint64_t iters) {
324
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
325 326 327 328 329 330 331 332 333
       << setw(15-strlen(unit)) << fixed << right << setprecision(2) << (protobuf / iters) << unit
       << setw(15-strlen(unit)) << fixed << right << setprecision(2) << (capnproto / iters) << unit;

  // Since smaller is better, the "improvement" is the "gain" from capnproto to protobuf.
  cout << setw(14) << right << Gain(capnproto, protobuf) << endl;
}

void reportIntComparison(const char* name, const char* unit, uint64_t protobuf, uint64_t capnproto,
                         uint64_t iters) {
334
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
335 336
       << setw(15-strlen(unit)) << right << (protobuf / iters) << unit
       << setw(15-strlen(unit)) << right << (capnproto / iters) << unit;
Kenton Varda's avatar
Kenton Varda committed
337 338 339 340 341

  // Since smaller is better, the "improvement" is the "gain" from capnproto to protobuf.
  cout << setw(14) << right << Gain(capnproto, protobuf) << endl;
}

342
size_t fileSize(const std::string& name) {
Kenton Varda's avatar
Kenton Varda committed
343
  struct stat stats;
344 345
  if (stat(name.c_str(), &stats) < 0) {
    perror(name.c_str());
Kenton Varda's avatar
Kenton Varda committed
346 347 348 349 350 351 352 353 354
    exit(1);
  }

  return stats.st_size;
}

int main(int argc, char* argv[]) {
  char* path = argv[0];
  char* slashpos = strrchr(path, '/');
355 356 357 358 359
  char origDir[1024];
  if (getcwd(origDir, sizeof(origDir)) == nullptr) {
    perror("getcwd");
    return 1;
  }
Kenton Varda's avatar
Kenton Varda committed
360 361 362 363 364 365 366 367 368 369 370 371 372
  if (slashpos != nullptr) {
    *slashpos = '\0';
    if (chdir(path) < 0) {
      perror("chdir");
      return 1;
    }
    *slashpos = '/';
  }

  TestCase testCase = TestCase::CATRANK;
  Mode mode = Mode::PIPE_SYNC;
  Compression compression = Compression::NONE;
  uint64_t iters = 1;
373
  const char* oldDir = nullptr;
Kenton Varda's avatar
Kenton Varda committed
374 375 376 377 378 379 380 381 382 383 384

  for (int i = 1; i < argc; i++) {
    string arg = argv[i];
    if (isdigit(argv[i][0])) {
      iters = strtoul(argv[i], nullptr, 0);
    } else if (arg == "async") {
      mode = Mode::PIPE_ASYNC;
    } else if (arg == "inmem") {
      mode = Mode::BYTES;
    } else if (arg == "eval") {
      testCase = TestCase::EVAL;
Kenton Varda's avatar
Kenton Varda committed
385 386
    } else if (arg == "carsales") {
      testCase = TestCase::CARSALES;
Kenton Varda's avatar
Kenton Varda committed
387 388
    } else if (arg == "snappy") {
      compression = Compression::SNAPPY;
389 390 391 392 393 394 395
    } else if (arg == "-c") {
      ++i;
      if (i == argc) {
        fprintf(stderr, "-c requires argument.\n");
        return 1;
      }
      oldDir = argv[i];
Kenton Varda's avatar
Kenton Varda committed
396 397 398
    } else {
      fprintf(stderr, "Unknown option: %s\n", argv[i]);
      return 1;
Kenton Varda's avatar
Kenton Varda committed
399 400 401
    }
  }

402
  // Scale iterations to something reasonable for each case.
Kenton Varda's avatar
Kenton Varda committed
403 404 405 406 407 408 409
  switch (testCase) {
    case TestCase::EVAL:
      iters *= 100000;
      break;
    case TestCase::CATRANK:
      iters *= 1000;
      break;
Kenton Varda's avatar
Kenton Varda committed
410 411 412
    case TestCase::CARSALES:
      iters *= 20000;
      break;
Kenton Varda's avatar
Kenton Varda committed
413 414 415 416 417 418 419 420 421 422
  }

  cout << "Running " << iters << " iterations of ";
  switch (testCase) {
    case TestCase::EVAL:
      cout << "calculator";
      break;
    case TestCase::CATRANK:
      cout << "CatRank";
      break;
Kenton Varda's avatar
Kenton Varda committed
423 424 425
    case TestCase::CARSALES:
      cout << "car sales";
      break;
Kenton Varda's avatar
Kenton Varda committed
426 427 428 429 430 431 432 433 434 435
  }

  cout << " example case with:" << endl;

  switch (mode) {
    case Mode::OBJECTS:
    case Mode::OBJECT_SIZE:
      // Can't happen.
      break;
    case Mode::BYTES:
436
      cout << "* in-memory I/O" << endl;
David Renshaw's avatar
David Renshaw committed
437
      cout << "  * with client and server in the same thread" << endl;
Kenton Varda's avatar
Kenton Varda committed
438 439
      break;
    case Mode::PIPE_SYNC:
440 441 442
      cout << "* pipe I/O" << endl;
      cout << "  * with client and server in separate processes" << endl;
      cout << "  * client waits for each response before sending next request" << endl;
Kenton Varda's avatar
Kenton Varda committed
443 444
      break;
    case Mode::PIPE_ASYNC:
445 446 447
      cout << "* pipe I/O" << endl;
      cout << "  * with client and server in separate processes" << endl;
      cout << "  * client sends as many simultaneous requests as it can" << endl;
Kenton Varda's avatar
Kenton Varda committed
448 449 450 451 452 453
      break;
  }
  switch (compression) {
    case Compression::NONE:
      cout << "* no compression" << endl;
      break;
454 455 456 457 458 459 460
    case Compression::PACKED:
      cout << "* de-zero packing for Cap'n Proto" << endl;
      cout << "* standard packing for Protobuf" << endl;
      break;
    case Compression::SNAPPY:
      cout << "* Snappy compression" << endl;
      break;
Kenton Varda's avatar
Kenton Varda committed
461 462
  }

463 464
  cout << endl;

Kenton Varda's avatar
Kenton Varda committed
465 466 467
  reportTableHeader();

  TestResult nullCase = runTest(
468
      Product::NULLCASE, testCase, Mode::OBJECT_SIZE, Reuse::YES, compression, iters);
Kenton Varda's avatar
Kenton Varda committed
469 470 471
  reportResults("Theoretical best pass-by-object", iters, nullCase);

  TestResult protobufBase = runTest(
472 473 474
      Product::PROTOBUF, testCase, Mode::OBJECTS, Reuse::YES, compression, iters);
  protobufBase.objectSize = runTest(
      Product::PROTOBUF, testCase, Mode::OBJECT_SIZE, Reuse::YES, compression, iters).objectSize;
Kenton Varda's avatar
Kenton Varda committed
475 476 477
  reportResults("Protobuf pass-by-object", iters, protobufBase);

  TestResult capnpBase = runTest(
478 479 480
      Product::CAPNPROTO, testCase, Mode::OBJECTS, Reuse::YES, compression, iters);
  capnpBase.objectSize = runTest(
      Product::CAPNPROTO, testCase, Mode::OBJECT_SIZE, Reuse::YES, compression, iters).objectSize;
Kenton Varda's avatar
Kenton Varda committed
481 482
  reportResults("Cap'n Proto pass-by-object", iters, capnpBase);

483 484 485
  TestResult nullCaseNoReuse = runTest(
      Product::NULLCASE, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters);
  reportResults("Theoretical best w/o object reuse", iters, nullCaseNoReuse);
Kenton Varda's avatar
Kenton Varda committed
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
  TestResult protobufNoReuse = runTest(
      Product::PROTOBUF, testCase, Mode::OBJECTS, Reuse::NO, compression, iters);
  protobufNoReuse.objectSize = runTest(
      Product::PROTOBUF, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters).objectSize;
  reportResults("Protobuf w/o object reuse", iters, protobufNoReuse);

  TestResult capnpNoReuse = runTest(
      Product::CAPNPROTO, testCase, Mode::OBJECTS, Reuse::NO, compression, iters);
  capnpNoReuse.objectSize = runTest(
      Product::CAPNPROTO, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters).objectSize;
  reportResults("Cap'n Proto w/o object reuse", iters, capnpNoReuse);

  TestResult protobuf = runTest(
      Product::PROTOBUF, testCase, mode, Reuse::YES, compression, iters);
  protobuf.objectSize = protobufBase.objectSize;
  reportResults("Protobuf I/O", iters, protobuf);
503

Kenton Varda's avatar
Kenton Varda committed
504
  TestResult capnp = runTest(
505 506 507 508 509 510 511
      Product::CAPNPROTO, testCase, mode, Reuse::YES, compression, iters);
  capnp.objectSize = capnpBase.objectSize;
  reportResults("Cap'n Proto I/O", iters, capnp);
  TestResult capnpPacked = runTest(
      Product::CAPNPROTO, testCase, mode, Reuse::YES, Compression::PACKED, iters);
  capnpPacked.objectSize = capnpBase.objectSize;
  reportResults("Cap'n Proto packed I/O", iters, capnpPacked);
Kenton Varda's avatar
Kenton Varda committed
512

513 514 515 516 517 518 519 520 521
  size_t protobufBinarySize = fileSize("protobuf-" + std::string(testCaseName(testCase)));
  size_t capnpBinarySize = fileSize("capnproto-" + std::string(testCaseName(testCase)));
  size_t protobufCodeSize = fileSize(std::string(testCaseName(testCase)) + ".pb.cc")
                          + fileSize(std::string(testCaseName(testCase)) + ".pb.h");
  size_t capnpCodeSize = fileSize(std::string(testCaseName(testCase)) + ".capnp.c++")
                       + fileSize(std::string(testCaseName(testCase)) + ".capnp.h");
  size_t protobufObjSize = fileSize(std::string(testCaseName(testCase)) + ".pb.o");
  size_t capnpObjSize = fileSize(std::string(testCaseName(testCase)) + ".capnp.o");

Kenton Varda's avatar
Kenton Varda committed
522 523
  TestResult oldNullCase;
  TestResult oldNullCaseNoReuse;
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  TestResult oldCapnpBase;
  TestResult oldCapnpNoReuse;
  TestResult oldCapnp;
  TestResult oldCapnpPacked;
  size_t oldCapnpBinarySize = 0;
  size_t oldCapnpCodeSize = 0;
  size_t oldCapnpObjSize = 0;
  if (oldDir != nullptr) {
    if (chdir(origDir) < 0) {
      perror("chdir");
      return 1;
    }
    if (chdir(oldDir) < 0) {
      perror(oldDir);
      return 1;
    }
Kenton Varda's avatar
Kenton Varda committed
540 541 542 543 544

    oldNullCase = runTest(
        Product::NULLCASE, testCase, Mode::OBJECT_SIZE, Reuse::YES, compression, iters);
    reportResults("Old theoretical best pass-by-object", iters, nullCase);

545 546 547 548 549 550 551
    oldCapnpBase = runTest(
        Product::CAPNPROTO, testCase, Mode::OBJECTS, Reuse::YES, compression, iters);
    oldCapnpBase.objectSize = runTest(
        Product::CAPNPROTO, testCase, Mode::OBJECT_SIZE, Reuse::YES, compression, iters)
        .objectSize;
    reportResults("Old Cap'n Proto pass-by-object", iters, oldCapnpBase);

Kenton Varda's avatar
Kenton Varda committed
552 553 554 555
    oldNullCaseNoReuse = runTest(
        Product::NULLCASE, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters);
    reportResults("Old theoretical best w/o object reuse", iters, oldNullCaseNoReuse);

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    oldCapnpNoReuse = runTest(
        Product::CAPNPROTO, testCase, Mode::OBJECTS, Reuse::NO, compression, iters);
    oldCapnpNoReuse.objectSize = runTest(
        Product::CAPNPROTO, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters).objectSize;
    reportResults("Old Cap'n Proto w/o object reuse", iters, oldCapnpNoReuse);

    oldCapnp = runTest(
        Product::CAPNPROTO, testCase, mode, Reuse::YES, compression, iters);
    oldCapnp.objectSize = oldCapnpBase.objectSize;
    reportResults("Old Cap'n Proto I/O", iters, oldCapnp);
    oldCapnpPacked = runTest(
        Product::CAPNPROTO, testCase, mode, Reuse::YES, Compression::PACKED, iters);
    oldCapnpPacked.objectSize = oldCapnpBase.objectSize;
    reportResults("Old Cap'n Proto packed I/O", iters, oldCapnpPacked);

    oldCapnpBinarySize = fileSize("capnproto-" + std::string(testCaseName(testCase)));
    oldCapnpCodeSize = fileSize(std::string(testCaseName(testCase)) + ".capnp.c++")
                     + fileSize(std::string(testCaseName(testCase)) + ".capnp.h");
    oldCapnpObjSize = fileSize(std::string(testCaseName(testCase)) + ".capnp.o");
  }

Kenton Varda's avatar
Kenton Varda committed
577 578 579
  cout << endl;

  reportComparisonHeader();
580 581 582 583 584
  reportComparison("memory overhead (vs ideal)",
      nullCase.objectSize, protobufBase.objectSize, capnpBase.objectSize, iters);
  reportComparison("memory overhead w/o object reuse",
      nullCaseNoReuse.objectSize, protobufNoReuse.objectSize, capnpNoReuse.objectSize, iters);
  reportComparison("object manipulation time (us)", "",
585 586
      ((int64_t)protobufBase.time.user - (int64_t)nullCase.time.user) / 1000.0,
      ((int64_t)capnpBase.time.user - (int64_t)nullCase.time.user) / 1000.0, iters);
587
  reportComparison("object manipulation time w/o reuse (us)", "",
588 589
      ((int64_t)protobufNoReuse.time.user - (int64_t)nullCaseNoReuse.time.user) / 1000.0,
      ((int64_t)capnpNoReuse.time.user - (int64_t)nullCaseNoReuse.time.user) / 1000.0, iters);
590
  reportComparison("I/O time (us)", "",
591 592
      ((int64_t)protobuf.time.user - (int64_t)protobufBase.time.user) / 1000.0,
      ((int64_t)capnp.time.user - (int64_t)capnpBase.time.user) / 1000.0, iters);
593
  reportComparison("packed I/O time (us)", "",
594 595
      ((int64_t)protobuf.time.user - (int64_t)protobufBase.time.user) / 1000.0,
      ((int64_t)capnpPacked.time.user - (int64_t)capnpBase.time.user) / 1000.0, iters);
Kenton Varda's avatar
Kenton Varda committed
596

597 598 599
  reportIntComparison("message size (bytes)", "", protobuf.messageSize, capnp.messageSize, iters);
  reportIntComparison("packed message size (bytes)", "",
                      protobuf.messageSize, capnpPacked.messageSize, iters);
600

601
  reportComparison("binary size (KiB)", "",
602
      protobufBinarySize / 1024.0, capnpBinarySize / 1024.0, 1);
603
  reportComparison("generated code size (KiB)", "",
604
      protobufCodeSize / 1024.0, capnpCodeSize / 1024.0, 1);
605
  reportComparison("generated obj size (KiB)", "",
606 607 608
      protobufObjSize / 1024.0, capnpObjSize / 1024.0, 1);

  if (oldDir != nullptr) {
609 610 611
    cout << endl;
    reportOldNewComparisonHeader();

612
    reportComparison("memory overhead",
Kenton Varda's avatar
Kenton Varda committed
613
        oldNullCase.objectSize, oldCapnpBase.objectSize, capnpBase.objectSize, iters);
614
    reportComparison("memory overhead w/o object reuse",
Kenton Varda's avatar
Kenton Varda committed
615
        oldNullCaseNoReuse.objectSize, oldCapnpNoReuse.objectSize, capnpNoReuse.objectSize, iters);
616
    reportComparison("object manipulation time (us)", "",
Kenton Varda's avatar
Kenton Varda committed
617 618
        ((int64_t)oldCapnpBase.time.user - (int64_t)oldNullCase.time.user) / 1000.0,
        ((int64_t)capnpBase.time.user - (int64_t)oldNullCase.time.user) / 1000.0, iters);
619
    reportComparison("object manipulation time w/o reuse (us)", "",
Kenton Varda's avatar
Kenton Varda committed
620 621
        ((int64_t)oldCapnpNoReuse.time.user - (int64_t)oldNullCaseNoReuse.time.user) / 1000.0,
        ((int64_t)capnpNoReuse.time.user - (int64_t)oldNullCaseNoReuse.time.user) / 1000.0, iters);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    reportComparison("I/O time (us)", "",
        ((int64_t)oldCapnp.time.user - (int64_t)oldCapnpBase.time.user) / 1000.0,
        ((int64_t)capnp.time.user - (int64_t)capnpBase.time.user) / 1000.0, iters);
    reportComparison("packed I/O time (us)", "",
        ((int64_t)oldCapnpPacked.time.user - (int64_t)oldCapnpBase.time.user) / 1000.0,
        ((int64_t)capnpPacked.time.user - (int64_t)capnpBase.time.user) / 1000.0, iters);

    reportIntComparison("message size (bytes)", "", oldCapnp.messageSize, capnp.messageSize, iters);
    reportIntComparison("packed message size (bytes)", "",
                        oldCapnpPacked.messageSize, capnpPacked.messageSize, iters);

    reportComparison("binary size (KiB)", "",
        oldCapnpBinarySize / 1024.0, capnpBinarySize / 1024.0, 1);
    reportComparison("generated code size (KiB)", "",
        oldCapnpCodeSize / 1024.0, capnpCodeSize / 1024.0, 1);
    reportComparison("generated obj size (KiB)", "",
        oldCapnpObjSize / 1024.0, capnpObjSize / 1024.0, 1);
  }
Kenton Varda's avatar
Kenton Varda committed
640 641 642 643 644 645

  return 0;
}

}  // namespace runner
}  // namespace benchmark
646
}  // namespace capnp
Kenton Varda's avatar
Kenton Varda committed
647 648

int main(int argc, char* argv[]) {
649
  return capnp::benchmark::runner::main(argc, argv);
Kenton Varda's avatar
Kenton Varda committed
650
}