-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark
115 lines (107 loc) · 2.5 KB
/
benchmark
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
const algorithms = {
slpf: require('./slpf'),
seedFill: require('./seed-fill')
};
const { performance } = require('perf_hooks');
// validate command line arguments
const { argv } = require('yargs')
.usage('Usage: $0 -a algorithm -p polygon -s size -t tests')
.example(
'$0 -a slpf -p star -s 1024',
'Run SLPF to draw a star of size 1024'
)
.example(
'$0 -a seedFill -p diamond -t 10',
'Run Seed Fill to draw a diamond of size 512 ten times'
)
.options({
'a': {
alias: 'algorithm',
choices: ['slpf', 'seedFill'],
demandOption: true,
description: 'Chosen algorithm to run',
requiresArg: true,
type: 'string'
},
'p': {
alias: 'polygon',
choices: ['square', 'diamond', 'star'],
demandOption: true,
description: 'Chosen polygon to fill',
requiresArg: true,
type: 'string'
},
's': {
alias: 'size',
default: 512,
demandOption: true,
description: 'Chosen size to draw at',
requiresArg: true,
type: 'number'
},
't': {
alias: 'tests',
default: 1,
description: 'Chosen number of tests to average over',
requiresArg: true,
type: 'number'
}
})
.alias('h', 'help')
.demandCommand(0)
.showHelpOnFail(false, 'Specify --help for available options')
.version(false)
.strict();
// read in command line arguments
const { algorithm, polygon } = argv;
const size = Math.floor(argv.size);
const tests = Math.floor(argv.tests);
// validate size argument
if(size < 4) {
console.error('size argument must be at least 4');
process.exit(1);
}
// validate tests argument
if(tests < 1) {
console.error('tests argument must be at least 1');
process.exit(1);
}
// setup shapes
const W = size;
const H = size;
const shapes = {
square: [
[1, 1],
[W - 1, 1],
[W - 1, H - 1],
[1, H - 1]
],
diamond: [
[0, H >> 1],
[W >> 1, 0],
[W, H >> 1],
[W >> 1, H]
],
star: [
[W >> 1, 0],
[3 * W >> 2, H >> 2],
[W, H >> 2],
[3 * W >> 2, H >> 1],
[W, H],
[W >> 1, 3 * H >> 2],
[0, H],
[W >> 2, H >> 1],
[0, H >> 2],
[W >> 2, H >> 2]
]
};
// benchmark algorithm
const label = `${algorithm} (${size}px ${polygon})`;
let timeElapsed = 0;
for(let i = 0; i < tests; i++) {
const t0 = performance.now();
algorithms[algorithm](shapes[polygon], [], [W >> 1, H >> 2]);
timeElapsed += performance.now() - t0;
}
console.log(`${label}: ${(timeElapsed / tests).toFixed(3)}ms`);