Pages

Wednesday, January 30, 2019

plpgsql_check - new report for code coverage ratio calculation

Few months ago I integrated a profiler into plpgsql_check.

The result of prifiling is line oriented:

postgres=# select lineno, avg_time, source from plpgsql_profiler_function_tb('fx(int)');
┌────────┬──────────┬───────────────────────────────────────────────────────────────────┐
│ lineno │ avg_time │                              source                               │
╞════════╪══════════╪═══════════════════════════════════════════════════════════════════╡
│      1 │          │                                                                   │
│      2 │          │ declare result int = 0;                                           │
│      3 │    0.075 │ begin                                                             │
│      4 │    0.202 │   for i in 1..$1 loop                                             │
│      5 │    0.005 │     select result + i into result; select result + i into result; │
│      6 │          │   end loop;                                                       │
│      7 │        0 │   return result;                                                  │
│      8 │          │ end;                                                              │
└────────┴──────────┴───────────────────────────────────────────────────────────────────┘
(9 rows)

This format is well readable, but it is not practical for calculation of code coverage metrics. So this week I wrote new function, that produce
report based on commands:

        CREATE OR REPLACE FUNCTION public.fx1(a integer)
         RETURNS integer
         LANGUAGE plpgsql
1       AS $function$
2       begin
3         if a > 10 then
4           raise notice 'ahoj';
5           return -1;
6         else
7           raise notice 'nazdar';
8           return 1;
9         end if;
10      end;
11      $function$

postgres=# select stmtid, parent_stmtid, parent_note, lineno, exec_stmts, stmtname
             from plpgsql_profiler_function_statements_tb('fx1');
┌────────┬───────────────┬─────────────┬────────┬────────────┬─────────────────┐
│ stmtid │ parent_stmtid │ parent_note │ lineno │ exec_stmts │    stmtname     │
╞════════╪═══════════════╪═════════════╪════════╪════════════╪═════════════════╡
│      0 │             ∅ │ ∅           │      2 │          0 │ statement block │
│      1 │             0 │ body        │      3 │          0 │ IF              │
│      2 │             1 │ then body   │      4 │          0 │ RAISE           │
│      3 │             1 │ then body   │      5 │          0 │ RETURN          │
│      4 │             1 │ else body   │      7 │          0 │ RAISE           │
│      5 │             1 │ else body   │      8 │          0 │ RETURN          │
└────────┴───────────────┴─────────────┴────────┴────────────┴─────────────────┘
(6 rows)

Now, it should not be too difficult to calculate (by SQL) some coverage metrics.

4 comments:

  1. make install throws error:

    /bin/mkdir -p '/usr/lib/postgresql/9.5/lib'
    /bin/mkdir -p '/usr/share/postgresql/9.5/extension'
    /bin/mkdir -p '/usr/share/postgresql/9.5/extension'
    /usr/bin/install -c -m 755 plpgsql_check.so '/usr/lib/postgresql/9.5/lib/plpgsql_check.so'
    /usr/bin/install -c -m 644 .//plpgsql_check.control '/usr/share/postgresql/9.5/extension/'
    /usr/bin/install -c -m 644 .//plpgsql_check--1.6.sql '/usr/share/postgresql/9.5/extension/'
    /usr/bin/install: cannot stat './/plpgsql_check--1.6.sql': No such file or directory
    /usr/lib/postgresql/9.5/lib/pgxs/src/makefiles/pgxs.mk:116: recipe for target 'install' failed
    make: *** [install] Error 1

    ReplyDelete
  2. @Gábor Szabó, should be fixed now, please, recheck

    ReplyDelete