Skip to content

Commit b9e235f

Browse files
test: changed database performance teststo reference functions
1 parent c61864b commit b9e235f

File tree

4 files changed

+36
-99
lines changed

4 files changed

+36
-99
lines changed

db/tests/performance/aggregated_ip.test.sql

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,38 @@ VALUES ('test-update', '00000000-0000-0000-0000-000000000000'::uuid, 20),
2626
('test-decrement', '00000000-0000-0000-0000-000000000000'::uuid, 30),
2727
('test-reset-key', '00000000-0000-0000-0000-000000000000'::uuid, 40);
2828

29-
PREPARE update_aggregation as
30-
INSERT INTO rate_limit.records_aggregated (key, session_id)
31-
VALUES ('test-update', '00000000-0000-0000-0000-000000000000')
32-
ON CONFLICT ON CONSTRAINT unique_session_key DO UPDATE
33-
SET count = records_aggregated.count + 1
34-
RETURNING count;
35-
3629
SELECT performs_ok(
37-
'update_aggregation',
30+
$bd$
31+
SELECT agg_increment as count FROM rate_limit.agg_increment('test-update', '00000000-0000-0000-0000-000000000000');
32+
$bd$,
3833
250,
3934
'inserting record should execute under 250ms'
4035
);
4136

42-
43-
PREPARE decrement_records as
44-
UPDATE rate_limit.records_aggregated
45-
SET count = greatest(0, count - 1)
46-
WHERE
47-
key = 'test-decrement'
48-
AND session_id = '00000000-0000-0000-0000-000000000000';
49-
5037
SELECT performs_ok(
51-
'decrement_records',
38+
$bd$
39+
SELECT * FROM rate_limit.agg_decrement('test-decrement', '00000000-0000-0000-0000-000000000000');
40+
$bd$,
5241
250,
5342
'decrementing query execute under 250ms'
5443
);
5544

56-
PREPARE reset_key as
57-
DELETE FROM rate_limit.records_aggregated
58-
WHERE
59-
key = 'test-reset-key'
60-
AND session_id = '00000000-0000-0000-0000-000000000000';
61-
62-
6345
SELECT performs_ok(
64-
'reset_key',
46+
$bd$
47+
SELECT * FROM rate_limit.agg_reset_key('test-reset-key', '00000000-0000-0000-0000-000000000000')
48+
$bd$,
6549
250,
6650
'resetting a key should execute under 250ms'
6751
);
6852

69-
PREPARE reset_all as
70-
DELETE FROM rate_limit.records_aggregated
71-
WHERE session_id = '00000000-0000-0000-0000-000000000000';
72-
73-
7453
SELECT performs_ok(
75-
'reset_all',
54+
$bd$
55+
SELECT * FROM rate_limit.agg_reset_session('00000000-0000-0000-0000-000000000000');
56+
$bd$,
7657
250,
7758
'resetting a session should execute under 250ms'
7859
);
7960

80-
DEALLOCATE update_aggregation;
81-
DEALLOCATE decrement_records;
82-
DEALLOCATE reset_key;
83-
DEALLOCATE reset_all;
84-
8561
-- Finish the tests and clean up.
8662
SELECT finish FROM finish();
8763
ROLLBACK;
Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BEGIN;
22
-- Plan the tests.
3-
SELECT plan(5);
3+
SELECT plan(4);
44

55
INSERT INTO rate_limit.sessions (name_, type_)
66
SELECT
@@ -26,79 +26,38 @@ VALUES ('test-count', '00000000-0000-0000-0000-000000000000'::uuid),
2626
('test-decrement', '00000000-0000-0000-0000-000000000000'::uuid),
2727
('test-reset-key', '00000000-0000-0000-0000-000000000000'::uuid);
2828

29-
PREPARE insert_individual_record as
30-
INSERT INTO rate_limit.individual_records (key, session_id)
31-
VALUES ('test', '00000000-0000-0000-0000-000000000000'::uuid);
32-
3329
SELECT performs_ok(
34-
'insert_individual_record',
30+
$bd$
31+
SELECT ind_increment as count FROM rate_limit.ind_increment('test-count', '00000000-0000-0000-0000-000000000000')
32+
$bd$,
3533
250,
3634
'inserting record should execute under 250ms'
3735
);
3836

