@@ -2,10 +2,21 @@ import * as fs from "fs";
22import { performance } from "perf_hooks" ;
33import { createSnakeFromCells } from "@snk/types/snake" ;
44import { realistic as grid } from "@snk/types/__fixtures__/grid" ;
5- import { AnimationOptions , createGif } from ".." ;
5+ import {
6+ type AnimationOptions ,
7+ type DrawOptions ,
8+ createGif as createGif_gifencore_gifslice ,
9+ } from ".." ;
610import { getBestRoute } from "@snk/solver/getBestRoute" ;
711import { getPathToPose } from "@snk/solver/getPathToPose" ;
8- import type { Options as DrawOptions } from "@snk/draw/drawWorld" ;
12+ import {
13+ canvasDrawNoOutput ,
14+ createGif_ffmpeg ,
15+ createGif_ffmpeg_gifslice ,
16+ createGif_gifEncoder ,
17+ createGif_graphicMagic ,
18+ createPngImageSequenceNoOutput ,
19+ } from "./createGif-variant" ;
920
1021let snake = createSnakeFromCells (
1122 Array . from ( { length : 4 } , ( _ , i ) => ( { x : i , y : - 1 } ) ) ,
@@ -42,47 +53,72 @@ const animationOptions: AnimationOptions = {
4253} ;
4354
4455( async ( ) => {
45- for (
46- let length = 10 ;
47- length < chain . length ;
48- length += Math . floor ( ( chain . length - 10 ) / 3 / 10 ) * 10
49- ) {
50- const stats : number [ ] = [ ] ;
56+ const runs : any [ ] = [ ] ;
5157
52- let buffer : Uint8Array ;
53- const start = Date . now ( ) ;
54- const chainL = chain . slice ( 0 , length ) ;
55- for ( let k = 0 ; k < 10 && ( Date . now ( ) - start < 10 * 1000 || k < 2 ) ; k ++ ) {
56- const s = performance . now ( ) ;
57- buffer = await createGif (
58- grid ,
59- null ,
60- chainL ,
61- drawOptions ,
62- animationOptions ,
63- ) ;
64- stats . push ( performance . now ( ) - s ) ;
65- }
58+ for ( const [ implementation , createGif ] of [
59+ [ "draw steps on canvas (no output)" , canvasDrawNoOutput ] ,
60+ [ "create png image sequence (no output)" , createPngImageSequenceNoOutput ] ,
61+ [ "gifEncoder" , createGif_gifEncoder ] ,
62+ [ "gifEncoder+gifslice" , createGif_gifencore_gifslice ] ,
63+ [ "ffmpeg" , createGif_ffmpeg ] ,
64+ [ "ffmpeg+gifslice" , createGif_ffmpeg_gifslice ] ,
65+ [ "graphicMagic" , createGif_graphicMagic ] ,
66+ ] as const )
67+ for ( const colorBackground of [
68+ //
69+ "transparent" ,
70+ "white" ,
71+ ] )
72+ for ( const frameByStep of [
73+ //
74+ 1 , 2 , 3 ,
75+ ] )
76+ for ( const maxChainLength of [
77+ //
78+ 1_000 ,
79+ ] ) {
80+ let buffer : Uint8Array ;
81+ const start = Date . now ( ) ;
82+ const chainL = chain . slice ( 0 , maxChainLength ) ;
83+ const chainLength = chainL . length ;
84+ const gridDimension = `${ grid . width } x${ grid . height } ` ;
6685
67- console . log (
68- [
69- "---" ,
70- `grid dimension: ${ grid . width } x${ grid . height } ` ,
71- `chain length: ${ length } ` ,
72- `resulting size: ${ ( buffer ! . length / 1024 ) . toFixed ( 1 ) } ko` ,
73- `generation duration (mean): ${ (
74- stats . reduce ( ( s , x ) => x + s ) / stats . length
75- ) . toLocaleString ( undefined , {
76- maximumFractionDigits : 0 ,
77- } ) } ms`,
78- "" ,
79- ] . join ( "\n" ) ,
80- stats ,
81- ) ;
86+ drawOptions . colorBackground = colorBackground ;
87+ animationOptions . frameByStep = frameByStep ;
8288
83- fs . writeFileSync (
84- __dirname + `/__snapshots__/benchmark-output-${ length } .gif` ,
85- buffer ! ,
86- ) ;
87- }
89+ const filename = `benchmark-output-${ implementation } -${ chainLength } -${ animationOptions . frameByStep } -${ colorBackground } .gif` ;
90+
91+ for (
92+ let k = 0 ;
93+ k < 10 && ( Date . now ( ) - start < 12_000 || k < 2 ) ;
94+ k ++
95+ ) {
96+ const s = performance . now ( ) ;
97+ buffer = await createGif (
98+ grid ,
99+ null ,
100+ chainL ,
101+ drawOptions ,
102+ animationOptions ,
103+ ) ;
104+ runs . push ( {
105+ durationMs : performance . now ( ) - s ,
106+ colorBackground,
107+ implementation,
108+ chainLength,
109+ gridDimension,
110+ frameByStep,
111+ fileSizeByte : buffer . length ,
112+ filename,
113+ } ) ;
114+ }
115+
116+ if ( buffer ! . length > 0 )
117+ fs . writeFileSync ( __dirname + `/__snapshots__/${ filename } ` , buffer ! ) ;
118+
119+ fs . writeFileSync (
120+ __dirname + `/__snapshots__/benchmark-result.json` ,
121+ "[\n" + runs . map ( ( r ) => JSON . stringify ( r ) ) . join ( ",\n" ) + "\n]" ,
122+ ) ;
123+ }
88124} ) ( ) ;
0 commit comments