create AGGREGATE FUNCTION PERCENTILES(IN val float, IN flag_return boolean, INOUT buffer float ARRAY, INOUT counter INT)
RETURNS varchar2
CONTAINS SQL
BEGIN ATOMIC
declare i number default 1;
declare stats varchar2(2000) default '';
declare asum float default 0;
declare p0, p50, p90, p99, p100, pavg number default 0;
declare sorted_buffer float ARRAY;
IF flag_return THEN
--call println('PERCENTILES return');
set sorted_buffer = SORT_ARRAY(buffer);
call println('PERCENTILES sorted');
set p50 = sorted_buffer[1];
set p90 = sorted_buffer[1];
set p99 = sorted_buffer[1];
while i <= counter do
begin atomic
--call println('PERCENTILES selection '||i);
set asum = asum + sorted_buffer[i];
if abs(i - round(counter / 2, 0)) < 0.001 then
set p50 = round(sorted_buffer[i], 2);
end if;
if abs(i - round(counter * 9 / 10, 0)) < 0.001 then
set p90 = round(sorted_buffer[i], 2);
end if;
if abs(i - round(counter * 99 / 100, 0)) < 0.001 then
set p99 = round(sorted_buffer[i], 2);
end if;
set i = i+1;
end;
end while;
set p0 = round(sorted_buffer[1],2);
set p100 = round(sorted_buffer[counter],2);
set pavg = round(asum / counter, 2);
--call println('PERCENTILES preparing result');
set stats = 'min='|| p0 || '|p50='|| p50 || '|p90='||p90 || '|p99='||p99 || '|max='||p100 || '|avg='|| pavg;
RETURN stats;
ELSE
IF val IS NULL THEN RETURN NULL; END IF;
IF counter IS NULL THEN SET counter = 0; END IF;
SET counter = counter + 1;
IF counter = 1 THEN SET buffer = ARRAY[val];
ELSE SET buffer[counter] = val; END IF;
RETURN NULL;
END IF;
END
for (select appid, percentiles(mem_pct) as mem_stats, avg(mem_mb) as mem,
percentiles(cpu_pct) as cpu_stats, avg(cpu_cnt) as cpu,
percentiles(gc_millis) as gc_stats from T_VM_STAT group by appid) do
begin atomic
set r = REGEXP_SUBSTRING_ARRAY(mem_stats, '((?<==)[0-9]+[\.\,]*[0-9]*)');
set (p0,p50,p90,p99,p100,pavg)=(to_number(r[1]), to_number(r[2]), to_number(r[3]), to_number(r[4]), to_number(r[5]), to_number(r[6]));
czwartek, września 17, 2015
Subskrybuj:
Komentarze do posta (Atom)
0 komentarze:
Prześlij komentarz