39-
PREPARE retrieve_count AS
40-
SELECT count(id) AS count
41-
FROM rate_limit.individual_records
42-
WHERE
43-
key = 'test-count'
44-
AND session_id = '00000000-0000-0000-0000-000000000000'::uuid;
45-
46-
SELECT performs_ok(
47-
'retrieve_count',
48-
250,
49-
'retrieving count should execute under 250ms'
50-
);
51-
52-
53-
PREPARE decrement_records as
54-
WITH
55-
rows_to_delete AS (
56-
SELECT id FROM rate_limit.individual_records
57-
WHERE
58-
key = 'test-decrement'
59-
AND session_id = '00000000-0000-0000-0000-000000000000'
60-
ORDER BY event_time
61-
LIMIT 1
62-
)
63-
DELETE FROM rate_limit.individual_records
64-
USING rows_to_delete WHERE individual_records.id = rows_to_delete.id;
65-
6637
SELECT performs_ok(
67-
'decrement_records',
38+
$bd$
39+
SELECT * FROM rate_limit.ind_decrement('test-decrement', '00000000-0000-0000-0000-000000000000');
40+
$bd$,
6841
250,
6942
'decrementing query execute under 250ms'
7043
);
7144

72-
PREPARE reset_key as
73-
DELETE FROM rate_limit.individual_records
74-
WHERE
75-
key = 'test-reset-key'
76-
AND session_id = '00000000-0000-0000-0000-000000000000';
77-
78-
7945
SELECT performs_ok(
80-
'reset_key',
46+
$bd$
47+
SELECT * FROM rate_limit.ind_reset_key('test-reset-key', '00000000-0000-0000-0000-000000000000');
48+
$bd$,
8149
250,
8250
'resetting a key should execute under 250ms'
8351
);
8452

85-
PREPARE reset_all as
86-
DELETE FROM rate_limit.individual_records
87-
WHERE session_id = '00000000-0000-0000-0000-000000000000';
88-
89-
9053
SELECT performs_ok(
91-
'reset_all',
54+
$bd$
55+
SELECT * FROM rate_limit.ind_reset_session('00000000-0000-0000-0000-000000000000');
56+
$bd$,
9257
250,
9358
'resetting a session should execute under 250ms'
9459
);
9560

96-
DEALLOCATE insert_individual_record;
97-
DEALLOCATE retrieve_count;
98-
DEALLOCATE decrement_records;
99-
DEALLOCATE reset_key;
100-
DEALLOCATE reset_all;
101-
10261
-- Finish the tests and clean up.
10362
SELECT finish FROM finish();
10463
ROLLBACK;

source/migrations/3-add-db-functions-ind.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ RETURNS int AS
33
$bd$
44
INSERT INTO rate_limit.individual_records(key, session_id) VALUES ($1, $2);
55

6-
SELECT count(id) AS count FROM rate_limit.individual_records WHERE key = $1 AND session_id = $2;
6+
SELECT count(id)::int AS count FROM rate_limit.individual_records WHERE key = $1 AND session_id = $2;
77
$bd$
88
LANGUAGE SQL;
99

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
CREATE OR REPLACE FUNCTION rate_limit.session_select(name_ text, type_ text)
2-
RETURNS TABLE(id uuid, name_ text, type_ text, expires_at timestamptz) AS
1+
CREATE OR REPLACE FUNCTION rate_limit.session_select(name_ TEXT, type_ TEXT)
2+
RETURNS TABLE (id UUID, name_ TEXT, type_ TEXT, expires_at TIMESTAMPTZ) AS
33
$bd$
44
SELECT id, name_, type_, expires_at
55
FROM rate_limit.sessions
6-
WHERE name_ = $1 and type_ = $2
6+
WHERE name_ = $1 AND type_ = $2
77
LIMIT 1;
88
$bd$
9-
LANGUAGE SQL;
9+
LANGUAGE sql;
1010

11-
CREATE OR REPLACE FUNCTION rate_limit.session_reset(name_ text, type_ text, expires_at_ timestamptz)
12-
RETURNS TABLE(id uuid, name_ text, type_ text) AS
11+
CREATE OR REPLACE FUNCTION rate_limit.session_reset(
12+
name_ TEXT, type_ TEXT, expires_at_ TIMESTAMPTZ
13+
)
14+
RETURNS TABLE (id UUID, name_ TEXT, type_ TEXT) AS
1315
$bd$
1416
DELETE FROM rate_limit.sessions
15-
WHERE name_ = $1 and type_ = $2;
17+
WHERE name_ = $1 AND type_ = $2;
1618

1719
INSERT INTO rate_limit.sessions(name_, type_, expires_at)
1820
SELECT $1, $2, $3
1921
RETURNING id, name_, type_;
2022
$bd$
21-
LANGUAGE SQL;
23+
LANGUAGE sql;

0 commit comments

Comments
 (0)