Skip to content

Commit

Permalink
Extract separate test for transfer cache
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jan 14, 2025
1 parent 041d1bb commit 25ffb73
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.opentripplanner.transit.speed_test;

import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.standalone.config.BuildConfig;
import org.opentripplanner.transit.service.TimetableRepository;

record LoadModel(Graph graph, TimetableRepository timetableRepository, BuildConfig buildConfig) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.opentripplanner.transit.speed_test;

import java.io.File;
import java.net.URI;
import org.opentripplanner.datastore.OtpDataStore;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.SerializedGraphObject;
import org.opentripplanner.standalone.config.ConfigModel;
import org.opentripplanner.standalone.config.OtpConfigLoader;
import org.opentripplanner.transit.service.TimetableRepository;
import org.opentripplanner.transit.speed_test.options.SpeedTestCmdLineOpts;

/**
* A package-private helper class for setting up speed tests.
*/
class SetupHelper {

static LoadModel loadGraph(File baseDir, URI path) {
File file = path == null
? OtpDataStore.graphFile(baseDir)
: path.isAbsolute() ? new File(path) : new File(baseDir, path.getPath());
SerializedGraphObject serializedGraphObject = SerializedGraphObject.load(file);
Graph graph = serializedGraphObject.graph;

if (graph == null) {
throw new IllegalStateException();
}

TimetableRepository timetableRepository = serializedGraphObject.timetableRepository;
timetableRepository.index();
graph.index(timetableRepository.getSiteRepository());
return new LoadModel(graph, timetableRepository, serializedGraphObject.buildConfig);
}

static void loadOtpFeatures(SpeedTestCmdLineOpts opts) {
ConfigModel.initializeOtpFeatures(new OtpConfigLoader(opts.rootDir()).loadOtpConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import org.opentripplanner.TestServerContext;
import org.opentripplanner.datastore.OtpDataStore;
import org.opentripplanner.framework.application.OtpAppException;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.raptor.configure.RaptorConfig;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.response.RoutingResponse;
import org.opentripplanner.routing.framework.DebugTimingAggregator;
import org.opentripplanner.routing.graph.Graph;
Expand All @@ -29,7 +27,6 @@
import org.opentripplanner.service.vehiclerental.internal.DefaultVehicleRentalService;
import org.opentripplanner.standalone.OtpStartupInfo;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.standalone.config.BuildConfig;
import org.opentripplanner.standalone.config.ConfigModel;
import org.opentripplanner.standalone.config.DebugUiConfig;
import org.opentripplanner.standalone.config.OtpConfigLoader;
Expand Down Expand Up @@ -193,8 +190,6 @@ public void runTest() {
}
}

measureTransferCacheComputation();

updateTimersWithGlobalCounters();

timer.finishUp();
Expand Down Expand Up @@ -342,22 +337,6 @@ private void saveTestCasesToResultFile() {
}
}

/**
* Measure how long it takes to compute the transfer cache.
*/
private void measureTransferCacheComputation() {
IntStream
.of(1, 2, 3, 4, 5, 6, 7)
.forEach(reluctance -> {
RouteRequest routeRequest = new RouteRequest();
routeRequest.withPreferences(b -> b.withWalk(c -> c.withReluctance(reluctance)));
timer.recordTimer(
"transfer_cache_computation",
() -> timetableRepository.getTransitLayer().initTransferCacheForRequest(routeRequest)
);
});
}

/**
* Add "static" transit statistics and JVM memory usages to the "timers" logging.
*/
Expand Down Expand Up @@ -390,8 +369,4 @@ private List<Itinerary> trimItineraries(RoutingResponse routingResponse) {
}
return stream.limit(opts.numOfItineraries()).toList();
}

/* inline classes */

record LoadModel(Graph graph, TimetableRepository timetableRepository, BuildConfig buildConfig) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.opentripplanner.transit.speed_test;

import static org.opentripplanner.standalone.configure.ConstructApplication.creatTransitLayerForRaptor;
import static org.opentripplanner.transit.speed_test.support.AssertSpeedTestSetup.assertTestDateHasData;

import java.util.stream.IntStream;
import org.opentripplanner.framework.application.OtpAppException;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.standalone.OtpStartupInfo;
import org.opentripplanner.transit.service.TimetableRepository;
import org.opentripplanner.transit.speed_test.model.timer.SpeedTestTimer;
import org.opentripplanner.transit.speed_test.options.SpeedTestCmdLineOpts;
import org.opentripplanner.transit.speed_test.options.SpeedTestConfig;

/**
* Test how long it takes to compute the transfer cache.
*/
public class TransferCacheTest {

public static void main(String[] args) {
try {
OtpStartupInfo.logInfo("Run transfer cache test");
// Given the following setup
SpeedTestCmdLineOpts opts = new SpeedTestCmdLineOpts(args);
var config = SpeedTestConfig.config(opts.rootDir());
SetupHelper.loadOtpFeatures(opts);
var model = SetupHelper.loadGraph(opts.rootDir(), config.graph);
var timetableRepository = model.timetableRepository();
var buildConfig = model.buildConfig();

var timer = new SpeedTestTimer();

assertTestDateHasData(timetableRepository, config, buildConfig);

// Creating transitLayerForRaptor should be integrated into the TimetableRepository, but for now
// we do it manually here
creatTransitLayerForRaptor(timetableRepository, config.transitRoutingParams);

measureTransferCacheComputation(timer, timetableRepository);

timer.setUp(false);
timer.finishUp();
} catch (OtpAppException ae) {
System.err.println(ae.getMessage());
System.exit(1);
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
System.exit(1);
}
}

/**
* Measure how long it takes to compute the transfer cache.
*/
private static void measureTransferCacheComputation(
SpeedTestTimer timer,
TimetableRepository timetableRepository
) {
IntStream
.range(1, 7)
.forEach(reluctance -> {
RouteRequest routeRequest = new RouteRequest();
routeRequest.withPreferences(b -> b.withWalk(c -> c.withReluctance(reluctance)));
timer.recordTimer(
"transfer_cache_computation",
() -> timetableRepository.getTransitLayer().initTransferCacheForRequest(routeRequest)
);
});
}
}

0 comments on commit 25ffb73

Please sign in to comment.