Skip to content

Commit

Permalink
feat: performance regression test の導入 (#107)
Browse files Browse the repository at this point in the history
* feat: add performance testing

* fix: testing failed to create complex object for testing

* refactor: remove describe block

* fix: bench実施処理を最適化

* fix: bench の一部がコケる問題を暫定的に対処

* misc: ついでのテストケース追加

* feat: codspeed 追加

* fix: こまい action 修正

* Create ninety-pens-suffer.md

* fix: 不要なコメントの削除

* fix: テスト縮小

* fix: テスト縮小
  • Loading branch information
mew-ton authored Jan 18, 2024
1 parent 5a4e9d1 commit 0054bcb
Show file tree
Hide file tree
Showing 24 changed files with 1,322 additions and 751 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-pens-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-origami": patch
---

unfold 関数にて、値が空配列・空オブジェクトのときに値が結果に反映されない問題の修正
3 changes: 2 additions & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"github>hacomono-lib/renovate-config:npm-lockfile",
"github>hacomono-lib/renovate-config:github-actions",
"github>hacomono-lib/renovate-config:pre-commit"
]
],
"platformAutomerge": true
}
36 changes: 36 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Codspeed Benchmarks

permissions:
contents: read
packages: read
pull-requests: write

on:
push:
branches:
- "main"
pull_request:

jobs:
init__node:
if: |
!contains(github.event.pull_request.labels.*.name, 'renovate')
name: "Initialize: node"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/init-node

benchmarks:
runs-on: ubuntu-latest
needs:
- init__node
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/init-node

- name: Run benchmarks
uses: CodSpeedHQ/action@v2
with:
run: yarn bench
token: ${{ secrets.CODSPEED_TOKEN }}
3 changes: 3 additions & 0 deletions .vscode/project.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
],
"settings": {
"typescript.tsdk": "node_modules/typescript/lib"
},
"extensions": {
"recommendations": ["zixuanchen.vitest-explorer"]
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"scripts": {
"dev": "vitest --ui",
"bench": "vitest bench",
"build": "tsup",
"test": "run-p test:*",
"test:spec": "vitest --run --silent",
Expand All @@ -51,11 +52,13 @@
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"@vitest/ui": "^1.2.0",
"defu": "^6.1.4",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"prettier": "^3.2.2",
"tsup": "^8.0.1",
"type-fest": "^4.9.0",
"typescript": "^5.3.3",
"vitest": "^1.2.0",
"yarn-run-all": "latest"
Expand Down
8 changes: 5 additions & 3 deletions src/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
* ```
*/
export function fold<D extends Dictionary>(obj: D, option?: FoldOption): Folded<D> {
if (Object.keys(obj).length <= 0) return {}

return Object.fromEntries(
flatEntries(option?.keyPrefix ?? '', obj, {
...defaultCommonOption,
Expand All @@ -52,13 +54,13 @@ function flatEntries(key: string, value: object, opt: FixedFoldOption): Array<[s
const appendKey = (k: string | number) =>
typeof k === 'number' ? arrayKeyMap[opt.arrayIndex](key, k) : key === '' ? k : `${key}.${k}`

if (Array.isArray(value)) {
if (Array.isArray(value) && value.length > 0) {
return value.flatMap((v, i) => flatEntries(appendKey(i), v, opt))
}

if (typeof value === 'object') {
if (typeof value === 'object' && Object.keys(value).length > 0) {
return Object.entries(value as object).flatMap(([k, v]) => flatEntries(appendKey(k), v, opt))
}

return [[key, value as Primitive]]
return [[key, value]]
}
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
export type Primitive = string | number | boolean
export type Primitive = string | number | boolean | {} | []

type MaybeReadonly<T> = T | (T extends Array<infer U> ? readonly U[] : Readonly<T>)

Expand Down
36 changes: 36 additions & 0 deletions test/fold.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { bench, describe } from 'vitest'
import {
createRandomObject,
BENCHMARK_TARGET_OBJECT_VALUES,
BENCHMARK_TARGET_LIGHT_OBJECT_VALUES
} from './utils'
import { fold } from '../src/fold'

const iterations = 10

interface TestCaseOption {
/**
* 生成するオブジェクトの値の数
*/
objectValues: number
}

function runBench({ objectValues }: TestCaseOption) {
const objects = Array.from({ length: iterations }, () =>
createRandomObject({ leafs: objectValues })
)
let index = 0

bench(`fold (complex object including ${objectValues} values)`, () => {
const object = objects[index++ % iterations]
fold(object)
})
}

describe('fold with light object', () => {
runBench({ objectValues: BENCHMARK_TARGET_LIGHT_OBJECT_VALUES })
})

describe('fold with heavy object', () => {
runBench({ objectValues: BENCHMARK_TARGET_OBJECT_VALUES })
})
Loading

0 comments on commit 0054bcb

Please sign in to comment.