carsales by-object benchmark works.

This commit is contained in:
David Renshaw 2014-06-20 12:53:13 -04:00
parent 1a3561c0f7
commit 1fb0adb351
2 changed files with 48 additions and 4 deletions

View file

@ -1,5 +1,6 @@
package org.capnproto.benchmark; package org.capnproto.benchmark;
import org.capnproto.MessageBuilder;
import org.capnproto.StructList; import org.capnproto.StructList;
import org.capnproto.Text; import org.capnproto.Text;
import org.capnproto.benchmark.CarSalesSchema.*; import org.capnproto.benchmark.CarSalesSchema.*;
@ -86,4 +87,47 @@ public class CarSales {
car.setHasNavSystem(rng.nextLessThan(2) == 1); car.setHasNavSystem(rng.nextLessThan(2) == 1);
} }
public static long setupRequest(Common.FastRand rng, ParkingLot.Builder request) {
long result = 0;
StructList.Builder<Car.Builder> cars = request.initCars(rng.nextLessThan(200));
for (int i = 0; i < cars.size(); ++i) {
Car.Builder car = cars.get(i);
randomCar(rng, car);
result += carValue(car.asReader());
}
return result;
}
public static void handleRequest(ParkingLot.Reader request, TotalValue.Builder response) {
long result = 0;
StructList.Reader<Car.Reader> cars = request.getCars();
for (int i =0; i < cars.size(); ++i) {
result += carValue(cars.get(i));
}
response.setAmount(result);
}
public static boolean checkResponse(TotalValue.Reader response, long expected) {
return response.getAmount() == expected;
}
public static void main(String[] args) {
Common.FastRand rng = new Common.FastRand();
for (int i = 0; i < 50000; ++i) {
MessageBuilder requestMessage = new MessageBuilder();
MessageBuilder responseMessage = new MessageBuilder();
ParkingLot.Builder request = requestMessage.initRoot(ParkingLot.Builder.factory);
long expected = setupRequest(rng, request);
TotalValue.Builder response = responseMessage.initRoot(TotalValue.Builder.factory);
handleRequest(request.asReader(), response);
if (!checkResponse(response.asReader(), expected)) {
System.out.println("mismatch!");
}
}
}
} }

View file

@ -16,13 +16,13 @@ public class Common {
} }
public int nextLessThan(int range) { public int nextLessThan(int range) {
// sign? // just chop off the sign bit.
return this.nextInt() % range; return (0x7fffffff & this.nextInt()) % range;
} }
public double nextDouble(double range) { public double nextDouble(double range) {
// XXX sign? // just chop off the sign bit.
return (double) this.nextInt() * range / (double)(0xffffffffL); return (double) (0x7fffffff & this.nextInt()) * range / (double)(0x7fffffff);
} }
} }