Skip to content

Commit

Permalink
Improve debug reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Nov 28, 2021
1 parent 60a0b9b commit f113ffd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 33 deletions.
42 changes: 28 additions & 14 deletions src/report/reporters/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { STAT_TITLES } from '../utils/stat_titles.js'
import { getTables } from '../utils/table.js'

// Debugging reporter only meant for development purpose
const reportTerminal = function ({ combinations, screenWidth }) {
const statNames = getStatNames(combinations)
const headerRows = getHeaderRow(statNames)
const bodyRows = getBodyRows(combinations, statNames)
return getTables([...headerRows, ...bodyRows], screenWidth)
}

// Retrieve the list of columns, each corresponding to a stat.
// Empty columns are not displayed.
const getStatNames = function (combinations) {
return STAT_NAMES.filter((statName) => hasStat(combinations, statName))
const reportTerminal = function (
{ combinations, screenWidth },
{
mini = false,
sparse = false,
header = true,
stats: statNames = DEFAULT_STAT_NAMES,
},
) {
const statNamesA = getStatNames(combinations, statNames, sparse)
const headerRows = getHeaderRow(statNamesA, mini, header)
const bodyRows = getBodyRows(combinations, statNamesA)
return getTables([...headerRows, ...bodyRows], screenWidth, mini)
}

// Order is significant as it is displayed in that order.
const STAT_NAMES = [
const DEFAULT_STAT_NAMES = [
'mean',
'meanMin',
'meanMax',
Expand All @@ -40,16 +42,28 @@ const STAT_NAMES = [
'minLoopDuration',
]

// Retrieve the list of columns, each corresponding to a stat.
// Empty columns are not displayed.
const getStatNames = function (combinations, statNames, sparse) {
return sparse
? statNames
: statNames.filter((statName) => hasStat(combinations, statName))
}

const hasStat = function (combinations, statName) {
return combinations.some(
(combination) => getCell(statName, combination) !== '',
)
}

const getHeaderRow = function (statNames) {
const getHeaderRow = function (statNames, mini, header) {
if (!header) {
return []
}

const firstRow =
statNames.length === 0 ? [] : ['', ...statNames.map(getHeaderName)]
return [firstRow, []]
return mini ? [firstRow] : [firstRow, []]
}

const getHeaderName = function (statName) {
Expand Down
26 changes: 18 additions & 8 deletions src/report/utils/separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ const TITLE_PADDING = ' '.repeat(TITLE_PADDING_WIDTH)
export const TITLE_SEPARATOR = `${TITLE_SEPARATOR_SIGN}${TITLE_PADDING}`
export const TITLE_SEPARATOR_COLORED = TITLE_SEPARATOR

// Separator between columns
// Works on all terminals
const COLUMN_SEPARATOR_SIGN = '\u2502'
const COLUMN_PADDING_WIDTH = 1
const COLUMN_PADDING = ' '.repeat(COLUMN_PADDING_WIDTH)
export const COLUMN_SEPARATOR = `${COLUMN_PADDING}${COLUMN_SEPARATOR_SIGN}${COLUMN_PADDING}`
export const COLUMN_SEPARATOR_COLORED = separatorColor(COLUMN_SEPARATOR)

// Separator between stats
// Works on most terminals
const STATS_SEPARATOR_SIGN = '\u2012'
const STATS_PADDING_WIDTH = 1
const STATS_PADDING = ' '.repeat(STATS_PADDING_WIDTH)
const STATS_SEPARATOR = `${STATS_PADDING}${STATS_SEPARATOR_SIGN}${STATS_PADDING}`
export const STATS_SEPARATOR_COLORED = separatorColor(STATS_SEPARATOR)

// Separator between columns
// Works on all terminals
export const getColSeparator = function (mini) {
return mini ? MCOL_SEPARATOR : COL_SEPARATOR
}

export const getColSeparatorColored = function (mini) {
return mini ? MCOL_SEPARATOR_COLORED : COL_SEPARATOR_COLORED
}

const COL_SEPARATOR_SIGN = '\u2502'
const COL_PADDING_WIDTH = 1
const COL_PADDING = ' '.repeat(COL_PADDING_WIDTH)
const COL_SEPARATOR = `${COL_PADDING}${COL_SEPARATOR_SIGN}${COL_PADDING}`
const COL_SEPARATOR_COLORED = separatorColor(COL_SEPARATOR)
const MCOL_SEPARATOR = COL_PADDING
const MCOL_SEPARATOR_COLORED = separatorColor(MCOL_SEPARATOR)
58 changes: 47 additions & 11 deletions src/report/utils/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { padString } from './pad.js'
import {
TITLE_SEPARATOR,
TITLE_SEPARATOR_COLORED,
COLUMN_SEPARATOR,
COLUMN_SEPARATOR_COLORED,
getColSeparator,
getColSeparatorColored,
} from './separator.js'

// Serialize a matrix of strings to a table.
Expand All @@ -18,16 +18,29 @@ import {
// - It does not use a column separator
// - It has its own width
// - It is omitted if only filled with empty strings
export const getTables = function (rows, screenWidth) {
export const getTables = function (rows, screenWidth, mini = false) {
const rowsLeft = rows.map(getRowLeft)
const rowsRight = rows.map(getRowRight)
const leftWidth = Math.max(...rowsLeft.map(stringWidth))
const columnsWidth = Math.max(...rowsRight.flat().map(stringWidth))
const availableWidth = getAvailableWidth(screenWidth, leftWidth)
const tablesRows = getTablesRows(rowsRight, availableWidth, columnsWidth)
const columnSeparator = getColSeparator(mini)
const columnSeparatorColored = getColSeparatorColored(mini)
const tablesRows = getTablesRows({
rows: rowsRight,
availableWidth,
columnsWidth,
columnSeparator,
})
return tablesRows
.map((tableRows) =>
getTable({ tableRows, rowsLeft, leftWidth, columnsWidth }),
getTable({
tableRows,
rowsLeft,
leftWidth,
columnsWidth,
columnSeparatorColored,
}),
)
.join('\n')
}
Expand All @@ -46,8 +59,13 @@ const getAvailableWidth = function (screenWidth, leftWidth) {
: screenWidth - leftWidth - TITLE_SEPARATOR.length
}

const getTablesRows = function (rows, availableWidth, columnsWidth) {
const separatorWidth = COLUMN_SEPARATOR.length
const getTablesRows = function ({
rows,
availableWidth,
columnsWidth,
columnSeparator,
}) {
const separatorWidth = columnSeparator.length
const columnsCountFloat =
(availableWidth + separatorWidth) / (columnsWidth + separatorWidth)
const columnsCount = Math.max(Math.floor(columnsCountFloat), 1)
Expand Down Expand Up @@ -91,18 +109,36 @@ const getTableRow = function ({
return [...tableRow, ...emptyCells]
}

const getTable = function ({ tableRows, rowsLeft, leftWidth, columnsWidth }) {
const getTable = function ({
tableRows,
rowsLeft,
leftWidth,
columnsWidth,
columnSeparatorColored,
}) {
return tableRows
.map((row, rowIndex) =>
getRow({ row, leftCell: rowsLeft[rowIndex], leftWidth, columnsWidth }),
getRow({
row,
leftCell: rowsLeft[rowIndex],
leftWidth,
columnsWidth,
columnSeparatorColored,
}),
)
.join('')
}

const getRow = function ({ row, leftCell, leftWidth, columnsWidth }) {
const getRow = function ({
row,
leftCell,
leftWidth,
columnsWidth,
columnSeparatorColored,
}) {
const rowA = row
.map((cell) => padString(cell, columnsWidth))
.join(COLUMN_SEPARATOR_COLORED)
.join(columnSeparatorColored)
return leftWidth === 0
? `${rowA}\n`
: `${padString(leftCell, leftWidth)}${TITLE_SEPARATOR_COLORED}${rowA}\n`
Expand Down

0 comments on commit f113ffd

Please sign in to comment.