root/trunk/epic/test/test_timing.sql

Revision 35, 1.0 kB (checked in by fumanchu, 3 years ago)

Oops. Forgot a change.

Line 
1-- Tests for the timing functions which Epic provides.
2-- To run, execute epic.sql, then this script, then test.run_module('test_timing').
3
4CREATE OR REPLACE FUNCTION test._sleep(s double precision) RETURNS VOID AS $$
5-- sleep function for Postgres 8.1
6DECLARE
7  start    timestamp with time zone;
8BEGIN
9  start := timeofday()::timestamp;
10  LOOP
11    EXIT WHEN (timeofday()::timestamp - start) > (s || ' second')::interval;
12  END LOOP;
13END;
14$$ LANGUAGE plpgsql;
15
16CREATE OR REPLACE FUNCTION test.test_timing() RETURNS VOID AS $$
17-- module: test_timing
18DECLARE
19  t           interval;
20BEGIN
21  -- Shame we have to use 0.01 but some platforms don't have finer resolution.
22  t := test.timing('SELECT test._sleep(0.01)', 100);
23 
24  -- Let's assume that sleep(0.01) * 100 should never run *under* 1s
25  PERFORM test.assert_greater_than_or_equal(t, '1.0');
26  -- ...but it does have overhead, up to 56% in my simple tests.
27  -- We'll assert < 2x.
28  PERFORM test.assert_less_than(t, '2.0');
29 
30  PERFORM test.pass();
31END;
32$$ LANGUAGE plpgsql;
33
Note: See TracBrowser for help on using the browser.