|
| 1 | +/* |
| 2 | + * Function: format_time() |
| 3 | + * |
| 4 | + * Takes a raw picoseconds value, and converts it to a human readable form. |
| 5 | + * Picoseconds are the precision that all latency values are printed in |
| 6 | + * within MySQL's Performance Schema. |
| 7 | + * |
| 8 | + * Parameters |
| 9 | + * picoseconds: The raw picoseconds value to convert |
| 10 | + * |
| 11 | + * mysql> select format_time(342342342342345); |
| 12 | + * +------------------------------+ |
| 13 | + * | format_time(342342342342345) | |
| 14 | + * +------------------------------+ |
| 15 | + * | 00:05:42 | |
| 16 | + * +------------------------------+ |
| 17 | + * 1 row in set (0.00 sec) |
| 18 | + * |
| 19 | + * mysql> select format_time(342342342); |
| 20 | + * +------------------------+ |
| 21 | + * | format_time(342342342) | |
| 22 | + * +------------------------+ |
| 23 | + * | 342.34 µs | |
| 24 | + * +------------------------+ |
| 25 | + * 1 row in set (0.00 sec) |
| 26 | + * |
| 27 | + * mysql> select format_time(34234); |
| 28 | + * +--------------------+ |
| 29 | + * | format_time(34234) | |
| 30 | + * +--------------------+ |
| 31 | + * | 34.23 ns | |
| 32 | + * +--------------------+ |
| 33 | + * 1 row in set (0.00 sec) |
| 34 | + * |
| 35 | + * mysql> select format_time(342); |
| 36 | + * +------------------+ |
| 37 | + * | format_time(342) | |
| 38 | + * +------------------+ |
| 39 | + * | 342 ps | |
| 40 | + * +------------------+ |
| 41 | + * 1 row in set (0.00 sec) |
| 42 | + */ |
| 43 | + |
| 44 | +DROP FUNCTION IF EXISTS format_time; |
| 45 | + |
| 46 | +DELIMITER $$ |
| 47 | + |
| 48 | +CREATE FUNCTION format_time(picoseconds BIGINT UNSIGNED) |
| 49 | + RETURNS VARCHAR(16) CHARSET UTF8 DETERMINISTIC |
| 50 | +BEGIN |
| 51 | + IF picoseconds IS NULL THEN RETURN NULL; |
| 52 | + ELSEIF picoseconds >= 3600000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 3600000000000000, 2), 'h'); |
| 53 | + ELSEIF picoseconds >= 60000000000000 THEN RETURN SEC_TO_TIME(ROUND(picoseconds / 1000000000000, 2)); |
| 54 | + ELSEIF picoseconds >= 1000000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000000, 2), ' s'); |
| 55 | + ELSEIF picoseconds >= 1000000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000000, 2), ' ms'); |
| 56 | + ELSEIF picoseconds >= 1000000 THEN RETURN CONCAT(ROUND(picoseconds / 1000000, 2), ' us'); |
| 57 | + ELSEIF picoseconds >= 1000 THEN RETURN CONCAT(ROUND(picoseconds / 1000, 2), ' ns'); |
| 58 | + ELSE RETURN CONCAT(picoseconds, ' ps'); |
| 59 | + END IF; |
| 60 | +END $$ |
| 61 | + |
| 62 | +DELIMITER ; |
0 commit comments