Repository navigation
Expand file tree
/
Copy pathbenchmark.js
More file actions
78 lines (71 loc) · 2.55 KB
/
Copy pathbenchmark.js
File metadata and controls
78 lines (71 loc) · 2.55 KB
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
import { createWriteStream, promises as fs } from 'fs'
import { pipeline } from 'stream/promises'
import { asyncBufferFromFile, parquetMetadataAsync, parquetReadObjects, parquetSchema } from 'hyparquet'
import { snappyCompressor } from 'hysnappy'
import { parquetWriteFile } from './src/node.js'
const url = 'https://s3.hyperparam.app/wiki-en-00000-of-00041.parquet'
const filename = 'data/wiki-en-00000-of-00041.parquet'
// download test parquet file if needed
let stat = await fs.stat(filename).catch(() => undefined)
if (!stat) {
console.log('downloading ' + url)
const res = await fetch(url)
if (!res.ok) throw new Error(res.statusText)
// write to file async
await pipeline(res.body, createWriteStream(filename))
stat = await fs.stat(filename)
console.log('downloaded example.parquet', stat.size)
}
// asyncBuffer
const file = await asyncBufferFromFile(filename)
console.log(`parsing ${filename} (${stat.size.toLocaleString()} bytes)`)
let startTime = performance.now()
// read parquet file
const metadata = await parquetMetadataAsync(file)
const rows = await parquetReadObjects({
file,
metadata,
// columns: ['l_comment'],
// rowStart: 0,
// rowEnd: 100_000,
})
let ms = performance.now() - startTime
console.log(`parsed ${filename} ${rows.length.toLocaleString()} rows in ${ms.toFixed(0)} ms`)
// transpose rows
const schema = parquetSchema(metadata)
const columnData = schema.children.map(({ element }) => ({
name: element.name,
data: [],
pageIndex: true,
})) // .filter(({ name }) => name === 'l_comment')
for (const row of rows) {
for (const { name, data } of columnData) {
data.push(row[name])
}
}
// write parquet file
const outputFilename = 'data/benchmark-output.parquet'
console.log(`writing ${outputFilename} (${rows.length.toLocaleString()} rows)`)
startTime = performance.now()
parquetWriteFile({
filename: outputFilename,
columnData,
schema: metadata.schema,
compressors: { SNAPPY: snappyCompressor() },
// rowGroupSize: 200_000,
})
ms = performance.now() - startTime
stat = await fs.stat(outputFilename)
console.log(`wrote ${outputFilename} (${stat.size.toLocaleString()} bytes) in ${ms.toFixed(0)} ms`)
// check data is the same
const outputFile = await asyncBufferFromFile(outputFilename)
const outputRows = await parquetReadObjects({ file: outputFile })
for (let i = 0; i < rows.length; i++) {
const inputRow = JSON.stringify(rows[i])
const outputRow = JSON.stringify(outputRows[i])
if (inputRow !== outputRow) {
console.log(`row ${i} mismatch`)
console.log('input ', inputRow)
console.log('output', outputRow)
}
}