null-carsales.c++ 5.73 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:
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:
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.
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.
21 22 23

#include "null-common.h"

24
namespace capnp {
25 26 27
namespace benchmark {
namespace null {

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

struct Wheel {
  float airPressure;
43
  uint16_t diameter;
44 45 46 47
  bool snowTires;
};

struct Engine {
48
  uint32_t cc;
49 50
  uint16_t horsepower;
  uint8_t cylinders;
51 52 53 54 55 56 57
  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);
  }
58 59 60
};

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

  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);
  }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
};


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;
110 111
  if (engine.usesElectric()) {
    if (engine.usesGas()) {
112 113 114 115 116 117 118
      // hybrid
      result += 5000;
    } else {
      result += 3000;
    }
  }

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

  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);
156
  car->engine.setBits(true, fastRand(2));
157 158 159

  car->fuelCapacity = 10.0 + fastRandDouble(30.0);
  car->fuelLevel = fastRandDouble(car->fuelCapacity);
160 161 162
  bool hasPowerWindows = fastRand(2);
  bool hasPowerSteering = fastRand(2);
  bool hasCruiseControl = fastRand(2);
163
  car->cupHolders = fastRand(12);
164 165
  bool hasNavSystem = fastRand(2);
  car->setBits(hasPowerWindows, hasPowerSteering, hasCruiseControl, hasNavSystem);
166 167 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
}

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
195
}  // namespace capnp
196 197

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