Skip to content

Commit

Permalink
tweak benchmark setup ⚡️
Browse files Browse the repository at this point in the history
- Improve the UX by un-quoting the individual *.sql files and moving the `bench()` call to index.sql.
- Include the *.sql filename in the output CSV.
- Make psql stop on the first error.
  • Loading branch information
derhuerst committed Jul 8, 2024
1 parent 3ed31d0 commit df5674c
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 152 deletions.
6 changes: 2 additions & 4 deletions benchmark/arrs_deps_by_non_existent_stop.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SELECT * from bench(
'SELECT count(*)
SELECT count(*)
FROM arrivals_departures
WHERE stop_id = ''definitely-non-existent'''
);
WHERE stop_id = 'definitely-non-existent'
12 changes: 5 additions & 7 deletions benchmark/arrs_deps_by_route_name_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE route_short_name = ''S1''
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')'
);
WHERE route_short_name = 'S1'
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
13 changes: 5 additions & 8 deletions benchmark/arrs_deps_by_station_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')',
40
);
WHERE station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
15 changes: 6 additions & 9 deletions benchmark/arrs_deps_by_station_and_time_seq_0.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')
AND stop_sequence = 0',
50
);
WHERE station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
AND stop_sequence = 0
6 changes: 2 additions & 4 deletions benchmark/arrs_deps_by_stop.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SELECT * from bench(
'SELECT count(*)
SELECT count(*)
FROM arrivals_departures
WHERE stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)'
);
WHERE stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin)
12 changes: 5 additions & 7 deletions benchmark/arrs_deps_by_stop_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')'
);
WHERE stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
11 changes: 4 additions & 7 deletions benchmark/arrs_deps_by_time.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02''::timestamp with time zone)
AND date <= dates_filter_max(''2022-08-09T07:30+02''::timestamp with time zone)',
10
);
WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02'::timestamp with time zone)
AND date <= dates_filter_max('2022-08-09T07:30+02'::timestamp with time zone)
11 changes: 4 additions & 7 deletions benchmark/arrs_deps_by_time_manual.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= ''2022-08-08''
AND date <= ''2022-08-09''',
10
);
WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= '2022-08-08'
AND date <= '2022-08-09'
8 changes: 3 additions & 5 deletions benchmark/arrs_deps_by_trip_and_date.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM arrivals_departures
WHERE trip_id = ''168977951''
AND date > ''2022-08-08'' AND date <= ''2022-08-09'''
);
WHERE trip_id = '168977951'
AND date > '2022-08-08' AND date <= '2022-08-09'
2 changes: 1 addition & 1 deletion benchmark/as-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pipeline(
if (firstRow) {
firstRow = false

const keys = Object.keys(row)
const keys = Object.keys(row).filter(key => key !== 'filename')
process.stdout.write(`| ${keys.join(' | ')} |\n`)
process.stdout.write(`| ${keys.map(_ => '-').join(' | ')} |\n`)
}
Expand Down
6 changes: 2 additions & 4 deletions benchmark/connections_by_non_existent_stop.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SELECT * from bench(
'SELECT count(*)
SELECT count(*)
FROM connections
WHERE from_stop_id = ''definitely-non-existent'''
);
WHERE from_stop_id = 'definitely-non-existent'
12 changes: 5 additions & 7 deletions benchmark/connections_by_route_name_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE route_short_name = ''S1''
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')'
);
WHERE route_short_name = 'S1'
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
13 changes: 5 additions & 8 deletions benchmark/connections_by_station_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE from_station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')',
40
);
WHERE from_station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
15 changes: 6 additions & 9 deletions benchmark/connections_by_station_and_time_seq_0.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE from_station_id = ''de:11000:900100001'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')
AND from_stop_sequence = 0',
50
);
WHERE from_station_id = 'de:11000:900100001' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
AND from_stop_sequence = 0
6 changes: 2 additions & 4 deletions benchmark/connections_by_stop.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SELECT * from bench(
'SELECT count(*)
SELECT count(*)
FROM connections
WHERE from_stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)'
);
WHERE from_stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin)
12 changes: 5 additions & 7 deletions benchmark/connections_by_stop_and_time.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE from_stop_id = ''de:11000:900100001::4'' -- S+U Friedrichstr. (Berlin)
AND t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02'')
AND date <= dates_filter_max(''2022-08-09T07:30+02'')'
);
WHERE from_stop_id = 'de:11000:900100001::4' -- S+U Friedrichstr. (Berlin)
AND t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02')
AND date <= dates_filter_max('2022-08-09T07:30+02')
13 changes: 5 additions & 8 deletions benchmark/connections_by_time.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= dates_filter_min(''2022-08-09T07:10+02''::timestamp with time zone)
AND date <= dates_filter_max(''2022-08-09T07:30+02''::timestamp with time zone)
WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= dates_filter_min('2022-08-09T07:10+02'::timestamp with time zone)
AND date <= dates_filter_max('2022-08-09T07:30+02'::timestamp with time zone)
ORDER BY t_departure
LIMIT 100',
7
);
LIMIT 100
13 changes: 5 additions & 8 deletions benchmark/connections_by_time_manual.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE t_departure >= ''2022-08-09T07:10+02'' AND t_departure <= ''2022-08-09T07:30+02''
AND date >= ''2022-08-08''
AND date <= ''2022-08-09''
WHERE t_departure >= '2022-08-09T07:10+02' AND t_departure <= '2022-08-09T07:30+02'
AND date >= '2022-08-08'
AND date <= '2022-08-09'
ORDER BY t_departure
LIMIT 100',
7
);
LIMIT 100
8 changes: 3 additions & 5 deletions benchmark/connections_by_trip_and_date.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM connections
WHERE trip_id = ''168977951''
AND date > ''2022-08-08'' AND date <= ''2022-08-09'''
);
WHERE trip_id = '168977951'
AND date > '2022-08-08' AND date <= '2022-08-09'
64 changes: 43 additions & 21 deletions benchmark/index.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BEGIN;
CREATE TEMP TABLE _benchmark (
filename TEXT,
query TEXT,
avg FLOAT,
min FLOAT,
Expand All @@ -14,7 +15,7 @@ CREATE TEMP TABLE _benchmark (

-- slightly modified from "How to benchmark PostgreSQL queries well"
-- https://www.tangramvision.com/blog/how-to-benchmark-postgresql-queries-well#sql-function-with-clock_timestamp
CREATE OR REPLACE FUNCTION bench(_query TEXT, _iterations INTEGER = 100)
CREATE OR REPLACE FUNCTION bench(_filename TEXT, _query TEXT, _iterations INTEGER = 100)
RETURNS void
AS $$
DECLARE
Expand All @@ -41,6 +42,7 @@ BEGIN

INSERT INTO _benchmark
SELECT
_filename,
_query,
round(avg(elapsed)::numeric, 0),
min(elapsed),
Expand All @@ -58,26 +60,46 @@ END
$$
LANGUAGE plpgsql;

\i stops_by_distance.sql
\i arrs_deps_by_route_name_and_time.sql
\i arrs_deps_by_station_and_time.sql
\i arrs_deps_by_station_and_time_seq_0.sql
\i arrs_deps_by_stop_and_time.sql
\i arrs_deps_by_trip_and_date.sql
\i arrs_deps_by_stop.sql
\i arrs_deps_by_non_existent_stop.sql
\i arrs_deps_by_time.sql
\i arrs_deps_by_time_manual.sql
\i connections_by_route_name_and_time.sql
\i connections_by_station_and_time.sql
\i connections_by_station_and_time_seq_0.sql
\i connections_by_stop_and_time.sql
\i connections_by_trip_and_date.sql
\i connections_by_stop.sql
\i connections_by_non_existent_stop.sql
\i connections_by_time.sql
\i connections_by_time_manual.sql
\i stats_by_route_date.sql
\set query `cat arrs_deps_by_non_existent_stop.sql`
SELECT bench('arrs_deps_by_non_existent_stop.sql', :'query');
\set query `cat arrs_deps_by_route_name_and_time.sql`
SELECT bench('arrs_deps_by_route_name_and_time.sql', :'query');
\set query `cat arrs_deps_by_station_and_time.sql`
SELECT bench('arrs_deps_by_station_and_time.sql', :'query', 40);
\set query `cat arrs_deps_by_station_and_time_seq_0.sql`
SELECT bench('arrs_deps_by_station_and_time_seq_0.sql', :'query', 50);
\set query `cat arrs_deps_by_stop.sql`
SELECT bench('arrs_deps_by_stop.sql', :'query');
\set query `cat arrs_deps_by_stop_and_time.sql`
SELECT bench('arrs_deps_by_stop_and_time.sql', :'query');
\set query `cat arrs_deps_by_time.sql`
SELECT bench('arrs_deps_by_time.sql', :'query', 10);
\set query `cat arrs_deps_by_time_manual.sql`
SELECT bench('arrs_deps_by_time_manual.sql', :'query', 10);
\set query `cat arrs_deps_by_trip_and_date.sql`
SELECT bench('arrs_deps_by_trip_and_date.sql', :'query');
\set query `cat connections_by_non_existent_stop.sql`
SELECT bench('connections_by_non_existent_stop.sql', :'query');
\set query `cat connections_by_route_name_and_time.sql`
SELECT bench('connections_by_route_name_and_time.sql', :'query');
\set query `cat connections_by_station_and_time.sql`
SELECT bench('connections_by_station_and_time.sql', :'query', 40);
\set query `cat connections_by_station_and_time_seq_0.sql`
SELECT bench('connections_by_station_and_time_seq_0.sql', :'query', 50);
\set query `cat connections_by_stop.sql`
SELECT bench('connections_by_stop.sql', :'query');
\set query `cat connections_by_stop_and_time.sql`
SELECT bench('connections_by_stop_and_time.sql', :'query');
\set query `cat connections_by_time.sql`
SELECT bench('connections_by_time.sql', :'query', 7);
\set query `cat connections_by_time_manual.sql`
SELECT bench('connections_by_time_manual.sql', :'query', 7);
\set query `cat connections_by_trip_and_date.sql`
SELECT bench('connections_by_trip_and_date.sql', :'query');
\set query `cat stats_by_route_date.sql`
SELECT bench('stats_by_route_date.sql', :'query', 10);
\set query `cat stops_by_distance.sql`
SELECT bench('stops_by_distance.sql', :'query');

SELECT * FROM _benchmark;

Expand Down
2 changes: 1 addition & 1 deletion benchmark/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ set -x

psql -c 'VACUUM ANALYZE'

psql -q -b --csv -f index.sql
psql -q -b -v 'ON_ERROR_STOP=1' --csv -f index.sql
11 changes: 4 additions & 7 deletions benchmark/stats_by_route_date.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM stats_by_route_date
WHERE route_id = ''17452_900'' -- M4
AND date >= ''2022-08-08'' AND date <= ''2022-08-14''
AND is_effective = true',
10
);
WHERE route_id = '17452_900' -- M4
AND date >= '2022-08-08' AND date <= '2022-08-14'
AND is_effective = true
6 changes: 2 additions & 4 deletions benchmark/stops_by_distance.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
SELECT * from bench(
'SELECT *
SELECT *
FROM stops
ORDER BY ST_Distance(stop_loc::geometry, ST_SetSRID(ST_MakePoint(9.7, 50.547), 4326)) ASC
LIMIT 100'
);
LIMIT 100

0 comments on commit df5674c

Please sign in to comment.