From b7b48025b25e05a175310541e363e3ddca0c1091 Mon Sep 17 00:00:00 2001 From: zeon256 Date: Sat, 27 Jan 2024 02:05:24 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20benchmark=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++ benches/benchmark.rs | 141 ++++++++++++++++++++++--------------------- src/bus.rs | 8 +-- src/lib.rs | 2 +- 4 files changed, 87 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 96b78b8..4ba7d8d 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/benches/benchmark.rs b/benches/benchmark.rs index a7770fc..9d08309 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -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}; @@ -17,7 +19,7 @@ mod de { where F: FnOnce() -> &'de str, I: Deserialize<'de> + Into, - S: Serialize, + S: Serialize + Debug { let str_data = input_fn(); serde_json::from_str(str_data).map(|v: I| v.into()).unwrap() @@ -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 { + pub fn bus_routes() -> Vec { gen!(BusRouteResp, _, "../dumped_data/bus_route.json") } - pub fn bus_service() -> Vec { + pub fn bus_service() -> Vec { gen!(BusServiceResp, _, "../dumped_data/bus_services.json") } - pub fn bus_stops() -> Vec { + pub fn bus_stops() -> Vec { gen!(BusStopsResp, _, "../dumped_data/bus_stops.json") } - pub fn carpark_avail() -> Vec { + pub fn carpark_avail() -> Vec { gen!(CarparkAvailResp, _, "../dumped_data/carpark_avail.json") } - pub fn erp_rates() -> Vec { + pub fn erp_rates() -> Vec { gen!(ErpRatesResp, _, "../dumped_data/erp_rates.json") } @@ -81,7 +83,7 @@ mod de { gen!(TaxiAvailResp, _, "../dumped_data/taxi_avail.json") } - pub fn taxi_stands() -> Vec { + pub fn taxi_stands() -> Vec { gen!(TaxiStandsResp, _, "../dumped_data/taxi_stands.json") } @@ -89,15 +91,15 @@ mod de { gen!(TrainServiceAlertResp,_,"../dumped_data/train_service_alert.json") } - pub fn est_travel_time() -> Vec { + pub fn est_travel_time() -> Vec { gen!(EstTravelTimeResp, _, "../dumped_data/est_travel_time.json") } - pub fn crowd_density_rt() -> Vec { + pub fn crowd_density_rt() -> Vec { 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") } @@ -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); diff --git a/src/bus.rs b/src/bus.rs index 331d48e..6b5038b 100644 --- a/src/bus.rs +++ b/src/bus.rs @@ -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}, @@ -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, @@ -144,8 +144,8 @@ pub mod bus_arrival { pub services: Vec, } - impl From for BusArrivalResp { - fn from(data: RawBusArrivalResp) -> Self { + impl From 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(), diff --git a/src/lib.rs b/src/lib.rs index 6b7b35e..e392507 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -338,7 +338,7 @@ mod tests { serde_messagepack_struct, serde_flexbuffer_struct, } = gen_test!( - RawBusArrivalResp, + BusArrivalRespRaw, BusArrivalResp, "../dumped_data/bus_arrival.json" );