Skip to content

Commit a265632

Browse files
ponelatshockey
authored andcommitted
performance: add buffer for semantic error callback (#1628)
* add debounced buffer for semantic error callback A lot of wasted effort doing the operations immediately. * Minimize debounce delay in tests * LESS test debounce delay * Use CI flag to disable delay as well * Raise test harness semantic validation async waiting period * MOAR delay....
1 parent 5ca7f99 commit a265632

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"test": "npm run just-test-in-node && npm run lint-errors",
3434
"test-in-node": "npm run lint-errors && npm run just-test-in-node",
3535
"just-test": "karma start --config karma.conf.js",
36-
"just-test-in-node": "mocha --recursive --compilers js:babel-core/register test",
36+
"just-test-in-node": "NODE_ENV=test mocha --recursive --compilers js:babel-core/register test",
3737
"just-test-in-node-watch": "npm run just-test-in-node -- -w",
3838
"serve-static": "http-server -i -a 0.0.0.0 -p 3001",
3939
"prestart": "npm install",

src/plugins/validate-semantic/actions.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
import forEach from "lodash/forEach"
2+
import debounce from "lodash/debounce"
23

34
export const SOURCE = "semantic"
45

6+
// the test system does not tolerate slowness!
7+
const { NODE_ENV, CI } = process.env
8+
const DEBOUNCE_MS = (NODE_ENV === "test" || CI === "true") ? 0 : 30
9+
10+
// System for buffering/batching errors
11+
var errorCollector = []
12+
const debNewSpecErrBatch = debounce(() => {
13+
const system = errorCollector.system // Just a reference to the "latest" system
14+
try {
15+
errorCollector.forEach(obj => {
16+
obj.line = obj.line || system.fn.AST.getLineNumberForPath(system.specSelectors.specStr(), obj.path)
17+
obj.source = SOURCE
18+
})
19+
system.errActions.newSpecErrBatch(errorCollector)
20+
errorCollector = [] // Clear stack
21+
} catch(e) {
22+
console.error(e)
23+
}
24+
}, DEBOUNCE_MS)
25+
26+
const bufferedNewSpecErrBatch = (system, obj) => {
27+
errorCollector.push(obj)
28+
errorCollector.system = system
29+
debNewSpecErrBatch()
30+
}
31+
532
export const all = () => (system) => {
633

734
// don't run validation if spec is empty
@@ -13,20 +40,16 @@ export const all = () => (system) => {
1340
source: SOURCE
1441
})
1542

16-
const cb = (obj) => setTimeout(() => {
17-
obj.line = obj.line || system.fn.AST.getLineNumberForPath(system.specSelectors.specStr(), obj.path)
18-
obj.source = SOURCE
19-
system.errActions.newSpecErr(obj)
20-
}, 0)
43+
const errCb = (obj) => bufferedNewSpecErrBatch(system, obj)
2144

2245
forEach(system.validateActions, (fn, name) => {
2346
if(name.indexOf("validateAsync") === 0) {
24-
fn(cb) // Function send messages on its own, it won't be cached ( due to the nature of async operations )
47+
fn(errCb) // Function send messages on its own, it won't be cached ( due to the nature of async operations )
2548
} else if(name.indexOf("validate") === 0) {
2649
Promise.resolve(fn())
2750
.then(validationObjs => {
2851
if(validationObjs) {
29-
validationObjs.forEach(cb)
52+
validationObjs.forEach(errCb)
3053
}
3154
})
3255
}

test/plugins/validate-semantic/validate-helper.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ValidateBasePlugin from "plugins/validate-base"
55
import ValidateSemanticPlugin from "plugins/validate-semantic"
66
import ASTPlugin from "plugins/ast"
77

8+
const DELAY_MS = process.env.CI === "true" ? 100 : 60
9+
810
export default function validateHelper(spec) {
911
return new Promise((resolve) => {
1012
const system = SwaggerUi({
@@ -26,7 +28,7 @@ export default function validateHelper(spec) {
2628
]
2729
})
2830
system.validateActions.all()
29-
setTimeout(resolve.bind(null, system), 60)
31+
setTimeout(resolve.bind(null, system), DELAY_MS)
3032
})
3133

3234
}

0 commit comments

Comments
 (0)