Skip to content

Commit 417fccd

Browse files
authored
Sort tests by name when creating servo score report (#68)
* Sort tests by name when creating servo score report Signed-off-by: Nico Burns <[email protected]> * Use Map for non-quirky sorting order Signed-off-by: Nico Burns <[email protected]> --------- Signed-off-by: Nico Burns <[email protected]>
1 parent f3e4a40 commit 417fccd

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ async function all_runs_sorted (runs_dir) {
3636
const dir = await opendir(`./${runs_dir}`)
3737
const runs = []
3838
for await (const run of dir) {
39+
if (run.name === '.DS_Store') continue
3940
runs.push(run.name)
4041
}
4142

@@ -47,6 +48,8 @@ async function process_chunks (path) {
4748
const dir = await opendir(path)
4849
let result = {}
4950
for await (const chunk of dir) {
51+
if (chunk.name === '.DS_Store') continue
52+
console.log(`Processing chunk ${chunk.name}`)
5053
const chunk_run = await read_json_file(`${path}/${chunk.name}`)
5154
const scored_chunk = process_raw_results(chunk_run)
5255
if (!result.run_info) {
@@ -59,9 +62,44 @@ async function process_chunks (path) {
5962
delete scored_chunk.run_info
6063
result = merge_nonoverlap(result, scored_chunk)
6164
}
65+
66+
sort_test_results(result)
67+
6268
return result
6369
}
6470

71+
// Sorts the keys of the test_scores object alphabetically
72+
function sort_test_results (result) {
73+
console.log('Sorting results')
74+
result.test_scores = into_sorted_map(result.test_scores)
75+
}
76+
77+
// Convert an object into a sorted Map
78+
//
79+
// JS Maps serialize keys in insertion order, so to control the order of JSON serialized output
80+
// we can:
81+
//
82+
// - Convert the object into an array
83+
// - Sort the array by test object key
84+
// - Reinsert the results into a new Map in order
85+
//
86+
// Storing the result in a Map rather a regular object allows us to have full control over the order
87+
function into_sorted_map (obj) {
88+
const arr = Object.entries(obj).map(([key, value]) => ({ key, value }))
89+
arr.sort((a, b) => {
90+
if (a.key < b.key) return -1
91+
if (a.key > b.key) return 1
92+
return 0
93+
})
94+
95+
const map = new Map()
96+
for (const entry of arr) {
97+
map[entry.key] = entry.value
98+
}
99+
100+
return map
101+
}
102+
65103
async function add_run (runs_dir, chunks_dir, date) {
66104
const new_run = await process_chunks(chunks_dir)
67105
await write_compressed(`./${runs_dir}/${date}.xz`, new_run)

0 commit comments

Comments
 (0)