runner.c++ 20.9 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Kenton Varda's avatar
Kenton Varda committed
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Kenton Varda's avatar
Kenton Varda committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

#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;

37
namespace capnp {
Kenton Varda's avatar
Kenton Varda committed
38 39 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
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 {
79 80
  uint64_t objectSize;
  uint64_t messageSize;
Kenton Varda's avatar
Kenton Varda committed
81 82 83 84 85 86 87 88 89 90 91
  Times time;
};

enum class Product {
  CAPNPROTO,
  PROTOBUF,
  NULLCASE
};

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

96 97 98 99 100 101 102 103 104 105 106 107 108
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
109 110 111 112 113 114 115 116 117 118 119 120 121 122
enum class Mode {
  OBJECTS,
  OBJECT_SIZE,
  BYTES,
  PIPE_SYNC,
  PIPE_ASYNC
};

enum class Reuse {
  YES,
  NO
};

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

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

  string progName;
Kenton Varda's avatar
Kenton Varda committed
133 134 135

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

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

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

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

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

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

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

  // 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
214
  for (int i = 0; i < 4; i++) {
Kenton Varda's avatar
Kenton Varda committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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;
239 240
  result.objectSize = mode == Mode::OBJECT_SIZE ? throughput : 0;
  result.messageSize = mode == Mode::OBJECT_SIZE ? 0 : throughput;
Kenton Varda's avatar
Kenton Varda committed
241 242 243 244 245 246 247 248 249
  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"
250
       << setw(10) << right << "obj size"
Kenton Varda's avatar
Kenton Varda committed
251 252 253 254 255
       << setw(10) << right << "I/O bytes"
       << setw(10) << right << "wall ns"
       << setw(10) << right << "user ns"
       << setw(10) << right << "sys ns"
       << endl;
256
  cout << setfill('=') << setw(90) << "" << setfill(' ') << endl;
Kenton Varda's avatar
Kenton Varda committed
257 258 259 260
}

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

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

278 279 280 281 282 283 284 285 286
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
287 288 289
class Gain {
public:
  Gain(double oldValue, double newValue)
Kenton Varda's avatar
Kenton Varda committed
290
      : amount(newValue / oldValue) {}
Kenton Varda's avatar
Kenton Varda committed
291 292

  void writeTo(std::ostream& os) {
Kenton Varda's avatar
Kenton Varda committed
293 294
    if (amount < 2) {
      double percent = (amount - 1) * 100;
Kenton Varda's avatar
Kenton Varda committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
      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,
311
                      uint64_t iters) {
312
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
313 314 315 316 317 318 319 320
       << 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,
321
                      uint64_t iters) {
322
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
323 324 325 326 327 328 329 330 331
       << 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) {
332
  cout << setw(40) << left << name
Kenton Varda's avatar
Kenton Varda committed
333 334
       << setw(15-strlen(unit)) << right << (protobuf / iters) << unit
       << setw(15-strlen(unit)) << right << (capnproto / iters) << unit;
Kenton Varda's avatar
Kenton Varda committed
335 336 337 338 339

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

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

  return stats.st_size;
}

int main(int argc, char* argv[]) {
  char* path = argv[0];
  char* slashpos = strrchr(path, '/');
353 354 355 356 357
  char origDir[1024];
  if (getcwd(origDir, sizeof(origDir)) == nullptr) {
    perror("getcwd");
    return 1;
  }
Kenton Varda's avatar
Kenton Varda committed
358 359 360 361 362 363 364 365 366 367 368 369 370
  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;
371
  const char* oldDir = nullptr;
Kenton Varda's avatar
Kenton Varda committed
372 373 374 375 376 377 378 379 380 381 382

  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
383 384
    } else if (arg == "carsales") {
      testCase = TestCase::CARSALES;
Kenton Varda's avatar
Kenton Varda committed
385 386
    } else if (arg == "snappy") {
      compression = Compression::SNAPPY;
387 388 389 390 391 392 393
    } 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
394 395 396
    } else {
      fprintf(stderr, "Unknown option: %s\n", argv[i]);
      return 1;
Kenton Varda's avatar
Kenton Varda committed
397 398 399
    }
  }

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

  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
421 422 423
    case TestCase::CARSALES:
      cout << "car sales";
      break;
Kenton Varda's avatar
Kenton Varda committed
424 425 426 427 428 429 430 431 432 433
  }

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

  switch (mode) {
    case Mode::OBJECTS:
    case Mode::OBJECT_SIZE:
      // Can't happen.
      break;
    case Mode::BYTES:
434
      cout << "* in-memory I/O" << endl;
David Renshaw's avatar
David Renshaw committed
435
      cout << "  * with client and server in the same thread" << endl;
Kenton Varda's avatar
Kenton Varda committed
436 437
      break;
    case Mode::PIPE_SYNC:
438 439 440
      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
441 442
      break;
    case Mode::PIPE_ASYNC:
443 444 445
      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
446 447 448 449 450 451
      break;
  }
  switch (compression) {
    case Compression::NONE:
      cout << "* no compression" << endl;
      break;
452 453 454 455 456 457 458
    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
459 460
  }

