Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.0.2",
"@types/json2csv": "^5.0.3",
"@types/markdown-table": "^2.0.0",
"@types/node": "^16.10.2",
"@types/prettier": "^2.4.1",
"@typescript-eslint/eslint-plugin": "^4.32.0",
Expand All @@ -76,6 +77,7 @@
"fs-extra": "^10.0.0",
"json2csv": "^5.0.6",
"kleur": "^4.1.4",
"log-update": "^4.0.0"
"log-update": "^4.0.0",
"markdown-table": "^2.0.0"
}
}
2 changes: 1 addition & 1 deletion src/internal/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export type SaveOptions = {
*
* @default 'json'
*/
format?: 'json' | 'csv' | 'table.html' | 'chart.html'
format?: 'json' | 'csv' | 'table.html' | 'chart.html' | 'table.md'
}

export type CaseResult = {
Expand Down
26 changes: 25 additions & 1 deletion src/internal/prepareFileContent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { method, multi } from '@arrows/multimethod'
import { parse } from 'json2csv'
import { CaseResultWithDiff, SaveOptions, Summary } from './common-types'
import { stripIndent, html } from 'common-tags'
import { stripIndent, html, stripIndents } from 'common-tags'
import * as markdownTable from 'markdown-table'

const flattenResults = (results: CaseResultWithDiff[]) => {
return results.map((result) => ({
Expand Down Expand Up @@ -102,6 +103,28 @@ const prepareHTMLTable = (summary: Summary, options: SaveOptions) => {
return stripIndent(markup)
}

const prepareMarkdownTable = (summary: Summary, options: SaveOptions) => {
const results = options.details
? flattenResults(summary.results)
: summary.results.map(({ name, ops, margin, percentSlower }) => {
return { name, ops, margin, percentSlower }
})

const headers = Object.keys(results[0])
const tableBody: string[][] = results.map((result) => {
// @ts-ignore
return headers.map((key: string) => (String(result[key])))
})

return stripIndents`**${summary.name}:**

${markdownTable([
headers,
...tableBody
])}
`
}

const prepareColors = (percents: number[]) => {
return percents.map((percent) => {
const hue = 120 * ((100 - percent) / 100)
Expand Down Expand Up @@ -242,6 +265,7 @@ const prepareFileContent = multi(
method('csv', prepareCSV),
method('table.html', prepareHTMLTable),
method('chart.html', prepareHTMLChart),
method('table.md', prepareMarkdownTable),
method(prepareJSON),
)

Expand Down
82 changes: 82 additions & 0 deletions src/suite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,88 @@ describe('suite', () => {
TIMEOUT,
)

it(
'Correctly saves simple Markdown table',
async () => {
await suite(
'Example 17',

add(
'First',
() => {
return () => [1, 2, 3, 4, 5].reduce((a, b) => a + b)
},
{ maxTime: 0.01 },
),

add(
'Second',
() => {
return () => [1, 2].reduce((a, b) => a + b)
},
{ maxTime: 0.01 },
),

save({ format: 'table.md' }),
)

await delay(1000)

const file = fs.readdirSync('benchmark/results')[0]

const content = fs.readFileSync(`benchmark/results/${file}`).toString()

const lineSplit = content.split('\n')

expect(lineSplit[0]).toMatch(/.*(Example 17).*$/)
expect(lineSplit[2]).toMatch(/.*(name).*(ops).*(margin).*(percentSlower).*$/)
expect(lineSplit[4]).toMatch(/.*(First).*$/)
expect(lineSplit[5]).toMatch(/.*(Second).*$/)
},
TIMEOUT,
)

it(
'Correctly saves detailed Markdown table',
async () => {
await suite(
'Example 18',

add(
'First',
() => {
return () => [1, 2, 3, 4, 5].reduce((a, b) => a + b)
},
{ maxTime: 0.01 },
),

add(
'Second',
() => {
return () => [1, 2].reduce((a, b) => a + b)
},
{ maxTime: 0.01 },
),

save({ format: 'table.md', details: true }),
)

await delay(1000)

const file = fs.readdirSync('benchmark/results')[0]

const content = fs.readFileSync(`benchmark/results/${file}`).toString()

const lineSplit = content.split('\n')

expect(lineSplit[0]).toMatch(/.*(Example 18).*$/)
expect(lineSplit[2]).toMatch(/.*(name).*(ops).*(margin).*(percentSlower).*(samples).*(promise).*(min).*(max).*(mean).*(median).*(standardDeviation).*(marginOfError).*(standardErrorOfMean).*(sampleVariance).*$/)
expect(lineSplit[4]).toMatch(/.*(First).*$/)
expect(lineSplit[5]).toMatch(/.*(Second).*$/)
},
TIMEOUT,
)

it(
'Rounds results to smallest distinctive precision',
async () => {
Expand Down