Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark tests #148

Merged
merged 17 commits into from
Jun 29, 2017
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ module.exports = {
'jsx',
'json'
],
verbose: true
verbose: true,
testPathIgnorePatterns: [
'node_modules',
'benchmark'
]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"lint": "eslint src test",
"start": "webpack-dev-server --config ./webpack.config.dev.js",
"test": "jest",
"test:bench": "node -r babel-register test/benchmark/parser.js",
"test:coverage": "jest --coverage",
"webpack:library": "rimraf dist/library/* && NODE_ENV=production webpack --config webpack.config.library.js"
},
Expand Down Expand Up @@ -81,6 +82,7 @@
"babel-preset-stage-2": "^6.24.1",
"babel-regenerator-runtime": "^6.5.0",
"babel-runtime": "^6.23.0",
"benchmark": "^2.1.4",
"css-loader": "^0.28.4",
"eslint": "^4.1.0",
"eslint-config-standard": "^10.2.1",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/clone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cloneDeep } from 'lodash'

/** Unsafe JSON clone */
// export const clone = (obj) => JSON.parse(JSON.stringify(obj))
export const clone = cloneDeep
9 changes: 4 additions & 5 deletions src/parser/open-api/allOfResolver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import clone from 'lodash/clone'
import cloneDeep from 'lodash/cloneDeep'
import { clone } from '../../lib/clone'
import uniq from 'lodash/uniq'

/**
Expand All @@ -10,7 +9,7 @@ import uniq from 'lodash/uniq'
* @return {Object}
*/
function resolveAllOfItem (node) {
const output = cloneDeep(node)
const output = clone(node)
delete output.allOf

const allOfItems = node.allOf
Expand All @@ -19,12 +18,12 @@ function resolveAllOfItem (node) {

Object.keys(item).forEach(key => {
if (!output.hasOwnProperty(key)) {
output[key] = cloneDeep(item[key])
output[key] = clone(item[key])
} else if (key === 'properties') {
const properties = item[key]

Object.keys(properties).forEach(name => {
output.properties[name] = cloneDeep(properties[name])
output.properties[name] = clone(properties[name])
})
} else if (key === 'required') {
// Concatenate to existing list and remove duplicates
Expand Down
6 changes: 3 additions & 3 deletions src/parser/open-api/oneOfResolver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cloneDeep from 'lodash/cloneDeep'
import { clone } from '../../lib/clone'
import get from 'lodash/get'
import update from 'lodash/update'
import toPath from 'lodash/toPath'
Expand Down Expand Up @@ -72,7 +72,7 @@ function getStates (paths, obj) {
* @return {object[]}
*/
function getStateAt (path, obj) {
const clonedObj = cloneDeep(obj)
const clonedObj = clone(obj)
const states = get(clonedObj, path)

// Couldn't retrieve the states at this path, bail.
Expand All @@ -90,7 +90,7 @@ function getStateAt (path, obj) {
// Replace the path with the state
update(clonedObj, parentPath, (value) => merge(value, state))

return cloneDeep(clonedObj)
return clone(clonedObj)
})
}

Expand Down
54 changes: 54 additions & 0 deletions test/benchmark/Benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Bench from 'benchmark'
import { writeFile, ensureDir } from 'fs-extra'
import { dirname } from 'path'
import { cloneDeep } from 'lodash'

export class Benchmark {
key;
config;

constructor (key, config = {}) {
Object.assign(this, { key, config })
}

async run (task) {
async function fn (defer) {
await task()
defer.resolve()
}

this.results = []

await new Promise((resolve) => {
const bench = new Bench(this.key, {
defer: true,
async: true,
onComplete: resolve,
onCycle: (event) => {
this.results.push(cloneDeep(event.target))

if (this.config.onCycle) { this.config.onCycle(event) }
},
fn,
...this.config
})

bench.run()
})

return this
}

serialize (results = this.results) {
return results.slice(-3).map((result) => {
return String(result)
}).join('\n')
}

async saveSnapshot (filePath, results = this.results) {
const dir = dirname(filePath)

await ensureDir(dir)
return writeFile(filePath, this.serialize(results), 'utf8')
}
}
2 changes: 2 additions & 0 deletions test/benchmark/definitions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as petStore } from './petStore'
export { default as temandoFull } from './temandoFull'
Loading