carsales by-object benchmark works.
This commit is contained in:
parent
1a3561c0f7
commit
1fb0adb351
2 changed files with 48 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
|||
package org.capnproto.benchmark;
|
||||
|
||||
import org.capnproto.MessageBuilder;
|
||||
import org.capnproto.StructList;
|
||||
import org.capnproto.Text;
|
||||
import org.capnproto.benchmark.CarSalesSchema.*;
|
||||
|
@ -86,4 +87,47 @@ public class CarSales {
|
|||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,13 +16,13 @@ public class Common {
|
|||
}
|
||||
|
||||
public int nextLessThan(int range) {
|
||||
// sign?
|
||||
return this.nextInt() % range;
|
||||
// just chop off the sign bit.
|
||||
return (0x7fffffff & this.nextInt()) % range;
|
||||
}
|
||||
|
||||
public double nextDouble(double range) {
|
||||
// XXX sign?
|
||||
return (double) this.nextInt() * range / (double)(0xffffffffL);
|
||||
// just chop off the sign bit.
|
||||
return (double) (0x7fffffff & this.nextInt()) * range / (double)(0x7fffffff);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue