|
1 |
| -import { baseCompile } from '@intlify/message-compiler' |
| 1 | +import { createCommonJS } from 'mlly' |
2 | 2 | import {
|
3 | 3 | translate,
|
4 | 4 | createCoreContext,
|
5 | 5 | clearCompileCache
|
6 | 6 | } from '@intlify/core-base'
|
7 | 7 | import { createI18n } from 'vue-i18n'
|
8 |
| -import convertHrtime from 'convert-hrtime' |
9 | 8 | import { resolve, dirname } from 'pathe'
|
10 | 9 | import { readJson } from './utils.mjs'
|
11 | 10 |
|
12 |
| -async function run() { |
13 |
| - const simpleData = await readJson( |
14 |
| - resolve(dirname('.'), './benchmark/simple.json') |
15 |
| - ) |
16 |
| - const len = Object.keys(simpleData).length |
| 11 | +const { require } = createCommonJS(import.meta.url) |
| 12 | +const { Suite } = require('benchmark') |
17 | 13 |
|
18 |
| - console.log('simple pattern ...') |
19 |
| - |
20 |
| - console.log(`compile time: ${len} resources`) |
21 |
| - let start = convertHrtime(process.hrtime.bigint()) |
22 |
| - for (const [, source] of Object.entries(simpleData)) { |
23 |
| - baseCompile(source) |
24 |
| - } |
25 |
| - let end = convertHrtime(process.hrtime.bigint()) |
26 |
| - console.log(`sec: ${end.seconds - start.seconds}`) |
27 |
| - console.log(`ms: ${end.milliseconds - start.milliseconds}`) |
| 14 | +async function main() { |
| 15 | + const data = await readJson(resolve(dirname('.'), './benchmark/simple.json')) |
| 16 | + const len = Object.keys(data).length |
28 | 17 |
|
| 18 | + console.log(`simple pattern on ${len} resources:`) |
29 | 19 | console.log()
|
30 | 20 |
|
31 |
| - console.log(`resolve time with core: ${len} resources`) |
32 |
| - const ctx = createCoreContext({ |
33 |
| - locale: 'en', |
34 |
| - messages: { |
35 |
| - en: simpleData |
36 |
| - } |
37 |
| - }) |
38 |
| - start = convertHrtime(process.hrtime.bigint()) |
39 |
| - for (const [key] of Object.entries(simpleData)) { |
40 |
| - translate(ctx, key) |
41 |
| - } |
42 |
| - end = convertHrtime(process.hrtime.bigint()) |
43 |
| - console.log(`ms: ${end.milliseconds - start.milliseconds}`) |
44 |
| - |
45 |
| - clearCompileCache() |
46 |
| - console.log() |
| 21 | + let i18n |
47 | 22 |
|
48 |
| - console.log(`resolve time on composition: ${len} resources`) |
49 |
| - const i18n = createI18n({ |
50 |
| - legacy: false, |
51 |
| - locale: 'en', |
52 |
| - messages: { |
53 |
| - en: simpleData |
54 |
| - } |
55 |
| - }) |
56 |
| - start = convertHrtime(process.hrtime.bigint()) |
57 |
| - for (const [key] of Object.entries(simpleData)) { |
58 |
| - i18n.global.t(key) |
59 |
| - } |
60 |
| - end = convertHrtime(process.hrtime.bigint()) |
61 |
| - console.log(`ms: ${end.milliseconds - start.milliseconds}`) |
| 23 | + new Suite('complex pattern') |
| 24 | + .add(`resolve time with core`, () => { |
| 25 | + const ctx = createCoreContext({ |
| 26 | + locale: 'en', |
| 27 | + messages: { |
| 28 | + en: data |
| 29 | + } |
| 30 | + }) |
| 31 | + for (const [key] of Object.entries(data)) { |
| 32 | + translate(ctx, key) |
| 33 | + } |
| 34 | + }) |
| 35 | + .add(`resolve time on composition`, () => { |
| 36 | + clearCompileCache() |
62 | 37 |
|
63 |
| - console.log( |
64 |
| - `resolve time on composition with compile cache: ${len} resources` |
65 |
| - ) |
66 |
| - start = convertHrtime(process.hrtime.bigint()) |
67 |
| - for (const [key] of Object.entries(simpleData)) { |
68 |
| - i18n.global.t(key) |
69 |
| - } |
70 |
| - end = convertHrtime(process.hrtime.bigint()) |
71 |
| - console.log(`ms: ${end.milliseconds - start.milliseconds}`) |
| 38 | + i18n = createI18n({ |
| 39 | + legacy: false, |
| 40 | + locale: 'en', |
| 41 | + messages: { |
| 42 | + en: data |
| 43 | + } |
| 44 | + }) |
| 45 | + for (const [key] of Object.entries(data)) { |
| 46 | + i18n.global.t(key) |
| 47 | + } |
| 48 | + }) |
| 49 | + .add(`resolve time on composition with compile cache`, () => { |
| 50 | + for (const [key] of Object.entries(data)) { |
| 51 | + i18n.global.t(key) |
| 52 | + } |
| 53 | + }) |
| 54 | + .on('error', event => { |
| 55 | + console.log(String(event.target)) |
| 56 | + }) |
| 57 | + .on('cycle', event => { |
| 58 | + console.log(String(event.target)) |
| 59 | + }) |
| 60 | + .run() |
72 | 61 | }
|
73 | 62 |
|
74 |
| -;(async () => { |
75 |
| - try { |
76 |
| - await run() |
77 |
| - } catch (e) { |
78 |
| - console.error(e) |
79 |
| - } |
80 |
| -})() |
| 63 | +main().catch(err => { |
| 64 | + console.error(err) |
| 65 | + process.exit(1) |
| 66 | +}) |
0 commit comments