1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 module capnproto.benchmark.CarSales;
23 
24 import capnproto.StructList;
25 import capnproto.Text;
26 
27 import capnproto.benchmark.carsalesschema;
28 import capnproto.benchmark.Common;
29 import capnproto.benchmark.TestCase;
30 
31 void main(string[] args)
32 {
33 	auto testCase = new CarSales();
34 	testCase.execute(args);
35 }
36 
37 final class CarSales : TestCase!(ParkingLot, TotalValue, long)
38 {
39 public: //Methods.
40 	override long setupRequest(ParkingLot.Builder request)
41 	{
42 		long result = 0;
43 		auto cars = request.initCars(fastRand(200));
44 		foreach(car; cars)
45 		{
46 			randomCar(car);
47 			result += carValue(car.asReader());
48 		}
49 		return result;
50 	}
51 	
52 	override void handleRequest(ParkingLot.Reader request, TotalValue.Builder response)
53 	{
54 		long result = 0;
55 		foreach(car; request.getCars())
56 			result += carValue(car);
57 		response.setAmount(result);
58 	}
59 	
60 	override bool checkResponse(TotalValue.Reader response, long expected)
61 	{
62 		return response.getAmount() == expected;
63 	}
64 
65 package: //Methods.
66 	long carValue(Car.Reader car)
67 	{
68 		long result = 0;
69 		result += car.getSeats() * 200;
70 		result += car.getDoors() * 350;
71 		
72 		foreach(wheel; car.getWheels())
73 		{
74 			result += cast(long)wheel.getDiameter() * cast(long)wheel.getDiameter();
75 			result += wheel.getSnowTires()? 100 : 0;
76 		}
77 		
78 		result += cast(long)car.getLength() * cast(long)car.getWidth() * cast(long)car.getHeight() / 50;
79 		
80 		auto engine = car.getEngine();
81 		result += cast(long)engine.getHorsepower() * 40;
82 		if(engine.getUsesElectric())
83 			result += engine.getUsesGas()? 5000 : 3000;
84 		
85 		result += car.getHasPowerWindows()? 100 : 0;
86 		result += car.getHasPowerSteering()? 200 : 0;
87 		result += car.getHasCruiseControl()? 400 : 0;
88 		result += car.getHasNavSystem()? 2000 : 0;
89 		
90 		result += cast(long)car.getCupHolders() * 25;
91 		
92 		return result;
93 	}
94 	
95 	void randomCar(Car.Builder car)
96 	{
97 		static string[] MAKES = [ "Toyota", "GM", "Ford", "Honda", "Tesla" ];
98 		static string[] MODELS = [ "Camry", "Prius", "Volt", "Accord", "Leaf", "Model S" ];
99 		
100 		car.setMake(MAKES[fastRand(cast(uint)MAKES.length)]);
101 		car.setModel(MODELS[fastRand(cast(uint)MODELS.length)]);
102 		
103 		car.setColor(cast(Color)fastRand(cast(uint)Color.silver + 1));
104 		car.setSeats(cast(ubyte)(2 + fastRand(6)));
105 		car.setDoors(cast(ubyte)(2 + fastRand(3)));
106 		
107 		foreach(wheel; car.initWheels(4))
108 		{
109 			wheel.setDiameter(cast(short)(25 + fastRand(15)));
110 			wheel.setAirPressure(cast(float)(30 + fastRandDouble(20)));
111 			wheel.setSnowTires(fastRand(16) == 0);
112 		}
113 		
114 		car.setLength(cast(short)(170 + fastRand(150)));
115 		car.setWidth(cast(short)(48 + fastRand(36)));
116 		car.setHeight(cast(short)(54 + fastRand(48)));
117 		car.setWeight(car.getLength() * car.getWidth() * car.getHeight() / 200);
118 		
119 		auto engine = car.initEngine();
120 		engine.setHorsepower(cast(short)(100 * fastRand(400)));
121 		engine.setCylinders(cast(byte)(4 + 2 * fastRand(3)));
122 		engine.setCc(800 + fastRand(10000));
123 		engine.setUsesGas(true);
124 		engine.setUsesElectric(fastRand(2) == 1);
125 		
126 		car.setFuelCapacity(cast(float)(10.0 + fastRandDouble(30.0)));
127 		car.setFuelLevel(cast(float)(fastRandDouble(car.getFuelCapacity())));
128 		car.setHasPowerWindows(fastRand(2) == 1);
129 		car.setHasPowerSteering(fastRand(2) == 1);
130 		car.setHasCruiseControl(fastRand(2) == 1);
131 		car.setCupHolders(cast(byte)fastRand(12));
132 		car.setHasNavSystem(fastRand(2) == 1);
133 	}
134 }