forked from rin-nas/postgresql-patterns-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.sql
More file actions
51 lines (40 loc) · 1.66 KB
/
benchmark.sql
File metadata and controls
51 lines (40 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
-- TODO Works incorrect! Do not use!
-- PostgreSQL equivalent of MySQL's BENCHMARK() function
CREATE OR REPLACE FUNCTION benchmark(loop_count int,
sql_expr text, -- SQL expression
is_cache_plan boolean default true) returns interval
immutable
strict
parallel safe -- Postgres 10 or later
language plpgsql
AS
$$
BEGIN
if is_cache_plan then
EXECUTE 'select ($1) from generate_series(1, $2)' using sql_expr, loop_count;
else
FOR i IN 1..loop_count LOOP
EXECUTE 'select ($1)' using sql_expr;
END LOOP;
end if;
RETURN clock_timestamp() - now();
END
$$;
-- TESTS (parse URL)
select benchmark(
500000,
$$substring(format('https://www.domain%s.com/?aaa=1111&b[2]=3#test', (random()*1000)::int::text) from '^[^:]+://([^/]+)')$$,
true);
-- 0 years 0 mons 0 days 0 hours 0 mins 0.300638 secs
select benchmark(
500000,
$$substring(format('https://www.domain%s.com/?aaa=1111&b[2]=3#test', (random()*1000)::int::text) from '^[^:]+://([^/]+)')$$,
false);
-- 0 years 0 mons 0 days 0 hours 0 mins 6.735336 secs
-- TESTS (generate UUID)
SELECT benchmark(1000000, $$uuid_in(overlay(overlay(md5(random()::text || ':' || clock_timestamp()::text) placing '4' from 13) placing to_hex(floor(random()*(11-8+1) + 8)::int)::text from 17)::cstring)$$);
-- 0 years 0 mons 0 days 0 hours 0 mins 0.644648 secs
SELECT benchmark(1000000, $$md5(random()::text || clock_timestamp()::text)::uuid$$);
-- 0 years 0 mons 0 days 0 hours 0 mins 0.449026 secs
SELECT benchmark(1000000, $$gen_random_uuid()$$);
-- 0 years 0 mons 0 days 0 hours 0 mins 0.438084 secs