| | 939 | |
| | 940 | CREATE OR REPLACE FUNCTION test.timing(call text, number int) RETURNS interval AS $$ |
| | 941 | -- Return an interval encompassing 'number' runs of the given call. |
| | 942 | -- If 'number' is NULL or omitted, it defaults to 1000000. |
| | 943 | DECLARE |
| | 944 | v_call text; |
| | 945 | v_number int; |
| | 946 | start timestamp with time zone; |
| | 947 | BEGIN |
| | 948 | v_call := test.statement(call); |
| | 949 | v_number := number; |
| | 950 | IF number IS NULL THEN v_number := 1000000; END IF; |
| | 951 | -- Mustn't use now() here since that value is fixed for the entire transaction. |
| | 952 | start := clock_timestamp(); |
| | 953 | FOR i IN 1..v_number |
| | 954 | LOOP |
| | 955 | EXECUTE v_call; |
| | 956 | END LOOP; |
| | 957 | -- We grab the total clock time outside the loop. It's a toss-up whether the loop |
| | 958 | -- overhead outweighs assignment overhead if we accumulated the time inside the loop; |
| | 959 | -- therefore I chose "outside" since it makes the whole run faster. ;) |
| | 960 | RETURN (clock_timestamp() - start); |
| | 961 | END; |
| | 962 | $$ LANGUAGE plpgsql; |
| | 963 | |
| | 964 | |
| | 965 | CREATE OR REPLACE FUNCTION test.timing(call text) RETURNS interval AS $$ |
| | 966 | -- Return an interval encompassing 1,000 runs of the given call. |
| | 967 | BEGIN |
| | 968 | RETURN test.timing(call, NULL); |
| | 969 | END; |
| | 970 | $$ LANGUAGE plpgsql; |