|
| 1 | +import { performance } from 'node:perf_hooks'; |
| 2 | +import { describe, expect, test } from 'vitest'; |
| 3 | +import { normalize, serialize, serializeConfigPresentation4, validatePresentation4 } from '../../src/presentation-4'; |
| 4 | + |
| 5 | +function createLargeCanvasManifest(canvasCount = 200, annotationsPerCanvas = 4) { |
| 6 | + return { |
| 7 | + '@context': 'http://iiif.io/api/presentation/4/context.json', |
| 8 | + id: 'https://example.org/iiif/perf/canvas-manifest', |
| 9 | + type: 'Manifest', |
| 10 | + label: { en: ['Large canvas manifest'] }, |
| 11 | + items: Array.from({ length: canvasCount }).map((_, canvasIndex) => ({ |
| 12 | + id: `https://example.org/iiif/perf/canvas/${canvasIndex + 1}`, |
| 13 | + type: 'Canvas', |
| 14 | + width: 4000, |
| 15 | + height: 3000, |
| 16 | + items: [ |
| 17 | + { |
| 18 | + id: `https://example.org/iiif/perf/canvas/${canvasIndex + 1}/page/1`, |
| 19 | + type: 'AnnotationPage', |
| 20 | + items: Array.from({ length: annotationsPerCanvas }).map((__, annotationIndex) => ({ |
| 21 | + id: `https://example.org/iiif/perf/canvas/${canvasIndex + 1}/annotation/${annotationIndex + 1}`, |
| 22 | + type: 'Annotation', |
| 23 | + motivation: ['painting'], |
| 24 | + body: [ |
| 25 | + { |
| 26 | + id: `https://example.org/iiif/perf/image/${canvasIndex + 1}-${annotationIndex + 1}.jpg`, |
| 27 | + type: 'Image', |
| 28 | + format: 'image/jpeg', |
| 29 | + }, |
| 30 | + ], |
| 31 | + target: [ |
| 32 | + { |
| 33 | + id: `https://example.org/iiif/perf/canvas/${canvasIndex + 1}`, |
| 34 | + type: 'Canvas', |
| 35 | + }, |
| 36 | + ], |
| 37 | + })), |
| 38 | + }, |
| 39 | + ], |
| 40 | + })), |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function createLargeSceneManifest(sceneCount = 120, modelsPerScene = 3) { |
| 45 | + return { |
| 46 | + '@context': 'http://iiif.io/api/presentation/4/context.json', |
| 47 | + id: 'https://example.org/iiif/perf/scene-manifest', |
| 48 | + type: 'Manifest', |
| 49 | + label: { en: ['Large scene manifest'] }, |
| 50 | + items: Array.from({ length: sceneCount }).map((_, sceneIndex) => ({ |
| 51 | + id: `https://example.org/iiif/perf/scene/${sceneIndex + 1}`, |
| 52 | + type: 'Scene', |
| 53 | + items: [ |
| 54 | + { |
| 55 | + id: `https://example.org/iiif/perf/scene/${sceneIndex + 1}/page/1`, |
| 56 | + type: 'AnnotationPage', |
| 57 | + items: Array.from({ length: modelsPerScene }).map((__, modelIndex) => ({ |
| 58 | + id: `https://example.org/iiif/perf/scene/${sceneIndex + 1}/annotation/${modelIndex + 1}`, |
| 59 | + type: 'Annotation', |
| 60 | + motivation: ['painting'], |
| 61 | + body: [ |
| 62 | + { |
| 63 | + id: `https://example.org/iiif/perf/model/${sceneIndex + 1}-${modelIndex + 1}.glb`, |
| 64 | + type: 'Model', |
| 65 | + format: 'model/gltf-binary', |
| 66 | + }, |
| 67 | + ], |
| 68 | + target: [ |
| 69 | + { |
| 70 | + id: `https://example.org/iiif/perf/scene/${sceneIndex + 1}`, |
| 71 | + type: 'Scene', |
| 72 | + }, |
| 73 | + ], |
| 74 | + })), |
| 75 | + }, |
| 76 | + ], |
| 77 | + })), |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe('presentation-4 performance scale tests', () => { |
| 82 | + test('normalizes and serializes large canvas manifests', () => { |
| 83 | + const manifest = createLargeCanvasManifest(); |
| 84 | + const started = performance.now(); |
| 85 | + const normalized = normalize(manifest); |
| 86 | + const serialized = serialize<any>( |
| 87 | + { |
| 88 | + entities: normalized.entities as any, |
| 89 | + mapping: normalized.mapping as any, |
| 90 | + requests: {}, |
| 91 | + }, |
| 92 | + normalized.resource, |
| 93 | + serializeConfigPresentation4 |
| 94 | + ); |
| 95 | + const report = validatePresentation4(manifest, { mode: 'tolerant' }); |
| 96 | + const elapsed = performance.now() - started; |
| 97 | + |
| 98 | + expect(report.valid).toBe(true); |
| 99 | + expect(normalized.resource.type).toBe('Manifest'); |
| 100 | + expect(serialized.id).toBe(manifest.id); |
| 101 | + expect(Object.keys(normalized.mapping).length).toBeGreaterThan(1500); |
| 102 | + expect(elapsed).toBeLessThan(20000); |
| 103 | + }); |
| 104 | + |
| 105 | + test('normalizes and serializes large scene manifests', () => { |
| 106 | + const manifest = createLargeSceneManifest(); |
| 107 | + const started = performance.now(); |
| 108 | + const normalized = normalize(manifest); |
| 109 | + const serialized = serialize<any>( |
| 110 | + { |
| 111 | + entities: normalized.entities as any, |
| 112 | + mapping: normalized.mapping as any, |
| 113 | + requests: {}, |
| 114 | + }, |
| 115 | + normalized.resource, |
| 116 | + serializeConfigPresentation4 |
| 117 | + ); |
| 118 | + const report = validatePresentation4(manifest, { mode: 'tolerant' }); |
| 119 | + const elapsed = performance.now() - started; |
| 120 | + |
| 121 | + expect(report.valid).toBe(true); |
| 122 | + expect(normalized.resource.type).toBe('Manifest'); |
| 123 | + expect(serialized.id).toBe(manifest.id); |
| 124 | + expect(Object.keys(normalized.entities.Scene).length).toBe(120); |
| 125 | + expect(elapsed).toBeLessThan(20000); |
| 126 | + }); |
| 127 | +}); |
0 commit comments