forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark-rule.js
61 lines (52 loc) · 1.65 KB
/
benchmark-rule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable no-console */
import Benchmark from "benchmark"
import request from "request"
import postcss from "postcss"
import chalk from "chalk"
import rules from "./src/rules"
import normalizeRuleSettings from "./src/normalizeRuleSettings"
const ruleName = process.argv[2]
const ruleOptions = process.argv[3]
if (!ruleName || !ruleOptions) {
throw new Error("You must specify a rule name and rule options")
}
const CSS_URL = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css"
let parsedOptions = ruleOptions
if (
ruleOptions[0] === "["
|| parsedOptions === "true"
|| parsedOptions === "false"
|| Number(parsedOptions) == parsedOptions
) {
parsedOptions = JSON.parse(ruleOptions)
}
const rule = rules[ruleName](...normalizeRuleSettings(parsedOptions))
const processor = postcss().use(rule)
request(CSS_URL, (error, response, body) => {
if (error) throw error
const bench = new Benchmark("rule test", {
defer: true,
fn: deferred => benchFn(body, () => deferred.resolve()),
})
bench.on("complete", () => {
console.log(`${chalk.bold("Mean")}: ${bench.stats.mean * 1000} ms`)
console.log(`${chalk.bold("Deviation")}: ${bench.stats.deviation * 1000} ms`)
})
bench.run()
})
let firstTime = true
function benchFn(css, done) {
processor.process(css).then(result => {
if (firstTime) {
firstTime = false
result.messages.filter(m => m.stylelintType === "invalidOption").forEach(m => {
console.log(chalk.bold.yellow(`>> ${m.text}`))
})
console.log(`${chalk.bold("Warnings")}: ${result.warnings().length}`)
}
done()
}).catch(err => {
console.log(err.stack)
done()
})
}