null-carsales.c++ 5.92 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
// 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 "null-common.h"

26
namespace capnp {
27 28 29
namespace benchmark {
namespace null {

30
enum class Color: uint8_t {
31 32 33 34 35 36 37 38 39 40 41 42 43 44
  BLACK,
  WHITE,
  RED,
  GREEN,
  BLUE,
  CYAN,
  MAGENTA,
  YELLOW,
  SILVER
};
constexpr uint COLOR_RANGE = static_cast<uint>(Color::SILVER) + 1;

struct Wheel {
  float airPressure;
45
  uint16_t diameter;
46 47 48 49
  bool snowTires;
};

struct Engine {
50
  uint32_t cc;
51 52
  uint16_t horsepower;
  uint8_t cylinders;
53 54 55 56 57 58 59
  uint8_t bits;
  inline bool usesGas()      const { return bits & 1; }
  inline bool usesElectric() const { return bits & 2; }

  inline void setBits(bool usesGas, bool usesElectric) {
    bits = (uint8_t)usesGas | ((uint8_t)usesElectric << 1);
  }
60 61 62
};

struct Car {
63 64 65
  // SORT FIELDS BY SIZE since we need "theoretical best" memory usage
  Engine engine;
  List<Wheel> wheels;
66 67
  const char* make;
  const char* model;
68 69 70
  float fuelCapacity;
  float fuelLevel;
  uint32_t weight;
71 72 73
  uint16_t length;
  uint16_t width;
  uint16_t height;
74 75 76
  Color color;
  uint8_t seats;
  uint8_t doors;
77
  uint8_t cupHolders;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  uint8_t bits;

  inline bool hasPowerWindows()  const { return bits & 1; }
  inline bool hasPowerSteering() const { return bits & 2; }
  inline bool hasCruiseControl() const { return bits & 4; }
  inline bool hasNavSystem()     const { return bits & 8; }

  inline void setBits(bool hasPowerWindows, bool hasPowerSteering,
                      bool hasCruiseControl, bool hasNavSystem) {
    bits = (uint8_t)hasPowerWindows
         | ((uint8_t)hasPowerSteering << 1)
         | ((uint8_t)hasCruiseControl << 2)
         | ((uint8_t)hasNavSystem << 3);
  }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
};


uint64_t carValue(const Car& car) {
  // Do not think too hard about realism.

  uint64_t result = 0;

  result += car.seats * 200;
  result += car.doors * 350;
  for (auto wheel: car.wheels) {
    result += wheel.diameter * wheel.diameter;
    result += wheel.snowTires ? 100 : 0;
  }

  result += car.length * car.width * car.height / 50;

  auto engine = car.engine;
  result += engine.horsepower * 40;
112 113
  if (engine.usesElectric()) {
    if (engine.usesGas()) {
114 115 116 117 118 119 120
      // hybrid
      result += 5000;
    } else {
      result += 3000;
    }
  }

121 122 123 124
  result += car.hasPowerWindows() ? 100 : 0;
  result += car.hasPowerSteering() ? 200 : 0;
  result += car.hasCruiseControl() ? 400 : 0;
  result += car.hasNavSystem() ? 2000 : 0;
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

  result += car.cupHolders * 25;

  return result;
}

void randomCar(Car* car) {
  // Do not think too hard about realism.

  static const char* const MAKES[] = { "Toyota", "GM", "Ford", "Honda", "Tesla" };
  static const char* const MODELS[] = { "Camry", "Prius", "Volt", "Accord", "Leaf", "Model S" };

  car->make = copyString(MAKES[fastRand(sizeof(MAKES) / sizeof(MAKES[0]))]);
  car->model = copyString(MODELS[fastRand(sizeof(MODELS) / sizeof(MODELS[0]))]);

  car->color = (Color)fastRand(COLOR_RANGE);
  car->seats = 2 + fastRand(6);
  car->doors = 2 + fastRand(3);

  for (auto& wheel: car->wheels.init(4)) {
    wheel.diameter = 25 + fastRand(15);
    wheel.airPressure = 30 + fastRandDouble(20);
    wheel.snowTires = fastRand(16) == 0;
  }

  car->length = 170 + fastRand(150);
  car->width = 48 + fastRand(36);
  car->height = 54 + fastRand(48);
  car->weight = car->length * car->width * car->height / 200;

  car->engine.horsepower = 100 * fastRand(400);
  car->engine.cylinders = 4 + 2 * fastRand(3);
  car->engine.cc = 800 + fastRand(10000);
158
  car->engine.setBits(true, fastRand(2));
159 160 161

  car->fuelCapacity = 10.0 + fastRandDouble(30.0);
  car->fuelLevel = fastRandDouble(car->fuelCapacity);
162 163 164
  bool hasPowerWindows = fastRand(2);
  bool hasPowerSteering = fastRand(2);
  bool hasCruiseControl = fastRand(2);
165
  car->cupHolders = fastRand(12);
166 167
  bool hasNavSystem = fastRand(2);
  car->setBits(hasPowerWindows, hasPowerSteering, hasCruiseControl, hasNavSystem);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

class CarSalesTestCase {
public:
  typedef List<Car> Request;
  typedef uint64_t Response;
  typedef uint64_t Expectation;

  static uint64_t setupRequest(List<Car>* request) {
    uint64_t result = 0;
    for (auto& car: request->init(fastRand(200))) {
      randomCar(&car);
      result += carValue(car);
    }
    return result;
  }
  static void handleRequest(const List<Car>& request, uint64_t* response) {
    *response = 0;
    for (auto& car: request) {
      *response += carValue(car);
    }
  }
  static inline bool checkResponse(uint64_t response, uint64_t expected) {
    return response == expected;
  }
};

}  // namespace null
}  // namespace benchmark
197
}  // namespace capnp
198 199

int main(int argc, char* argv[]) {
200 201 202
  return capnp::benchmark::benchmarkMain<
      capnp::benchmark::null::BenchmarkTypes,
      capnp::benchmark::null::CarSalesTestCase>(argc, argv);
203
}