@@ -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,73 @@ 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 ,
75+ // 2, 3,
76+ ] )
77+ for ( const maxChainLength of [
78+ //
79+ // 100
80+ 1_000 ,
81+ ] ) {
82+ let buffer : Uint8Array ;
83+ const start = Date . now ( ) ;
84+ const chainL = chain . slice ( 0 , maxChainLength ) ;
85+ const chainLength = chainL . length ;
86+ const gridDimension = `${ grid . width } x${ grid . height } ` ;
6687
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- ) ;
88+ drawOptions . colorBackground = colorBackground ;
89+ animationOptions . frameByStep = frameByStep ;
8290
83- fs . writeFileSync (
84- __dirname + `/__snapshots__/benchmark-output-${ length } .gif` ,
85- buffer ! ,
86- ) ;
87- }
91+ const filename = `benchmark-output-${ implementation } -${ chainLength } -${ animationOptions . frameByStep } -${ colorBackground } .gif` ;
92+
93+ for (
94+ let k = 0 ;
95+ k < 10 && ( Date . now ( ) - start < 12_000 || k < 2 ) ;
96+ k ++
97+ ) {
98+ const s = performance . now ( ) ;
99+ buffer = await createGif (
100+ grid ,
101+ null ,
102+ chainL ,
103+ drawOptions ,
104+ animationOptions ,
105+ ) ;
106+ runs . push ( {
107+ durationMs : performance . now ( ) - s ,
108+ colorBackground,
109+ implementation,
110+ chainLength,
111+ gridDimension,
112+ frameByStep,
113+ fileSizeByte : buffer . length ,
114+ filename,
115+ } ) ;
116+ }
117+
118+ fs . writeFileSync ( __dirname + `/__snapshots__/${ filename } ` , buffer ! ) ;
119+
120+ fs . writeFileSync (
121+ __dirname + `/__snapshots__/benchmark-result.json` ,
122+ "[\n" + runs . map ( ( r ) => JSON . stringify ( r ) ) . join ( ",\n" ) + "\n]" ,
123+ ) ;
124+ }
88125} ) ( ) ;
0 commit comments