rag-lite-ts 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cli → cjs/cli}/indexer.js +1 -1
- package/dist/{cli → cjs/cli}/search.js +5 -10
- package/dist/{core → cjs/core}/binary-index-format.d.ts +28 -2
- package/dist/cjs/core/binary-index-format.js +291 -0
- package/dist/{core → cjs/core}/ingestion.d.ts +5 -1
- package/dist/{core → cjs/core}/ingestion.js +76 -9
- package/dist/{core → cjs/core}/model-validator.js +1 -1
- package/dist/{core → cjs/core}/reranking-strategies.js +4 -5
- package/dist/{core → cjs/core}/search.js +2 -1
- package/dist/{core → cjs/core}/types.d.ts +1 -1
- package/dist/{core → cjs/core}/vector-index.d.ts +4 -0
- package/dist/{core → cjs/core}/vector-index.js +10 -2
- package/dist/{file-processor.d.ts → cjs/file-processor.d.ts} +2 -0
- package/dist/{file-processor.js → cjs/file-processor.js} +20 -0
- package/dist/{index-manager.d.ts → cjs/index-manager.d.ts} +17 -1
- package/dist/{index-manager.js → cjs/index-manager.js} +148 -7
- package/dist/{multimodal → cjs/multimodal}/clip-embedder.js +71 -66
- package/dist/esm/api-errors.d.ts +90 -0
- package/dist/esm/api-errors.js +320 -0
- package/dist/esm/cli/indexer.d.ts +11 -0
- package/dist/esm/cli/indexer.js +471 -0
- package/dist/esm/cli/search.d.ts +7 -0
- package/dist/esm/cli/search.js +332 -0
- package/dist/esm/cli.d.ts +3 -0
- package/dist/esm/cli.js +529 -0
- package/dist/esm/config.d.ts +51 -0
- package/dist/esm/config.js +79 -0
- package/dist/esm/core/abstract-embedder.d.ts +125 -0
- package/dist/esm/core/abstract-embedder.js +264 -0
- package/dist/esm/core/actionable-error-messages.d.ts +60 -0
- package/dist/esm/core/actionable-error-messages.js +397 -0
- package/dist/esm/core/adapters.d.ts +93 -0
- package/dist/esm/core/adapters.js +139 -0
- package/dist/esm/core/batch-processing-optimizer.d.ts +155 -0
- package/dist/esm/core/batch-processing-optimizer.js +536 -0
- package/dist/esm/core/binary-index-format.d.ts +78 -0
- package/dist/esm/core/binary-index-format.js +291 -0
- package/dist/esm/core/chunker.d.ts +119 -0
- package/dist/esm/core/chunker.js +73 -0
- package/dist/esm/core/cli-database-utils.d.ts +53 -0
- package/dist/esm/core/cli-database-utils.js +239 -0
- package/dist/esm/core/config.d.ts +102 -0
- package/dist/esm/core/config.js +247 -0
- package/dist/esm/core/content-errors.d.ts +111 -0
- package/dist/esm/core/content-errors.js +362 -0
- package/dist/esm/core/content-manager.d.ts +335 -0
- package/dist/esm/core/content-manager.js +1476 -0
- package/dist/esm/core/content-performance-optimizer.d.ts +150 -0
- package/dist/esm/core/content-performance-optimizer.js +516 -0
- package/dist/esm/core/content-resolver.d.ts +104 -0
- package/dist/esm/core/content-resolver.js +285 -0
- package/dist/esm/core/cross-modal-search.d.ts +164 -0
- package/dist/esm/core/cross-modal-search.js +342 -0
- package/dist/esm/core/database-connection-manager.d.ts +109 -0
- package/dist/esm/core/database-connection-manager.js +310 -0
- package/dist/esm/core/db.d.ts +213 -0
- package/dist/esm/core/db.js +895 -0
- package/dist/esm/core/embedder-factory.d.ts +154 -0
- package/dist/esm/core/embedder-factory.js +311 -0
- package/dist/esm/core/error-handler.d.ts +112 -0
- package/dist/esm/core/error-handler.js +239 -0
- package/dist/esm/core/index.d.ts +59 -0
- package/dist/esm/core/index.js +69 -0
- package/dist/esm/core/ingestion.d.ts +202 -0
- package/dist/esm/core/ingestion.js +901 -0
- package/dist/esm/core/interfaces.d.ts +408 -0
- package/dist/esm/core/interfaces.js +106 -0
- package/dist/esm/core/lazy-dependency-loader.d.ts +147 -0
- package/dist/esm/core/lazy-dependency-loader.js +435 -0
- package/dist/esm/core/mode-detection-service.d.ts +150 -0
- package/dist/esm/core/mode-detection-service.js +565 -0
- package/dist/esm/core/mode-model-validator.d.ts +92 -0
- package/dist/esm/core/mode-model-validator.js +203 -0
- package/dist/esm/core/model-registry.d.ts +116 -0
- package/dist/esm/core/model-registry.js +411 -0
- package/dist/esm/core/model-validator.d.ts +217 -0
- package/dist/esm/core/model-validator.js +782 -0
- package/dist/esm/core/path-manager.d.ts +47 -0
- package/dist/esm/core/path-manager.js +71 -0
- package/dist/esm/core/raglite-paths.d.ts +121 -0
- package/dist/esm/core/raglite-paths.js +145 -0
- package/dist/esm/core/reranking-config.d.ts +42 -0
- package/dist/esm/core/reranking-config.js +147 -0
- package/dist/esm/core/reranking-factory.d.ts +92 -0
- package/dist/esm/core/reranking-factory.js +410 -0
- package/dist/esm/core/reranking-strategies.d.ts +310 -0
- package/dist/esm/core/reranking-strategies.js +650 -0
- package/dist/esm/core/resource-cleanup.d.ts +163 -0
- package/dist/esm/core/resource-cleanup.js +371 -0
- package/dist/esm/core/resource-manager.d.ts +212 -0
- package/dist/esm/core/resource-manager.js +564 -0
- package/dist/esm/core/search-pipeline.d.ts +111 -0
- package/dist/esm/core/search-pipeline.js +287 -0
- package/dist/esm/core/search.d.ts +141 -0
- package/dist/esm/core/search.js +320 -0
- package/dist/esm/core/streaming-operations.d.ts +145 -0
- package/dist/esm/core/streaming-operations.js +409 -0
- package/dist/esm/core/types.d.ts +66 -0
- package/dist/esm/core/types.js +6 -0
- package/dist/esm/core/universal-embedder.d.ts +177 -0
- package/dist/esm/core/universal-embedder.js +139 -0
- package/dist/esm/core/validation-messages.d.ts +99 -0
- package/dist/esm/core/validation-messages.js +334 -0
- package/dist/esm/core/vector-index.d.ts +72 -0
- package/dist/esm/core/vector-index.js +333 -0
- package/dist/esm/dom-polyfills.d.ts +6 -0
- package/dist/esm/dom-polyfills.js +37 -0
- package/dist/esm/factories/index.d.ts +27 -0
- package/dist/esm/factories/index.js +29 -0
- package/dist/esm/factories/ingestion-factory.d.ts +200 -0
- package/dist/esm/factories/ingestion-factory.js +477 -0
- package/dist/esm/factories/search-factory.d.ts +154 -0
- package/dist/esm/factories/search-factory.js +344 -0
- package/dist/esm/file-processor.d.ts +147 -0
- package/dist/esm/file-processor.js +963 -0
- package/dist/esm/index-manager.d.ts +116 -0
- package/dist/esm/index-manager.js +598 -0
- package/dist/esm/index.d.ts +75 -0
- package/dist/esm/index.js +110 -0
- package/dist/esm/indexer.d.ts +7 -0
- package/dist/esm/indexer.js +54 -0
- package/dist/esm/ingestion.d.ts +63 -0
- package/dist/esm/ingestion.js +124 -0
- package/dist/esm/mcp-server.d.ts +46 -0
- package/dist/esm/mcp-server.js +1820 -0
- package/dist/esm/multimodal/clip-embedder.d.ts +327 -0
- package/dist/esm/multimodal/clip-embedder.js +996 -0
- package/dist/esm/multimodal/index.d.ts +6 -0
- package/dist/esm/multimodal/index.js +6 -0
- package/dist/esm/preprocess.d.ts +19 -0
- package/dist/esm/preprocess.js +203 -0
- package/dist/esm/preprocessors/index.d.ts +17 -0
- package/dist/esm/preprocessors/index.js +38 -0
- package/dist/esm/preprocessors/mdx.d.ts +25 -0
- package/dist/esm/preprocessors/mdx.js +101 -0
- package/dist/esm/preprocessors/mermaid.d.ts +68 -0
- package/dist/esm/preprocessors/mermaid.js +329 -0
- package/dist/esm/preprocessors/registry.d.ts +56 -0
- package/dist/esm/preprocessors/registry.js +179 -0
- package/dist/esm/run-error-recovery-tests.d.ts +7 -0
- package/dist/esm/run-error-recovery-tests.js +101 -0
- package/dist/esm/search-standalone.d.ts +7 -0
- package/dist/esm/search-standalone.js +117 -0
- package/dist/esm/search.d.ts +99 -0
- package/dist/esm/search.js +177 -0
- package/dist/esm/test-utils.d.ts +18 -0
- package/dist/esm/test-utils.js +27 -0
- package/dist/esm/text/chunker.d.ts +33 -0
- package/dist/esm/text/chunker.js +279 -0
- package/dist/esm/text/embedder.d.ts +111 -0
- package/dist/esm/text/embedder.js +386 -0
- package/dist/esm/text/index.d.ts +8 -0
- package/dist/esm/text/index.js +9 -0
- package/dist/esm/text/preprocessors/index.d.ts +17 -0
- package/dist/esm/text/preprocessors/index.js +38 -0
- package/dist/esm/text/preprocessors/mdx.d.ts +25 -0
- package/dist/esm/text/preprocessors/mdx.js +101 -0
- package/dist/esm/text/preprocessors/mermaid.d.ts +68 -0
- package/dist/esm/text/preprocessors/mermaid.js +330 -0
- package/dist/esm/text/preprocessors/registry.d.ts +56 -0
- package/dist/esm/text/preprocessors/registry.js +180 -0
- package/dist/esm/text/reranker.d.ts +49 -0
- package/dist/esm/text/reranker.js +274 -0
- package/dist/esm/text/sentence-transformer-embedder.d.ts +96 -0
- package/dist/esm/text/sentence-transformer-embedder.js +340 -0
- package/dist/esm/text/tokenizer.d.ts +22 -0
- package/dist/esm/text/tokenizer.js +64 -0
- package/dist/esm/types.d.ts +83 -0
- package/dist/esm/types.js +3 -0
- package/dist/esm/utils/vector-math.d.ts +31 -0
- package/dist/esm/utils/vector-math.js +70 -0
- package/package.json +30 -12
- package/dist/core/binary-index-format.js +0 -122
- /package/dist/{api-errors.d.ts → cjs/api-errors.d.ts} +0 -0
- /package/dist/{api-errors.js → cjs/api-errors.js} +0 -0
- /package/dist/{cli → cjs/cli}/indexer.d.ts +0 -0
- /package/dist/{cli → cjs/cli}/search.d.ts +0 -0
- /package/dist/{cli.d.ts → cjs/cli.d.ts} +0 -0
- /package/dist/{cli.js → cjs/cli.js} +0 -0
- /package/dist/{config.d.ts → cjs/config.d.ts} +0 -0
- /package/dist/{config.js → cjs/config.js} +0 -0
- /package/dist/{core → cjs/core}/abstract-embedder.d.ts +0 -0
- /package/dist/{core → cjs/core}/abstract-embedder.js +0 -0
- /package/dist/{core → cjs/core}/actionable-error-messages.d.ts +0 -0
- /package/dist/{core → cjs/core}/actionable-error-messages.js +0 -0
- /package/dist/{core → cjs/core}/adapters.d.ts +0 -0
- /package/dist/{core → cjs/core}/adapters.js +0 -0
- /package/dist/{core → cjs/core}/batch-processing-optimizer.d.ts +0 -0
- /package/dist/{core → cjs/core}/batch-processing-optimizer.js +0 -0
- /package/dist/{core → cjs/core}/chunker.d.ts +0 -0
- /package/dist/{core → cjs/core}/chunker.js +0 -0
- /package/dist/{core → cjs/core}/cli-database-utils.d.ts +0 -0
- /package/dist/{core → cjs/core}/cli-database-utils.js +0 -0
- /package/dist/{core → cjs/core}/config.d.ts +0 -0
- /package/dist/{core → cjs/core}/config.js +0 -0
- /package/dist/{core → cjs/core}/content-errors.d.ts +0 -0
- /package/dist/{core → cjs/core}/content-errors.js +0 -0
- /package/dist/{core → cjs/core}/content-manager.d.ts +0 -0
- /package/dist/{core → cjs/core}/content-manager.js +0 -0
- /package/dist/{core → cjs/core}/content-performance-optimizer.d.ts +0 -0
- /package/dist/{core → cjs/core}/content-performance-optimizer.js +0 -0
- /package/dist/{core → cjs/core}/content-resolver.d.ts +0 -0
- /package/dist/{core → cjs/core}/content-resolver.js +0 -0
- /package/dist/{core → cjs/core}/cross-modal-search.d.ts +0 -0
- /package/dist/{core → cjs/core}/cross-modal-search.js +0 -0
- /package/dist/{core → cjs/core}/database-connection-manager.d.ts +0 -0
- /package/dist/{core → cjs/core}/database-connection-manager.js +0 -0
- /package/dist/{core → cjs/core}/db.d.ts +0 -0
- /package/dist/{core → cjs/core}/db.js +0 -0
- /package/dist/{core → cjs/core}/embedder-factory.d.ts +0 -0
- /package/dist/{core → cjs/core}/embedder-factory.js +0 -0
- /package/dist/{core → cjs/core}/error-handler.d.ts +0 -0
- /package/dist/{core → cjs/core}/error-handler.js +0 -0
- /package/dist/{core → cjs/core}/index.d.ts +0 -0
- /package/dist/{core → cjs/core}/index.js +0 -0
- /package/dist/{core → cjs/core}/interfaces.d.ts +0 -0
- /package/dist/{core → cjs/core}/interfaces.js +0 -0
- /package/dist/{core → cjs/core}/lazy-dependency-loader.d.ts +0 -0
- /package/dist/{core → cjs/core}/lazy-dependency-loader.js +0 -0
- /package/dist/{core → cjs/core}/mode-detection-service.d.ts +0 -0
- /package/dist/{core → cjs/core}/mode-detection-service.js +0 -0
- /package/dist/{core → cjs/core}/mode-model-validator.d.ts +0 -0
- /package/dist/{core → cjs/core}/mode-model-validator.js +0 -0
- /package/dist/{core → cjs/core}/model-registry.d.ts +0 -0
- /package/dist/{core → cjs/core}/model-registry.js +0 -0
- /package/dist/{core → cjs/core}/model-validator.d.ts +0 -0
- /package/dist/{core → cjs/core}/path-manager.d.ts +0 -0
- /package/dist/{core → cjs/core}/path-manager.js +0 -0
- /package/dist/{core → cjs/core}/raglite-paths.d.ts +0 -0
- /package/dist/{core → cjs/core}/raglite-paths.js +0 -0
- /package/dist/{core → cjs/core}/reranking-config.d.ts +0 -0
- /package/dist/{core → cjs/core}/reranking-config.js +0 -0
- /package/dist/{core → cjs/core}/reranking-factory.d.ts +0 -0
- /package/dist/{core → cjs/core}/reranking-factory.js +0 -0
- /package/dist/{core → cjs/core}/reranking-strategies.d.ts +0 -0
- /package/dist/{core → cjs/core}/resource-cleanup.d.ts +0 -0
- /package/dist/{core → cjs/core}/resource-cleanup.js +0 -0
- /package/dist/{core → cjs/core}/resource-manager.d.ts +0 -0
- /package/dist/{core → cjs/core}/resource-manager.js +0 -0
- /package/dist/{core → cjs/core}/search-pipeline.d.ts +0 -0
- /package/dist/{core → cjs/core}/search-pipeline.js +0 -0
- /package/dist/{core → cjs/core}/search.d.ts +0 -0
- /package/dist/{core → cjs/core}/streaming-operations.d.ts +0 -0
- /package/dist/{core → cjs/core}/streaming-operations.js +0 -0
- /package/dist/{core → cjs/core}/types.js +0 -0
- /package/dist/{core → cjs/core}/universal-embedder.d.ts +0 -0
- /package/dist/{core → cjs/core}/universal-embedder.js +0 -0
- /package/dist/{core → cjs/core}/validation-messages.d.ts +0 -0
- /package/dist/{core → cjs/core}/validation-messages.js +0 -0
- /package/dist/{dom-polyfills.d.ts → cjs/dom-polyfills.d.ts} +0 -0
- /package/dist/{dom-polyfills.js → cjs/dom-polyfills.js} +0 -0
- /package/dist/{factories → cjs/factories}/index.d.ts +0 -0
- /package/dist/{factories → cjs/factories}/index.js +0 -0
- /package/dist/{factories → cjs/factories}/ingestion-factory.d.ts +0 -0
- /package/dist/{factories → cjs/factories}/ingestion-factory.js +0 -0
- /package/dist/{factories → cjs/factories}/search-factory.d.ts +0 -0
- /package/dist/{factories → cjs/factories}/search-factory.js +0 -0
- /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
- /package/dist/{index.js → cjs/index.js} +0 -0
- /package/dist/{indexer.d.ts → cjs/indexer.d.ts} +0 -0
- /package/dist/{indexer.js → cjs/indexer.js} +0 -0
- /package/dist/{ingestion.d.ts → cjs/ingestion.d.ts} +0 -0
- /package/dist/{ingestion.js → cjs/ingestion.js} +0 -0
- /package/dist/{mcp-server.d.ts → cjs/mcp-server.d.ts} +0 -0
- /package/dist/{mcp-server.js → cjs/mcp-server.js} +0 -0
- /package/dist/{multimodal → cjs/multimodal}/clip-embedder.d.ts +0 -0
- /package/dist/{multimodal → cjs/multimodal}/index.d.ts +0 -0
- /package/dist/{multimodal → cjs/multimodal}/index.js +0 -0
- /package/dist/{preprocess.d.ts → cjs/preprocess.d.ts} +0 -0
- /package/dist/{preprocess.js → cjs/preprocess.js} +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/index.d.ts +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/index.js +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/mdx.d.ts +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/mdx.js +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/mermaid.d.ts +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/mermaid.js +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/registry.d.ts +0 -0
- /package/dist/{preprocessors → cjs/preprocessors}/registry.js +0 -0
- /package/dist/{run-error-recovery-tests.d.ts → cjs/run-error-recovery-tests.d.ts} +0 -0
- /package/dist/{run-error-recovery-tests.js → cjs/run-error-recovery-tests.js} +0 -0
- /package/dist/{search-standalone.d.ts → cjs/search-standalone.d.ts} +0 -0
- /package/dist/{search-standalone.js → cjs/search-standalone.js} +0 -0
- /package/dist/{search.d.ts → cjs/search.d.ts} +0 -0
- /package/dist/{search.js → cjs/search.js} +0 -0
- /package/dist/{test-utils.d.ts → cjs/test-utils.d.ts} +0 -0
- /package/dist/{test-utils.js → cjs/test-utils.js} +0 -0
- /package/dist/{text → cjs/text}/chunker.d.ts +0 -0
- /package/dist/{text → cjs/text}/chunker.js +0 -0
- /package/dist/{text → cjs/text}/embedder.d.ts +0 -0
- /package/dist/{text → cjs/text}/embedder.js +0 -0
- /package/dist/{text → cjs/text}/index.d.ts +0 -0
- /package/dist/{text → cjs/text}/index.js +0 -0
- /package/dist/{text → cjs/text}/preprocessors/index.d.ts +0 -0
- /package/dist/{text → cjs/text}/preprocessors/index.js +0 -0
- /package/dist/{text → cjs/text}/preprocessors/mdx.d.ts +0 -0
- /package/dist/{text → cjs/text}/preprocessors/mdx.js +0 -0
- /package/dist/{text → cjs/text}/preprocessors/mermaid.d.ts +0 -0
- /package/dist/{text → cjs/text}/preprocessors/mermaid.js +0 -0
- /package/dist/{text → cjs/text}/preprocessors/registry.d.ts +0 -0
- /package/dist/{text → cjs/text}/preprocessors/registry.js +0 -0
- /package/dist/{text → cjs/text}/reranker.d.ts +0 -0
- /package/dist/{text → cjs/text}/reranker.js +0 -0
- /package/dist/{text → cjs/text}/sentence-transformer-embedder.d.ts +0 -0
- /package/dist/{text → cjs/text}/sentence-transformer-embedder.js +0 -0
- /package/dist/{text → cjs/text}/tokenizer.d.ts +0 -0
- /package/dist/{text → cjs/text}/tokenizer.js +0 -0
- /package/dist/{types.d.ts → cjs/types.d.ts} +0 -0
- /package/dist/{types.js → cjs/types.js} +0 -0
- /package/dist/{utils → cjs/utils}/vector-math.d.ts +0 -0
- /package/dist/{utils → cjs/utils}/vector-math.js +0 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Performance Optimizer - Task 9.2 Implementation
|
|
3
|
+
* Provides performance optimizations for content operations including caching,
|
|
4
|
+
* batch processing, and I/O optimizations
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for performance optimizer
|
|
8
|
+
*/
|
|
9
|
+
export interface PerformanceOptimizerConfig {
|
|
10
|
+
hashCacheSize: number;
|
|
11
|
+
hashCacheTTL: number;
|
|
12
|
+
maxConcurrentOperations: number;
|
|
13
|
+
batchSize: number;
|
|
14
|
+
fileBufferSize: number;
|
|
15
|
+
enableAsyncIO: boolean;
|
|
16
|
+
enableMetrics: boolean;
|
|
17
|
+
metricsRetentionTime: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Performance metrics for monitoring
|
|
21
|
+
*/
|
|
22
|
+
export interface PerformanceMetrics {
|
|
23
|
+
operationType: string;
|
|
24
|
+
duration: number;
|
|
25
|
+
bytesProcessed: number;
|
|
26
|
+
cacheHit: boolean;
|
|
27
|
+
timestamp: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Batch operation result
|
|
31
|
+
*/
|
|
32
|
+
export interface BatchOperationResult<T> {
|
|
33
|
+
results: T[];
|
|
34
|
+
totalDuration: number;
|
|
35
|
+
successCount: number;
|
|
36
|
+
errorCount: number;
|
|
37
|
+
averageSpeed: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Content Performance Optimizer class
|
|
41
|
+
*/
|
|
42
|
+
export declare class ContentPerformanceOptimizer {
|
|
43
|
+
private config;
|
|
44
|
+
private hashCache;
|
|
45
|
+
private streamingOps;
|
|
46
|
+
private metrics;
|
|
47
|
+
private metricsCleanupInterval?;
|
|
48
|
+
constructor(config?: Partial<PerformanceOptimizerConfig>);
|
|
49
|
+
/**
|
|
50
|
+
* Optimized hash calculation with caching
|
|
51
|
+
* @param filePath - Path to the file to hash
|
|
52
|
+
* @returns Promise that resolves to hash string
|
|
53
|
+
*/
|
|
54
|
+
calculateFileHashOptimized(filePath: string): Promise<string>;
|
|
55
|
+
/**
|
|
56
|
+
* Optimized buffer hash calculation with caching
|
|
57
|
+
* @param content - Buffer to hash
|
|
58
|
+
* @param cacheKey - Optional cache key for the content
|
|
59
|
+
* @returns Promise that resolves to hash string
|
|
60
|
+
*/
|
|
61
|
+
calculateBufferHashOptimized(content: Buffer, cacheKey?: string): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Optimized batch processing with controlled concurrency and resource management
|
|
64
|
+
* @param items - Array of items to process
|
|
65
|
+
* @param processor - Function to process each item
|
|
66
|
+
* @param options - Batch processing options
|
|
67
|
+
* @returns Promise that resolves to batch operation result
|
|
68
|
+
*/
|
|
69
|
+
processBatchOptimized<T, R>(items: T[], processor: (item: T) => Promise<R>, options?: {
|
|
70
|
+
batchSize?: number;
|
|
71
|
+
maxConcurrency?: number;
|
|
72
|
+
enableMetrics?: boolean;
|
|
73
|
+
}): Promise<BatchOperationResult<R>>;
|
|
74
|
+
/**
|
|
75
|
+
* Optimized file I/O operations with better buffering
|
|
76
|
+
* @param filePath - Path to the file to read
|
|
77
|
+
* @param options - I/O options
|
|
78
|
+
* @returns Promise that resolves to file content
|
|
79
|
+
*/
|
|
80
|
+
readFileOptimized(filePath: string, options?: {
|
|
81
|
+
encoding?: BufferEncoding;
|
|
82
|
+
bufferSize?: number;
|
|
83
|
+
enableCaching?: boolean;
|
|
84
|
+
}): Promise<string | Buffer>;
|
|
85
|
+
/**
|
|
86
|
+
* Gets performance metrics for monitoring and optimization
|
|
87
|
+
* @param operationType - Optional filter by operation type
|
|
88
|
+
* @param timeRange - Optional time range in milliseconds
|
|
89
|
+
* @returns Array of performance metrics
|
|
90
|
+
*/
|
|
91
|
+
getPerformanceMetrics(operationType?: string, timeRange?: number): PerformanceMetrics[];
|
|
92
|
+
/**
|
|
93
|
+
* Gets performance statistics summary
|
|
94
|
+
* @param operationType - Optional filter by operation type
|
|
95
|
+
* @returns Performance statistics
|
|
96
|
+
*/
|
|
97
|
+
getPerformanceStats(operationType?: string): {
|
|
98
|
+
totalOperations: number;
|
|
99
|
+
averageDuration: number;
|
|
100
|
+
totalBytesProcessed: number;
|
|
101
|
+
averageSpeed: number;
|
|
102
|
+
cacheHitRate: number;
|
|
103
|
+
errorRate: number;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Clears the hash cache
|
|
107
|
+
*/
|
|
108
|
+
clearHashCache(): void;
|
|
109
|
+
/**
|
|
110
|
+
* Gets hash cache statistics
|
|
111
|
+
* @returns Cache statistics
|
|
112
|
+
*/
|
|
113
|
+
getHashCacheStats(): {
|
|
114
|
+
size: number;
|
|
115
|
+
maxSize: number;
|
|
116
|
+
hitRate: number;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Cleanup resources
|
|
120
|
+
*/
|
|
121
|
+
cleanup(): void;
|
|
122
|
+
/**
|
|
123
|
+
* Records a performance metric
|
|
124
|
+
* @param metric - Metric to record
|
|
125
|
+
*/
|
|
126
|
+
private recordMetric;
|
|
127
|
+
/**
|
|
128
|
+
* Starts periodic cleanup of old metrics
|
|
129
|
+
*/
|
|
130
|
+
private startMetricsCleanup;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates a ContentPerformanceOptimizer instance with default configuration
|
|
134
|
+
* @param config - Optional configuration overrides
|
|
135
|
+
* @returns ContentPerformanceOptimizer instance
|
|
136
|
+
*/
|
|
137
|
+
export declare function createContentPerformanceOptimizer(config?: Partial<PerformanceOptimizerConfig>): ContentPerformanceOptimizer;
|
|
138
|
+
/**
|
|
139
|
+
* Utility function to format cache hit rate
|
|
140
|
+
* @param hitRate - Hit rate as percentage
|
|
141
|
+
* @returns Formatted string
|
|
142
|
+
*/
|
|
143
|
+
export declare function formatCacheHitRate(hitRate: number): string;
|
|
144
|
+
/**
|
|
145
|
+
* Utility function to format processing speed
|
|
146
|
+
* @param speed - Speed in MB/s
|
|
147
|
+
* @returns Formatted string
|
|
148
|
+
*/
|
|
149
|
+
export declare function formatProcessingSpeed(speed: number): string;
|
|
150
|
+
//# sourceMappingURL=content-performance-optimizer.d.ts.map
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content Performance Optimizer - Task 9.2 Implementation
|
|
3
|
+
* Provides performance optimizations for content operations including caching,
|
|
4
|
+
* batch processing, and I/O optimizations
|
|
5
|
+
*/
|
|
6
|
+
import { LRUCache } from 'lru-cache';
|
|
7
|
+
import { promises as fs } from 'fs';
|
|
8
|
+
import { createHash } from 'crypto';
|
|
9
|
+
import { createStreamingOperations, calculateProcessingSpeed } from './streaming-operations.js';
|
|
10
|
+
/**
|
|
11
|
+
* Default performance optimizer configuration
|
|
12
|
+
*/
|
|
13
|
+
const DEFAULT_PERFORMANCE_CONFIG = {
|
|
14
|
+
hashCacheSize: 1000, // Cache up to 1000 hashes
|
|
15
|
+
hashCacheTTL: 60 * 60 * 1000, // 1 hour TTL
|
|
16
|
+
maxConcurrentOperations: 10,
|
|
17
|
+
batchSize: 50,
|
|
18
|
+
fileBufferSize: 256 * 1024, // 256KB buffer
|
|
19
|
+
enableAsyncIO: true,
|
|
20
|
+
enableMetrics: true,
|
|
21
|
+
metricsRetentionTime: 24 * 60 * 60 * 1000 // 24 hours
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Content Performance Optimizer class
|
|
25
|
+
*/
|
|
26
|
+
export class ContentPerformanceOptimizer {
|
|
27
|
+
config;
|
|
28
|
+
hashCache;
|
|
29
|
+
streamingOps;
|
|
30
|
+
metrics = [];
|
|
31
|
+
metricsCleanupInterval;
|
|
32
|
+
constructor(config = {}) {
|
|
33
|
+
this.config = { ...DEFAULT_PERFORMANCE_CONFIG, ...config };
|
|
34
|
+
// Initialize hash cache
|
|
35
|
+
this.hashCache = new LRUCache({
|
|
36
|
+
max: this.config.hashCacheSize,
|
|
37
|
+
ttl: this.config.hashCacheTTL,
|
|
38
|
+
updateAgeOnGet: true,
|
|
39
|
+
updateAgeOnHas: true
|
|
40
|
+
});
|
|
41
|
+
// Initialize streaming operations with optimized settings
|
|
42
|
+
this.streamingOps = createStreamingOperations({
|
|
43
|
+
chunkSize: this.config.fileBufferSize,
|
|
44
|
+
enableProgress: false,
|
|
45
|
+
enableHashing: true,
|
|
46
|
+
timeout: 300000 // 5 minutes
|
|
47
|
+
});
|
|
48
|
+
// Start metrics cleanup if enabled
|
|
49
|
+
if (this.config.enableMetrics) {
|
|
50
|
+
this.startMetricsCleanup();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Optimized hash calculation with caching
|
|
55
|
+
* @param filePath - Path to the file to hash
|
|
56
|
+
* @returns Promise that resolves to hash string
|
|
57
|
+
*/
|
|
58
|
+
async calculateFileHashOptimized(filePath) {
|
|
59
|
+
const startTime = Date.now();
|
|
60
|
+
let cacheHit = false;
|
|
61
|
+
let bytesProcessed = 0;
|
|
62
|
+
try {
|
|
63
|
+
// Get file stats for cache validation
|
|
64
|
+
const stats = await fs.stat(filePath);
|
|
65
|
+
bytesProcessed = stats.size;
|
|
66
|
+
// Check cache first
|
|
67
|
+
const cacheKey = `${filePath}:${stats.size}:${stats.mtime.getTime()}`;
|
|
68
|
+
const cachedEntry = this.hashCache.get(cacheKey);
|
|
69
|
+
if (cachedEntry &&
|
|
70
|
+
cachedEntry.fileSize === stats.size &&
|
|
71
|
+
cachedEntry.lastModified === stats.mtime.getTime()) {
|
|
72
|
+
cacheHit = true;
|
|
73
|
+
// Record metrics
|
|
74
|
+
if (this.config.enableMetrics) {
|
|
75
|
+
this.recordMetric({
|
|
76
|
+
operationType: 'hash_calculation',
|
|
77
|
+
duration: Date.now() - startTime,
|
|
78
|
+
bytesProcessed,
|
|
79
|
+
cacheHit: true,
|
|
80
|
+
timestamp: Date.now()
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return cachedEntry.hash;
|
|
84
|
+
}
|
|
85
|
+
// Calculate hash using streaming operations
|
|
86
|
+
const hashResult = await this.streamingOps.calculateFileHashStreaming(filePath);
|
|
87
|
+
// Cache the result
|
|
88
|
+
const cacheEntry = {
|
|
89
|
+
hash: hashResult.hash,
|
|
90
|
+
fileSize: stats.size,
|
|
91
|
+
lastModified: stats.mtime.getTime(),
|
|
92
|
+
computedAt: Date.now()
|
|
93
|
+
};
|
|
94
|
+
this.hashCache.set(cacheKey, cacheEntry);
|
|
95
|
+
// Record metrics
|
|
96
|
+
if (this.config.enableMetrics) {
|
|
97
|
+
this.recordMetric({
|
|
98
|
+
operationType: 'hash_calculation',
|
|
99
|
+
duration: Date.now() - startTime,
|
|
100
|
+
bytesProcessed,
|
|
101
|
+
cacheHit: false,
|
|
102
|
+
timestamp: Date.now()
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return hashResult.hash;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
// Record error metric
|
|
109
|
+
if (this.config.enableMetrics) {
|
|
110
|
+
this.recordMetric({
|
|
111
|
+
operationType: 'hash_calculation_error',
|
|
112
|
+
duration: Date.now() - startTime,
|
|
113
|
+
bytesProcessed,
|
|
114
|
+
cacheHit,
|
|
115
|
+
timestamp: Date.now()
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
throw new Error(`Optimized hash calculation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Optimized buffer hash calculation with caching
|
|
123
|
+
* @param content - Buffer to hash
|
|
124
|
+
* @param cacheKey - Optional cache key for the content
|
|
125
|
+
* @returns Promise that resolves to hash string
|
|
126
|
+
*/
|
|
127
|
+
async calculateBufferHashOptimized(content, cacheKey) {
|
|
128
|
+
const startTime = Date.now();
|
|
129
|
+
let cacheHit = false;
|
|
130
|
+
const bytesProcessed = content.length;
|
|
131
|
+
try {
|
|
132
|
+
// Check cache if key provided
|
|
133
|
+
if (cacheKey) {
|
|
134
|
+
const cachedEntry = this.hashCache.get(cacheKey);
|
|
135
|
+
if (cachedEntry && cachedEntry.fileSize === content.length) {
|
|
136
|
+
cacheHit = true;
|
|
137
|
+
// Record metrics
|
|
138
|
+
if (this.config.enableMetrics) {
|
|
139
|
+
this.recordMetric({
|
|
140
|
+
operationType: 'buffer_hash_calculation',
|
|
141
|
+
duration: Date.now() - startTime,
|
|
142
|
+
bytesProcessed,
|
|
143
|
+
cacheHit: true,
|
|
144
|
+
timestamp: Date.now()
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return cachedEntry.hash;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// Calculate hash using streaming operations for large buffers
|
|
151
|
+
let hash;
|
|
152
|
+
if (content.length > 1024 * 1024) { // Use streaming for buffers > 1MB
|
|
153
|
+
const hashResult = await this.streamingOps.calculateBufferHashStreaming(content);
|
|
154
|
+
hash = hashResult.hash;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// Use direct calculation for small buffers
|
|
158
|
+
hash = createHash('sha256').update(content).digest('hex');
|
|
159
|
+
}
|
|
160
|
+
// Cache the result if key provided
|
|
161
|
+
if (cacheKey) {
|
|
162
|
+
const cacheEntry = {
|
|
163
|
+
hash,
|
|
164
|
+
fileSize: content.length,
|
|
165
|
+
lastModified: Date.now(),
|
|
166
|
+
computedAt: Date.now()
|
|
167
|
+
};
|
|
168
|
+
this.hashCache.set(cacheKey, cacheEntry);
|
|
169
|
+
}
|
|
170
|
+
// Record metrics
|
|
171
|
+
if (this.config.enableMetrics) {
|
|
172
|
+
this.recordMetric({
|
|
173
|
+
operationType: 'buffer_hash_calculation',
|
|
174
|
+
duration: Date.now() - startTime,
|
|
175
|
+
bytesProcessed,
|
|
176
|
+
cacheHit: false,
|
|
177
|
+
timestamp: Date.now()
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return hash;
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
// Record error metric
|
|
184
|
+
if (this.config.enableMetrics) {
|
|
185
|
+
this.recordMetric({
|
|
186
|
+
operationType: 'buffer_hash_calculation_error',
|
|
187
|
+
duration: Date.now() - startTime,
|
|
188
|
+
bytesProcessed,
|
|
189
|
+
cacheHit,
|
|
190
|
+
timestamp: Date.now()
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
throw new Error(`Optimized buffer hash calculation failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Optimized batch processing with controlled concurrency and resource management
|
|
198
|
+
* @param items - Array of items to process
|
|
199
|
+
* @param processor - Function to process each item
|
|
200
|
+
* @param options - Batch processing options
|
|
201
|
+
* @returns Promise that resolves to batch operation result
|
|
202
|
+
*/
|
|
203
|
+
async processBatchOptimized(items, processor, options = {}) {
|
|
204
|
+
const startTime = Date.now();
|
|
205
|
+
const batchSize = options.batchSize || this.config.batchSize;
|
|
206
|
+
const maxConcurrency = options.maxConcurrency || this.config.maxConcurrentOperations;
|
|
207
|
+
const enableMetrics = options.enableMetrics !== undefined ? options.enableMetrics : this.config.enableMetrics;
|
|
208
|
+
const results = [];
|
|
209
|
+
let successCount = 0;
|
|
210
|
+
let errorCount = 0;
|
|
211
|
+
let totalBytesProcessed = 0;
|
|
212
|
+
try {
|
|
213
|
+
// Split items into batches
|
|
214
|
+
const batches = [];
|
|
215
|
+
for (let i = 0; i < items.length; i += batchSize) {
|
|
216
|
+
batches.push(items.slice(i, i + batchSize));
|
|
217
|
+
}
|
|
218
|
+
// Process batches with controlled concurrency
|
|
219
|
+
for (const batch of batches) {
|
|
220
|
+
// Create semaphore for concurrency control
|
|
221
|
+
const semaphore = new Array(Math.min(maxConcurrency, batch.length)).fill(null);
|
|
222
|
+
const batchPromises = batch.map(async (item, index) => {
|
|
223
|
+
// Wait for semaphore slot
|
|
224
|
+
await new Promise(resolve => {
|
|
225
|
+
const checkSlot = () => {
|
|
226
|
+
const slotIndex = index % semaphore.length;
|
|
227
|
+
if (semaphore[slotIndex] === null) {
|
|
228
|
+
semaphore[slotIndex] = item;
|
|
229
|
+
resolve(undefined);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
setTimeout(checkSlot, 1);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
checkSlot();
|
|
236
|
+
});
|
|
237
|
+
try {
|
|
238
|
+
const result = await processor(item);
|
|
239
|
+
successCount++;
|
|
240
|
+
// Estimate bytes processed (rough approximation)
|
|
241
|
+
if (typeof item === 'string') {
|
|
242
|
+
totalBytesProcessed += Buffer.byteLength(item);
|
|
243
|
+
}
|
|
244
|
+
else if (Buffer.isBuffer(item)) {
|
|
245
|
+
totalBytesProcessed += item.length;
|
|
246
|
+
}
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
catch (error) {
|
|
250
|
+
errorCount++;
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
// Release semaphore slot
|
|
255
|
+
const slotIndex = index % semaphore.length;
|
|
256
|
+
semaphore[slotIndex] = null;
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
const batchResults = await Promise.allSettled(batchPromises);
|
|
260
|
+
// Collect successful results
|
|
261
|
+
for (const result of batchResults) {
|
|
262
|
+
if (result.status === 'fulfilled') {
|
|
263
|
+
results.push(result.value);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const totalDuration = Date.now() - startTime;
|
|
268
|
+
const averageSpeed = totalBytesProcessed > 0 ?
|
|
269
|
+
calculateProcessingSpeed(totalBytesProcessed, totalDuration) : 0;
|
|
270
|
+
// Record batch metrics
|
|
271
|
+
if (enableMetrics) {
|
|
272
|
+
this.recordMetric({
|
|
273
|
+
operationType: 'batch_processing',
|
|
274
|
+
duration: totalDuration,
|
|
275
|
+
bytesProcessed: totalBytesProcessed,
|
|
276
|
+
cacheHit: false,
|
|
277
|
+
timestamp: Date.now()
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
results,
|
|
282
|
+
totalDuration,
|
|
283
|
+
successCount,
|
|
284
|
+
errorCount,
|
|
285
|
+
averageSpeed
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
const totalDuration = Date.now() - startTime;
|
|
290
|
+
// Record error metric
|
|
291
|
+
if (enableMetrics) {
|
|
292
|
+
this.recordMetric({
|
|
293
|
+
operationType: 'batch_processing_error',
|
|
294
|
+
duration: totalDuration,
|
|
295
|
+
bytesProcessed: totalBytesProcessed,
|
|
296
|
+
cacheHit: false,
|
|
297
|
+
timestamp: Date.now()
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
throw new Error(`Batch processing failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Optimized file I/O operations with better buffering
|
|
305
|
+
* @param filePath - Path to the file to read
|
|
306
|
+
* @param options - I/O options
|
|
307
|
+
* @returns Promise that resolves to file content
|
|
308
|
+
*/
|
|
309
|
+
async readFileOptimized(filePath, options = {}) {
|
|
310
|
+
const startTime = Date.now();
|
|
311
|
+
const bufferSize = options.bufferSize || this.config.fileBufferSize;
|
|
312
|
+
try {
|
|
313
|
+
// Get file stats
|
|
314
|
+
const stats = await fs.stat(filePath);
|
|
315
|
+
// For small files, use direct read
|
|
316
|
+
if (stats.size <= bufferSize) {
|
|
317
|
+
const content = await fs.readFile(filePath, options.encoding);
|
|
318
|
+
// Record metrics
|
|
319
|
+
if (this.config.enableMetrics) {
|
|
320
|
+
this.recordMetric({
|
|
321
|
+
operationType: 'file_read_direct',
|
|
322
|
+
duration: Date.now() - startTime,
|
|
323
|
+
bytesProcessed: stats.size,
|
|
324
|
+
cacheHit: false,
|
|
325
|
+
timestamp: Date.now()
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return content;
|
|
329
|
+
}
|
|
330
|
+
// For large files, use streaming read with optimized buffer size
|
|
331
|
+
const chunks = [];
|
|
332
|
+
const readStream = await fs.open(filePath, 'r');
|
|
333
|
+
try {
|
|
334
|
+
let position = 0;
|
|
335
|
+
while (position < stats.size) {
|
|
336
|
+
const chunkSize = Math.min(bufferSize, stats.size - position);
|
|
337
|
+
const buffer = Buffer.alloc(chunkSize);
|
|
338
|
+
const { bytesRead } = await readStream.read(buffer, 0, chunkSize, position);
|
|
339
|
+
if (bytesRead === 0)
|
|
340
|
+
break;
|
|
341
|
+
chunks.push(buffer.subarray(0, bytesRead));
|
|
342
|
+
position += bytesRead;
|
|
343
|
+
}
|
|
344
|
+
const content = Buffer.concat(chunks);
|
|
345
|
+
// Record metrics
|
|
346
|
+
if (this.config.enableMetrics) {
|
|
347
|
+
this.recordMetric({
|
|
348
|
+
operationType: 'file_read_streaming',
|
|
349
|
+
duration: Date.now() - startTime,
|
|
350
|
+
bytesProcessed: stats.size,
|
|
351
|
+
cacheHit: false,
|
|
352
|
+
timestamp: Date.now()
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
return options.encoding ? content.toString(options.encoding) : content;
|
|
356
|
+
}
|
|
357
|
+
finally {
|
|
358
|
+
await readStream.close();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
catch (error) {
|
|
362
|
+
// Record error metric
|
|
363
|
+
if (this.config.enableMetrics) {
|
|
364
|
+
this.recordMetric({
|
|
365
|
+
operationType: 'file_read_error',
|
|
366
|
+
duration: Date.now() - startTime,
|
|
367
|
+
bytesProcessed: 0,
|
|
368
|
+
cacheHit: false,
|
|
369
|
+
timestamp: Date.now()
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
throw new Error(`Optimized file read failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Gets performance metrics for monitoring and optimization
|
|
377
|
+
* @param operationType - Optional filter by operation type
|
|
378
|
+
* @param timeRange - Optional time range in milliseconds
|
|
379
|
+
* @returns Array of performance metrics
|
|
380
|
+
*/
|
|
381
|
+
getPerformanceMetrics(operationType, timeRange) {
|
|
382
|
+
if (!this.config.enableMetrics) {
|
|
383
|
+
return [];
|
|
384
|
+
}
|
|
385
|
+
let filteredMetrics = this.metrics;
|
|
386
|
+
// Filter by operation type
|
|
387
|
+
if (operationType) {
|
|
388
|
+
filteredMetrics = filteredMetrics.filter(m => m.operationType === operationType);
|
|
389
|
+
}
|
|
390
|
+
// Filter by time range
|
|
391
|
+
if (timeRange) {
|
|
392
|
+
const cutoffTime = Date.now() - timeRange;
|
|
393
|
+
filteredMetrics = filteredMetrics.filter(m => m.timestamp >= cutoffTime);
|
|
394
|
+
}
|
|
395
|
+
return filteredMetrics;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Gets performance statistics summary
|
|
399
|
+
* @param operationType - Optional filter by operation type
|
|
400
|
+
* @returns Performance statistics
|
|
401
|
+
*/
|
|
402
|
+
getPerformanceStats(operationType) {
|
|
403
|
+
const metrics = this.getPerformanceMetrics(operationType);
|
|
404
|
+
if (metrics.length === 0) {
|
|
405
|
+
return {
|
|
406
|
+
totalOperations: 0,
|
|
407
|
+
averageDuration: 0,
|
|
408
|
+
totalBytesProcessed: 0,
|
|
409
|
+
averageSpeed: 0,
|
|
410
|
+
cacheHitRate: 0,
|
|
411
|
+
errorRate: 0
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
const totalDuration = metrics.reduce((sum, m) => sum + m.duration, 0);
|
|
415
|
+
const totalBytes = metrics.reduce((sum, m) => sum + m.bytesProcessed, 0);
|
|
416
|
+
const cacheHits = metrics.filter(m => m.cacheHit).length;
|
|
417
|
+
const errors = metrics.filter(m => m.operationType.includes('error')).length;
|
|
418
|
+
return {
|
|
419
|
+
totalOperations: metrics.length,
|
|
420
|
+
averageDuration: totalDuration / metrics.length,
|
|
421
|
+
totalBytesProcessed: totalBytes,
|
|
422
|
+
averageSpeed: totalBytes > 0 ? calculateProcessingSpeed(totalBytes, totalDuration) : 0,
|
|
423
|
+
cacheHitRate: (cacheHits / metrics.length) * 100,
|
|
424
|
+
errorRate: (errors / metrics.length) * 100
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Clears the hash cache
|
|
429
|
+
*/
|
|
430
|
+
clearHashCache() {
|
|
431
|
+
this.hashCache.clear();
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Gets hash cache statistics
|
|
435
|
+
* @returns Cache statistics
|
|
436
|
+
*/
|
|
437
|
+
getHashCacheStats() {
|
|
438
|
+
const metrics = this.getPerformanceMetrics();
|
|
439
|
+
const hashMetrics = metrics.filter(m => m.operationType === 'hash_calculation' ||
|
|
440
|
+
m.operationType === 'buffer_hash_calculation');
|
|
441
|
+
const cacheHits = hashMetrics.filter(m => m.cacheHit).length;
|
|
442
|
+
const hitRate = hashMetrics.length > 0 ? (cacheHits / hashMetrics.length) * 100 : 0;
|
|
443
|
+
return {
|
|
444
|
+
size: this.hashCache.size,
|
|
445
|
+
maxSize: this.config.hashCacheSize,
|
|
446
|
+
hitRate
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Cleanup resources
|
|
451
|
+
*/
|
|
452
|
+
cleanup() {
|
|
453
|
+
if (this.metricsCleanupInterval) {
|
|
454
|
+
clearInterval(this.metricsCleanupInterval);
|
|
455
|
+
this.metricsCleanupInterval = undefined;
|
|
456
|
+
}
|
|
457
|
+
this.hashCache.clear();
|
|
458
|
+
this.metrics = [];
|
|
459
|
+
}
|
|
460
|
+
// =============================================================================
|
|
461
|
+
// PRIVATE METHODS
|
|
462
|
+
// =============================================================================
|
|
463
|
+
/**
|
|
464
|
+
* Records a performance metric
|
|
465
|
+
* @param metric - Metric to record
|
|
466
|
+
*/
|
|
467
|
+
recordMetric(metric) {
|
|
468
|
+
if (!this.config.enableMetrics) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
this.metrics.push(metric);
|
|
472
|
+
// Keep metrics array size manageable
|
|
473
|
+
if (this.metrics.length > 10000) {
|
|
474
|
+
this.metrics = this.metrics.slice(-5000); // Keep last 5000 metrics
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Starts periodic cleanup of old metrics
|
|
479
|
+
*/
|
|
480
|
+
startMetricsCleanup() {
|
|
481
|
+
this.metricsCleanupInterval = setInterval(() => {
|
|
482
|
+
const cutoffTime = Date.now() - this.config.metricsRetentionTime;
|
|
483
|
+
this.metrics = this.metrics.filter(m => m.timestamp >= cutoffTime);
|
|
484
|
+
}, 60000); // Cleanup every minute
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Creates a ContentPerformanceOptimizer instance with default configuration
|
|
489
|
+
* @param config - Optional configuration overrides
|
|
490
|
+
* @returns ContentPerformanceOptimizer instance
|
|
491
|
+
*/
|
|
492
|
+
export function createContentPerformanceOptimizer(config) {
|
|
493
|
+
return new ContentPerformanceOptimizer(config);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Utility function to format cache hit rate
|
|
497
|
+
* @param hitRate - Hit rate as percentage
|
|
498
|
+
* @returns Formatted string
|
|
499
|
+
*/
|
|
500
|
+
export function formatCacheHitRate(hitRate) {
|
|
501
|
+
return `${hitRate.toFixed(1)}%`;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Utility function to format processing speed
|
|
505
|
+
* @param speed - Speed in MB/s
|
|
506
|
+
* @returns Formatted string
|
|
507
|
+
*/
|
|
508
|
+
export function formatProcessingSpeed(speed) {
|
|
509
|
+
if (speed < 1) {
|
|
510
|
+
return `${(speed * 1024).toFixed(1)} KB/s`;
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
return `${speed.toFixed(1)} MB/s`;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
//# sourceMappingURL=content-performance-optimizer.js.map
|