-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompress-test.ts
executable file
·118 lines (102 loc) · 2.79 KB
/
compress-test.ts
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
116
117
118
#!/usr/bin/env ts-node
// tslint:disable no-var-requires
import { jsonToString } from '@beenotung/tslib/json'
import { startTimer } from '@beenotung/tslib/node'
import fs from 'fs'
import path from 'path'
import { compress, decompress } from '../src/core'
const timer = startTimer('load test data')
function toString(data: any) {
let text = jsonToString(data)
// console.log('== text ==')
// console.log(text)
// console.log('=====')
// console.log({text})
data = JSON.parse(text)
text = JSON.stringify(data, null, 2)
return text
}
function save(name: string, data: any) {
const file = path.join('data', name + '.json')
timer.next('save ' + file)
const text = typeof data === 'string' ? data : JSON.stringify(data)
// let text = JSON.stringify(data, null, 2)
fs.writeFileSync(file, text)
}
import { readJsonFileSync } from '@beenotung/tslib/fs'
let input = readJsonFileSync('data.json')
let sample = 100000
sample = 50000
sample = 10000
sample = 2000
sample = 1000
sample = 100
sample = 10
input = Object.fromEntries(Object.entries(input).splice(0, sample))
// import { sample } from './compress/test'
// let input = sample()
// let input = JSON.parse(fs.readFileSync('sample.txt').toString());
timer.next('prepare json string')
const inputStr = JSON.stringify(input)
let inputStrSorted: string // = toString(input)
// tslint:disable-next-line no-unused-declaration
function compare(name: string, input: any, reverse: any) {
if (!inputStrSorted) {
inputStrSorted = toString(input)
}
const reverseStrSorted = toString(reverse)
if (inputStrSorted === reverseStrSorted) {
return
}
fs.writeFileSync('input.txt', JSON.stringify(input, null, 2))
fs.writeFileSync('reverse.txt', JSON.stringify(reverse, null, 2))
throw new Error('compress/decompress mismatch: ' + name)
}
function test(name: string, c: (o: any) => any, d: (c: any) => any) {
timer.next('--------')
timer.next(name + '[compress]')
const output = c(input)
save(name, output)
if (!'compress only') {
return
}
timer.next(name + '[decompress]')
const reverse = d(output)
// save('reverse', reverse)
// skip to avoid out-of-memory error for large sample
if (!'skip compare') {
return
}
timer.next(name + '[compare]')
const reverseStr = JSON.stringify(reverse)
if (reverseStr === inputStr) {
return
}
console.log('\n not exact match')
// compare(name, input, reverse)
}
test(
'JSON',
o => JSON.stringify(o),
c => JSON.parse(c),
)
const cjson = require('compressed-json')
test(
'compressed-json',
o => cjson.compress(o),
c => cjson.decompress(c),
)
// too slow
const jsonpack = require('jsonpack')
test(
'jsonpack',
o => jsonpack.pack(o),
c => jsonpack.unpack(c),
)
test(
'compress-json',
o => compress(o),
c => decompress(c),
)
timer.end()
console.log('done.')