i18ntk
Version:
š The fastest i18n toolkit with 97% performance boost! Zero-dependency, enterprise-grade internationalization for React, Vue, Angular, Python, Java, PHP & more. Features PIN protection, auto framework detection, 7+ UI languages, and comprehensive transla
246 lines (218 loc) ⢠8.14 kB
JavaScript
/**
* Ultra-Performance Optimizer
* Advanced performance tuning utilities for i18n toolkit
* Targets sub-40ms processing for 200k keys with <10MB memory
*/
const os = require('os');
const fs = require('fs');
const path = require('path');
class UltraPerformanceOptimizer {
constructor() {
this.cpuCores = os.cpus().length;
this.totalMemory = os.totalmem();
this.availableMemory = this.totalMemory * 0.8; // Use 80% of system memory
this.optimalSettings = this.calculateOptimalSettings();
}
/**
* Calculate optimal settings based on system resources
* @returns {Object} Optimized configuration
*/
calculateOptimalSettings() {
const memoryPerKey = 50; // bytes per key (very aggressive)
const maxKeysInMemory = Math.floor(this.availableMemory / memoryPerKey);
return {
batchSize: Math.min(2000, Math.floor(maxKeysInMemory / 100)),
concurrency: Math.min(this.cpuCores * 2, 32),
memoryLimit: Math.floor(this.availableMemory / 1024 / 1024 * 0.7),
bufferSize: 64 * 1024,
cacheStrategy: 'lru',
compression: 'brotli',
streaming: true,
parallelProcessing: true,
aggressiveGC: true,
validateOnLoad: false,
validateOnSave: false,
skipBackup: true,
minimalLogging: true
};
}
/**
* Generate ultra-performance configuration
* @returns {Object} Ultra-performance config
*/
generateUltraConfig() {
return {
processing: {
mode: 'ultra-extreme',
batchSize: this.optimalSettings.batchSize,
concurrency: this.optimalSettings.concurrency,
timeout: 5000,
retryAttempts: 0,
retryDelay: 0,
maxFileSize: 1024 * 1024, // 1MB max files
validateOnSave: false,
validateOnLoad: false,
cacheEnabled: true,
cacheTTL: 300000, // 5 minutes
autoBackup: false,
fileFilter: "**/*.json",
memoryLimit: `${this.optimalSettings.memoryLimit}MB`,
garbageCollection: true,
gcInterval: 500
},
optimization: {
memoryLimit: `${this.optimalSettings.memoryLimit}MB`,
localeOptimizer: true,
compression: this.optimalSettings.compression,
parallelProcessing: this.optimalSettings.parallelProcessing,
streaming: this.optimalSettings.streaming,
bufferSize: this.optimalSettings.bufferSize,
aggressiveGC: this.optimalSettings.aggressiveGC,
minimalLogging: this.optimalSettings.minimalLogging
},
filesystem: {
concurrentReads: this.optimalSettings.concurrency,
bufferSize: this.optimalSettings.bufferSize,
cacheDirectory: '.cache-ultra',
tempDirectory: '.tmp-ultra',
cleanupInterval: 30000,
compression: this.optimalSettings.compression
}
};
}
/**
* Apply memory-efficient processing strategies
* @param {Array} keys - Translation keys to process
* @param {Function} processor - Processing function
* @returns {Promise<Array>} Processed results
*/
async processWithMemoryEfficiency(keys, processor) {
const batchSize = this.optimalSettings.batchSize;
const results = [];
for (let i = 0; i < keys.length; i += batchSize) {
const batch = keys.slice(i, i + batchSize);
const batchResults = await this.processBatch(batch, processor);
results.push(...batchResults);
// Aggressive memory management
if (global.gc && i % (batchSize * 10) === 0) {
global.gc();
}
}
return results;
}
/**
* Process a single batch with parallel execution
* @param {Array} batch - Batch of keys
* @param {Function} processor - Processing function
* @returns {Promise<Array>} Batch results
*/
async processBatch(batch, processor) {
const chunkSize = Math.ceil(batch.length / this.optimalSettings.concurrency);
const chunks = [];
for (let i = 0; i < batch.length; i += chunkSize) {
chunks.push(batch.slice(i, i + chunkSize));
}
const promises = chunks.map(chunk => this.processChunk(chunk, processor));
const results = await Promise.all(promises);
return results.flat();
}
/**
* Process a chunk of keys
* @param {Array} chunk - Chunk of keys
* @param {Function} processor - Processing function
* @returns {Promise<Array>} Chunk results
*/
async processChunk(chunk, processor) {
return chunk.map(processor);
}
/**
* Generate performance report
* @param {Object} metrics - Performance metrics
* @returns {Object} Formatted report
*/
generateReport(metrics) {
return {
timestamp: new Date().toISOString(),
system: {
cpuCores: this.cpuCores,
totalMemory: `${Math.floor(this.totalMemory / 1024 / 1024)}MB`,
platform: os.platform(),
nodeVersion: process.version
},
optimization: {
batchSize: this.optimalSettings.batchSize,
concurrency: this.optimalSettings.concurrency,
memoryLimit: this.optimalSettings.memoryLimit,
compression: this.optimalSettings.compression
},
performance: {
processingTime: metrics.processingTime,
memoryUsage: metrics.memoryUsage,
throughput: metrics.throughput,
efficiency: metrics.efficiency
},
recommendations: this.generateRecommendations(metrics)
};
}
/**
* Generate performance recommendations
* @param {Object} metrics - Performance metrics
* @returns {Array} Recommendations
*/
generateRecommendations(metrics) {
const recommendations = [];
if (metrics.processingTime > 40) {
recommendations.push('Consider reducing batch size for better memory locality');
}
if (metrics.memoryUsage > 15) {
recommendations.push('Enable more aggressive garbage collection');
}
if (metrics.throughput < 4000000) {
recommendations.push('Increase concurrency if CPU cores available');
}
return recommendations;
}
/**
* Benchmark current system capabilities
* @returns {Promise<Object>} Benchmark results
*/
async benchmarkSystem() {
const testKeys = Array.from({ length: 10000 }, (_, i) => `key_${i}`);
const startTime = process.hrtime.bigint();
const startMemory = process.memoryUsage();
const results = await this.processWithMemoryEfficiency(testKeys, key => ({
key,
processed: true,
timestamp: Date.now()
}));
const endTime = process.hrtime.bigint();
const endMemory = process.memoryUsage();
const processingTime = Number(endTime - startTime) / 1000000; // Convert to ms
const memoryDelta = (endMemory.heapUsed - startMemory.heapUsed) / 1024 / 1024;
const throughput = testKeys.length / (processingTime / 1000);
return {
processingTime,
memoryUsage: Math.max(0, memoryDelta),
throughput,
efficiency: testKeys.length / processingTime * 1000
};
}
}
module.exports = UltraPerformanceOptimizer;
// CLI usage
if (require.main === module) {
const optimizer = new UltraPerformanceOptimizer();
optimizer.benchmarkSystem().then(metrics => {
const report = optimizer.generateReport(metrics);
console.log('š Ultra-Performance Analysis');
console.log('================================');
console.log(`Processing Time: ${metrics.processingTime.toFixed(2)}ms`);
console.log(`Memory Usage: ${metrics.memoryUsage.toFixed(2)}MB`);
console.log(`Throughput: ${(metrics.throughput / 1000000).toFixed(2)}M keys/sec`);
console.log(`Efficiency: ${metrics.efficiency.toFixed(0)} keys/ms`);
console.log('\nāļø Optimal Settings:');
console.log(JSON.stringify(optimizer.optimalSettings, null, 2));
console.log('\nš Recommendations:');
report.recommendations.forEach(rec => console.log(`- ${rec}`));
});
}