461 462
  cout << endl;

Kenton Varda's avatar
Kenton Varda committed
463 464 465
  reportTableHeader();

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

  TestResult protobufBase = runTest(
470 471 472
      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
473 474 475
  reportResults("Protobuf pass-by-object", iters, protobufBase);

  TestResult capnpBase = runTest(
476 477 478
      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
479 480
  reportResults("Cap'n Proto pass-by-object", iters, capnpBase);

481 482 483
  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
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  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);
501

Kenton Varda's avatar
Kenton Varda committed
502
  TestResult capnp = runTest(
503 504 505 506 507 508 509
      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
510

511 512 513 514 515 516 517 518 519
  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
520 521
  TestResult oldNullCase;
  TestResult oldNullCaseNoReuse;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
  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
538 539 540 541 542

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

543 544 545 546 547 548 549
    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
550 551 552 553
    oldNullCaseNoReuse = runTest(
        Product::NULLCASE, testCase, Mode::OBJECT_SIZE, Reuse::NO, compression, iters);
    reportResults("Old theoretical best w/o object reuse", iters, oldNullCaseNoReuse);

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    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
575 576 577
  cout << endl;

  reportComparisonHeader();
578 579 580 581 582
  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)", "",
583 584
      ((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);
585
  reportComparison("object manipulation time w/o reuse (us)", "",
586 587
      ((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);
588
  reportComparison("I/O time (us)", "",
589 590
      ((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);
591
  reportComparison("packed I/O time (us)", "",
592 593
      ((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
594

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

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

  if (oldDir != nullptr) {
607 608 609
    cout << endl;
    reportOldNewComparisonHeader();

610
    reportComparison("memory overhead",
Kenton Varda's avatar
Kenton Varda committed
611
        oldNullCase.objectSize, oldCapnpBase.objectSize, capnpBase.objectSize, iters);
612
    reportComparison("memory overhead w/o object reuse",
Kenton Varda's avatar
Kenton Varda committed
613
        oldNullCaseNoReuse.objectSize, oldCapnpNoReuse.objectSize, capnpNoReuse.objectSize, iters);
614
    reportComparison("object manipulation time (us)", "",
Kenton Varda's avatar
Kenton Varda committed
615 616
        ((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);
617
    reportComparison("object manipulation time w/o reuse (us)", "",
Kenton Varda's avatar
Kenton Varda committed
618 619
        ((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);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
    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
638 639 640 641 642 643

  return 0;
}

}  // namespace runner
}  // namespace benchmark
644
}  // namespace capnp
Kenton Varda's avatar
Kenton Varda committed
645 646

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