Skip to content

Commit

Permalink
🐛 Fix benchmark code
Browse files Browse the repository at this point in the history
  • Loading branch information
zeon256 committed Jan 26, 2024
1 parent 50b0592 commit b7b4802
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 74 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ This repository contains the data structures required to interact with LTA's dat
lta-models = { version = "0.6.0" }
```

## Supported formats

| Format | Supported? | Tested? |
| ----------- | ---------- | ------- |
| JSON |||
| Bincode |||
| Flexbuffer |||
| MessagePack |||


## Performance & `fast-float` implementation
Some of the deserialization code _may_ benefit from using the `fastfloat` feature, but during testing the biggest performance improvement can be seen when you swap out the system allocator to something faster like [`mimalloc`](https://github.com/microsoft/mimalloc) or [`jemalloc`](https://github.com/jemalloc/jemalloc)

Expand Down
141 changes: 72 additions & 69 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ static GLOBAL: MiMalloc = MiMalloc;

#[rustfmt::skip]
mod de {
use std::fmt::Debug;

use lta_models::prelude::*;
use lta_models::bus::bus_arrival::NextBusRaw;
use serde::{Deserialize, Serialize};
Expand All @@ -17,7 +19,7 @@ mod de {
where
F: FnOnce() -> &'de str,
I: Deserialize<'de> + Into<S>,
S: Serialize,
S: Serialize + Debug
{
let str_data = input_fn();
serde_json::from_str(str_data).map(|v: I| v.into()).unwrap()
Expand All @@ -34,26 +36,26 @@ mod de {
}

pub fn bus_arrival() -> BusArrivalResp {
gen!(RawBusArrivalResp, _, "../dumped_data/bus_arrival.json")
gen!(BusArrivalRespRaw, _, "../dumped_data/bus_arrival.json")
}

pub fn bus_routes() -> Vec<BusRouteRaw> {
pub fn bus_routes() -> Vec<BusRoute> {
gen!(BusRouteResp, _, "../dumped_data/bus_route.json")
}

pub fn bus_service() -> Vec<BusServiceRaw> {
pub fn bus_service() -> Vec<BusService> {
gen!(BusServiceResp, _, "../dumped_data/bus_services.json")
}

pub fn bus_stops() -> Vec<BusStopRaw> {
pub fn bus_stops() -> Vec<BusStop> {
gen!(BusStopsResp, _, "../dumped_data/bus_stops.json")
}

pub fn carpark_avail() -> Vec<CarParkRaw> {
pub fn carpark_avail() -> Vec<CarPark> {
gen!(CarparkAvailResp, _, "../dumped_data/carpark_avail.json")
}

pub fn erp_rates() -> Vec<ErpRateRaw> {
pub fn erp_rates() -> Vec<ErpRate> {
gen!(ErpRatesResp, _, "../dumped_data/erp_rates.json")
}

Expand Down Expand Up @@ -81,23 +83,23 @@ mod de {
gen!(TaxiAvailResp, _, "../dumped_data/taxi_avail.json")
}

pub fn taxi_stands() -> Vec<TaxiStandRaw> {
pub fn taxi_stands() -> Vec<TaxiStand> {
gen!(TaxiStandsResp, _, "../dumped_data/taxi_stands.json")
}

pub fn train_service_alert() -> TrainServiceAlert {
gen!(TrainServiceAlertResp,_,"../dumped_data/train_service_alert.json")
}

pub fn est_travel_time() -> Vec<EstTravelTimeRaw> {
pub fn est_travel_time() -> Vec<EstTravelTime> {
gen!(EstTravelTimeResp, _, "../dumped_data/est_travel_time.json")
}

pub fn crowd_density_rt() -> Vec<StationCrowdLevelRaw> {
pub fn crowd_density_rt() -> Vec<StationCrowdLevel> {
gen!(StationCrowdLevelRawResp, _, "../dumped_data/crowd_density_rt.json")
}

pub fn crowd_density_forecast() -> CrowdDensityForecastRaw {
pub fn crowd_density_forecast() -> CrowdDensityForecast {
gen!(CrowdDensityForecastRawResp, _, "../dumped_data/crowd_density_forecast.json")
}

Expand All @@ -122,64 +124,65 @@ pub fn criterion_benchmark(c: &mut Criterion) {
feature: BusFeature::WheelChairAccessible,
bus_type: BusType::SingleDecker,
};
c.bench_function("transmute_nextbus", |b| b.iter(|| de::transmute_nextbus(black_box(sample_data.clone()))));
c.bench_function("into_nextbus", |b| b.iter(|| NextBus::from(black_box(sample_data.clone()))));
// c.bench_function("de::bike_parking.json", |b| b.iter(|| de::bike_parking()));
// c.bench_function("de::bus_arrival.json", |b| b.iter(|| de::bus_arrival()));
// c.bench_function("de::bus_route.json", |b| b.iter(|| de::bus_routes()));
// c.bench_function("de::bus_services.json", |b| b.iter(|| de::bus_service()));
// c.bench_function("de::bus_stops.json", |b| b.iter(|| de::bus_stops()));
// c.bench_function("de::carpark_avail.json", |b| b.iter(|| de::carpark_avail()));
// c.bench_function("de::erp_rates.json", |b| b.iter(|| de::erp_rates()));
// c.bench_function("de::est_travel_time.json", |b| b.iter(|| de::est_travel_time()));
// c.bench_function("de::faulty_traffic_lights.json", |b| b.iter(|| de::faulty_traffic_lights()));
// c.bench_function("de::passenger_vol_bus_stops.json", |b| b.iter(|| de::passenger_vol_bus_stops()));
// c.bench_function("de::passenger_od_bus_stops.json", |b| b.iter(|| de::passenger_vol_od_bus_stops()));
// c.bench_function("de::passenger_vol_od_train.json", |b| b.iter(|| de::passenger_vol_od_train()));
// c.bench_function("de::passenger_vol_train.json", |b| b.iter(|| de::passenger_vol_train()));
// c.bench_function("de::taxi_avail.json", |b| b.iter(|| de::taxi_avail()));
// c.bench_function("de::taxi_stands.json", |b| b.iter(|| de::taxi_stands()));
// c.bench_function("de::train_service_alert.json", |b| b.iter(|| de::train_service_alert()));
// c.bench_function("de::crowd_density_rt.json", |b| b.iter(|| de::crowd_density_rt()));
// c.bench_function("de::crowd_density_forecast.json", |b| b.iter(|| de::crowd_density_forecast()));

// let bike_parking = de::bike_parking();
// let bus_arrival = de::bus_arrival();
// let bus_routes = de::bus_routes();
// let bus_service = de::bus_service();
// let bus_stops = de::bus_stops();
// let carpark_avail = de::carpark_avail();
// let erp_rates = de::erp_rates();
// let est_travel_time = de::est_travel_time();
// let faulty_traffic_lights = de::faulty_traffic_lights();
// let passenger_vol_bus_stops = de::passenger_vol_bus_stops();
// let passenger_vol_od_bus_stops = de::passenger_vol_od_bus_stops();
// let passenger_vol_train = de::passenger_vol_train();
// let passenger_vol_od_train = de::passenger_vol_od_train();
// let taxi_avail = de::taxi_avail();
// let taxi_stands = de::taxi_stands();
// let train_service_alert = de::train_service_alert();
// let crowd_density_rt = de::crowd_density_rt();
// let crowd_density_forecast = de::crowd_density_forecast();

// c.bench_function("ser::bike_parking.json", |b| b.iter(|| serde_json::to_string(black_box(&bike_parking))));
// c.bench_function("ser::bus_arrival.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_arrival))));
// c.bench_function("ser::bus_route.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_routes))));
// c.bench_function("ser::bus_services.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_service))));
// c.bench_function("ser::bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_stops))));
// c.bench_function("ser::carpark_avail.json", |b| b.iter(|| serde_json::to_string(black_box(&carpark_avail))));
// c.bench_function("ser::erp_rates.json", |b| b.iter(|| serde_json::to_string(black_box(&erp_rates))));
// c.bench_function("ser::est_travel_time.json", |b| b.iter(|| serde_json::to_string(black_box(&est_travel_time))));
// c.bench_function("ser::faulty_traffic_lights.json", |b| b.iter(|| serde_json::to_string(black_box(&faulty_traffic_lights))));
// c.bench_function("ser::passenger_vol_bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_bus_stops))));
// c.bench_function("ser::passenger_od_bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_od_bus_stops))));
// c.bench_function("ser::passenger_vol_od_train.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_od_train))));
// c.bench_function("ser::passenger_vol_train.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_train))));
// c.bench_function("ser::taxi_avail.json", |b| b.iter(|| serde_json::to_string(black_box(&taxi_avail))));
// c.bench_function("ser::taxi_stands.json", |b| b.iter(|| serde_json::to_string(black_box(&taxi_stands))));
// c.bench_function("ser::train_service_alert.json", |b| b.iter(|| serde_json::to_string(black_box(&train_service_alert))));
// c.bench_function("ser::crowd_density_rt.json", |b| b.iter(|| serde_json::to_string(black_box(&crowd_density_rt))));
// c.bench_function("ser::crowd_density_forecast.json", |b| b.iter(|| serde_json::to_string(black_box(&crowd_density_forecast))));

c.bench_function("transmute_nextbus", |b| b.iter(|| de::transmute_nextbus(black_box(sample_data.clone()))));
c.bench_function("into_nextbus", |b| b.iter(|| NextBus::from(black_box(sample_data.clone()))));
c.bench_function("de::bike_parking.json", |b| b.iter(|| de::bike_parking()));
c.bench_function("de::bus_arrival.json", |b| b.iter(|| de::bus_arrival()));
c.bench_function("de::bus_route.json", |b| b.iter(|| de::bus_routes()));
c.bench_function("de::bus_services.json", |b| b.iter(|| de::bus_service()));
c.bench_function("de::bus_stops.json", |b| b.iter(|| de::bus_stops()));
c.bench_function("de::carpark_avail.json", |b| b.iter(|| de::carpark_avail()));
c.bench_function("de::erp_rates.json", |b| b.iter(|| de::erp_rates()));
c.bench_function("de::est_travel_time.json", |b| b.iter(|| de::est_travel_time()));
c.bench_function("de::faulty_traffic_lights.json", |b| b.iter(|| de::faulty_traffic_lights()));
c.bench_function("de::passenger_vol_bus_stops.json", |b| b.iter(|| de::passenger_vol_bus_stops()));
c.bench_function("de::passenger_od_bus_stops.json", |b| b.iter(|| de::passenger_vol_od_bus_stops()));
c.bench_function("de::passenger_vol_od_train.json", |b| b.iter(|| de::passenger_vol_od_train()));
c.bench_function("de::passenger_vol_train.json", |b| b.iter(|| de::passenger_vol_train()));
c.bench_function("de::taxi_avail.json", |b| b.iter(|| de::taxi_avail()));
c.bench_function("de::taxi_stands.json", |b| b.iter(|| de::taxi_stands()));
c.bench_function("de::train_service_alert.json", |b| b.iter(|| de::train_service_alert()));
c.bench_function("de::crowd_density_rt.json", |b| b.iter(|| de::crowd_density_rt()));
c.bench_function("de::crowd_density_forecast.json", |b| b.iter(|| de::crowd_density_forecast()));

let bike_parking = de::bike_parking();
let bus_arrival = de::bus_arrival();
let bus_routes = de::bus_routes();
let bus_service = de::bus_service();
let bus_stops = de::bus_stops();
let carpark_avail = de::carpark_avail();
let erp_rates = de::erp_rates();
let est_travel_time = de::est_travel_time();
let faulty_traffic_lights = de::faulty_traffic_lights();
let passenger_vol_bus_stops = de::passenger_vol_bus_stops();
let passenger_vol_od_bus_stops = de::passenger_vol_od_bus_stops();
let passenger_vol_train = de::passenger_vol_train();
let passenger_vol_od_train = de::passenger_vol_od_train();
let taxi_avail = de::taxi_avail();
let taxi_stands = de::taxi_stands();
let train_service_alert = de::train_service_alert();
let crowd_density_rt = de::crowd_density_rt();
let crowd_density_forecast = de::crowd_density_forecast();

c.bench_function("ser::bike_parking.json", |b| b.iter(|| serde_json::to_string(black_box(&bike_parking))));
c.bench_function("ser::bus_arrival.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_arrival))));
c.bench_function("ser::bus_route.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_routes))));
c.bench_function("ser::bus_services.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_service))));
c.bench_function("ser::bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&bus_stops))));
c.bench_function("ser::carpark_avail.json", |b| b.iter(|| serde_json::to_string(black_box(&carpark_avail))));
c.bench_function("ser::erp_rates.json", |b| b.iter(|| serde_json::to_string(black_box(&erp_rates))));
c.bench_function("ser::est_travel_time.json", |b| b.iter(|| serde_json::to_string(black_box(&est_travel_time))));
c.bench_function("ser::faulty_traffic_lights.json", |b| b.iter(|| serde_json::to_string(black_box(&faulty_traffic_lights))));
c.bench_function("ser::passenger_vol_bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_bus_stops))));
c.bench_function("ser::passenger_od_bus_stops.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_od_bus_stops))));
c.bench_function("ser::passenger_vol_od_train.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_od_train))));
c.bench_function("ser::passenger_vol_train.json", |b| b.iter(|| serde_json::to_string(black_box(&passenger_vol_train))));
c.bench_function("ser::taxi_avail.json", |b| b.iter(|| serde_json::to_string(black_box(&taxi_avail))));
c.bench_function("ser::taxi_stands.json", |b| b.iter(|| serde_json::to_string(black_box(&taxi_stands))));
c.bench_function("ser::train_service_alert.json", |b| b.iter(|| serde_json::to_string(black_box(&train_service_alert))));
c.bench_function("ser::crowd_density_rt.json", |b| b.iter(|| serde_json::to_string(black_box(&crowd_density_rt))));
c.bench_function("ser::crowd_density_forecast.json", |b| b.iter(|| serde_json::to_string(black_box(&crowd_density_forecast))));
}

criterion_group!(benches, criterion_benchmark);
Expand Down
8 changes: 4 additions & 4 deletions src/bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub mod prelude {
pub use {
crate::bus::bus_arrival::{BusArrivalResp, NextBus, RawBusArrivalResp},
crate::bus::bus_arrival::{BusArrivalResp, NextBus, BusArrivalRespRaw},
crate::bus::bus_routes::{BusRoute, BusRouteResp},
crate::bus::bus_services::{BusFreq, BusService, BusServiceResp},
crate::bus::bus_stops::{BusStop, BusStopsResp},
Expand Down Expand Up @@ -132,7 +132,7 @@ pub mod bus_arrival {

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct RawBusArrivalResp {
pub struct BusArrivalRespRaw {
#[serde(deserialize_with = "from_str")]
pub bus_stop_code: u32,
pub services: Vec<RawArrivalBusService>,
Expand All @@ -144,8 +144,8 @@ pub mod bus_arrival {
pub services: Vec<ArrivalBusService>,
}

impl From<RawBusArrivalResp> for BusArrivalResp {
fn from(data: RawBusArrivalResp) -> Self {
impl From<BusArrivalRespRaw> for BusArrivalResp {
fn from(data: BusArrivalRespRaw) -> Self {
Self {
bus_stop_code: data.bus_stop_code,
services: data.services.into_iter().map(|v| v.into()).collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ mod tests {
serde_messagepack_struct,
serde_flexbuffer_struct,
} = gen_test!(
RawBusArrivalResp,
BusArrivalRespRaw,
BusArrivalResp,
"../dumped_data/bus_arrival.json"
);
Expand Down

0 comments on commit b7b4802

Please sign in to comment.