Markiewicz_praca/benchmarks.js

167 lines
5.0 KiB
JavaScript

const print = (x)=>console.log(x)
const fs = require('fs');
const mockData = require("./data.js");
var { performance } = require('perf_hooks');
const {init, createSingleChart} = require("./nativeSsr.js")
const { fig2img } = require("./ssr.js");
const renderer = require("./ssr.js")
const {getMultipleBarsChartDataFromAnkieterDataFormat, rawData,prepareOptionsForMultipleBarsChartData} = require("./optionsGenerator.js")
const benchmark_to_csv = (results)=>{
let csv = "";
for (let [k,v] of Object.entries(results))
csv += `${k}, ${v}\n`
return csv
}
const timeit = (fun, number=1,prepare=()=>null)=>{
let time=0
for(let _=0; _<number;_++){
// console.log(_)
prepare()
const startTime = performance.now();
fun()
const endTime = performance.now();
// console.log(endTime-startTime)
time+=endTime-startTime;
}
return time
}
const benchmark_linear=(start,end,func,prepare=()=>null)=>{
const out ={};
for (let i =start; i<end; i++)
out[i] = timeit(func, i,prepare);
return out;
}
const benchmark_series = (number, func, prepare=()=>null)=>{
const out ={};
for (let i =0; i<number; i++)
out[i] = timeit(func, 1,prepare);
return out;
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
// let root = renderer.createNewRoot()
// console.log(root)
const measureUnits = async ()=>{
const c = performance.now()
await sleep(1000)
console.log(performance.now()-c)
}
let option = prepareOptionsForMultipleBarsChartData(getMultipleBarsChartDataFromAnkieterDataFormat(mockData.response))
fs.writeFileSync("option.txt",JSON.stringify(option))
option = {
legend: {},
tooltip: {},
dataset: {
// Define the dimension of array. In cartesian coordinate system,
// if the type of x-axis is category, map the first dimension to
// x-axis by default, the second dimension to y-axis.
// You can also specify 'series.encode' to complete the map
// without specify dimensions. Please see below.
dimensions: ['1','2015','2016'],
source: [
{ "1": 5, '2015': 43.3, '2016': 85.8, '2017': 93.7 },
{ "1": 2, '2015': 83.1, '2016': 73.4, '2017': 55.1 },
{ "1": 1, '2015': 86.4, '2016': 65.2, '2017': 82.5 },
{ "1": 8, '2015': 72.4, '2016': 53.9, '2017': 39.1 }
]
},
xAxis: { type: 'value' },
yAxis: {type:"category"},
series: [{ type: 'bar', 'stack':'total' }, { type: 'bar','stack':'total' }, { type: 'bar','stack':'total' }]
};
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
option = {
xAxis: {
type: 'value',
},
yAxis: {
type: 'category',
data: ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']
},
series: [
{
data: [1,2,3,4,5],
type: 'bar'
}
]
};
const benchmarkDataTransformationFromAnkieterToEchartsFormat = ()=>{
console.log(benchmark_linear(1,100, ()=>prepareOptionsForMultipleBarsChartData(getMultipleBarsChartDataFromAnkieterDataFormat(mockData.response))))
console.log(benchmark_to_csv(benchmark_linear(1,100, ()=>prepareOptionsForMultipleBarsChartData(getMultipleBarsChartDataFromAnkieterDataFormat(mockData.response)))))
}
const warmup = ()=>{
benchmark_to_csv(benchmark_linear(1,20,
()=>{
return createSingleChart(option)
},
()=>{
option.series[0].data=[Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random()]
}
)
)
}
// warmup()
const number=100
benchmarkDataTransformationFromAnkieterToEchartsFormat()
// fs.writeFileSync("barPlotNode.svg", createSingleChart(option))
// const res1 = benchmark_to_csv(benchmark_linear(1,number,
// ()=>{
// return createSingleChart(option)
// },
// ()=>{
// // option = JSON.parse(JSON.stringify(option))
// // option.series[0].data=[Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random()]
// }
// )
// )
// print("Liniowy benchmark z generowanien nowej instancji wykresu za każdym razem")
// print(res1)
// console.log(createSingleChart(option))
// const chartInstance = generateChartObject(root)
// const linearBenchmarkOnExistingInstance = benchmark_to_csv(benchmark_linear(1,number, ()=>{
// return generateChart(root,chartInstance,option)
// }))
// print("Liniowy benchmark na wczesniej przygotowanej instancji wykresu")
// print(linearBenchmarkOnExistingInstance)
// const res2 = benchmark_to_csv(benchmark_linear(1,number,()=>fig2img(root,generateChart(root, chartInstance,option), false) ))
// print("Liniowy benchmark create from scratch + save")
// print(res2)
// let linearBufforSaveInstance = generateChartObject(root)
// let figure = generateChart(root,linearBufforSaveInstance, option)
// const res3= benchmark_to_csv(benchmark_linear(1,number,()=>fig2img(root, figure, false) ))
// print("Liniowy benchmark zapisywania do bufora")
// print(res3)