-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_generator.js
More file actions
301 lines (258 loc) · 10.5 KB
/
test_generator.js
File metadata and controls
301 lines (258 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const fs = require('fs');
const path = require('path');
const GCodeGenerator = require('./generator.js');
const OrcaIntegration = require('./orca_integration');
class TestGenerator {
constructor() {
this.testResultsPath = path.join(__dirname, 'test_results');
this.settingsPath = path.join(__dirname, 'settings.json');
this.results = [];
}
// Очистка папки результатов
clearTestResults() {
if (fs.existsSync(this.testResultsPath)) {
const files = fs.readdirSync(this.testResultsPath);
files.forEach(file => {
fs.unlinkSync(path.join(this.testResultsPath, file));
});
console.log('Папка test_results очищена');
} else {
fs.mkdirSync(this.testResultsPath, { recursive: true });
console.log('Создана папка test_results');
}
}
// Генерация PA значений
generatePAValues(startPA, endPA, stepPA) {
const values = [];
for (let value = startPA; value <= endPA; value += stepPA) {
values.push(parseFloat(value.toFixed(3)));
}
return values;
}
// Безопасное имя файла
sanitizeFilename(name) {
return name.replace(/[<>:"/\\|?*]/g, '_').replace(/\s+/g, '_');
}
// Получение пути к слайсеру
getSlicerPath(slicer) {
const appData = process.env.APPDATA;
const slicerPaths = {
qidi: path.join(appData, 'QIDISlicer'),
prusa: path.join(appData, 'PrusaSlicer'),
orca: path.join(__dirname, 'ini_examples', 'orcaslicer')
};
return slicerPaths[slicer];
}
// Тестирование одной конфигурации
async testConfiguration(slicer, printerName, config) {
const testName = `${slicer}-${this.sanitizeFilename(printerName)}-${this.sanitizeFilename(config.filament)}-${this.sanitizeFilename(config.print)}`;
console.log(`\nТестирование: ${testName}`);
try {
const generator = new GCodeGenerator();
const paValues = this.generatePAValues(
config.paSettings.startPA,
config.paSettings.endPA,
config.paSettings.stepPA
);
let gcode;
const slicerPath = this.getSlicerPath(slicer);
// Извлекаем реальное имя принтера из физического принтера
let actualPrinterName = printerName;
if (printerName.includes('*')) {
// Для PrusaSlicer формат: физический_принтер*принтер
actualPrinterName = printerName.split('*')[1];
}
if (slicer === 'orca') {
// Для Orca используем специальную логику
gcode = generator.generate(
slicerPath,
actualPrinterName,
config.filament,
config.print,
paValues,
true // isOrca = true
);
} else {
// Для других слайсеров обычная логика
gcode = generator.generate(
slicerPath,
actualPrinterName,
config.filament,
config.print,
paValues,
false // isOrca = false
);
}
// Анализируем пропущенные плейсхолдеры
const placeholderAnalysis = this.analyzePlaceholders(gcode);
// Сохраняем результат
const filename = `${testName}.gcode`;
const filepath = path.join(this.testResultsPath, filename);
fs.writeFileSync(filepath, gcode, 'utf8');
this.results.push({
slicer,
printer: printerName,
filament: config.filament,
print: config.print,
paCount: paValues.length,
paRange: `${paValues[0]}-${paValues[paValues.length - 1]}`,
status: 'SUCCESS',
filename,
size: gcode.length,
placeholders: placeholderAnalysis
});
const placeholderWarning = placeholderAnalysis.unresolved.length > 0 ?
` ⚠️ ${placeholderAnalysis.unresolved.length} неразрешенных плейсхолдеров` : '';
console.log(`✓ Успешно: ${filename} (${gcode.length} символов, ${paValues.length} PA значений)${placeholderWarning}`);
} catch (error) {
console.error(`✗ Ошибка: ${error.message}`);
this.results.push({
slicer,
printer: printerName,
filament: config.filament,
print: config.print,
status: 'ERROR',
error: error.message,
filename: null
});
}
}
// Анализ плейсхолдеров в G-code
analyzePlaceholders(gcode) {
const lines = gcode.split('\n');
const unresolved = [];
const resolved = [];
// Паттерны для поиска плейсхолдеров
const patterns = [
/\{[^}]+\}/g, // {placeholder}
/\[[^\]]+\]/g // [placeholder] для Klipper
];
lines.forEach((line, lineNum) => {
patterns.forEach(pattern => {
const matches = line.match(pattern);
if (matches) {
matches.forEach(match => {
// Исключаем комментарии и валидные G-code команды
if (!line.trim().startsWith(';') &&
!match.match(/^\{[0-9.]+\}$/) && // числовые значения
!match.match(/^\[[XYZ][0-9.]+\]$/)) { // координаты
unresolved.push({
placeholder: match,
line: lineNum + 1,
context: line.trim()
});
} else {
resolved.push(match);
}
});
}
});
});
return {
total: unresolved.length + resolved.length,
resolved: resolved.length,
unresolved: unresolved
};
}
// Анализ результатов
analyzeResults() {
console.log('\n=== АНАЛИЗ РЕЗУЛЬТАТОВ ===');
const total = this.results.length;
const successful = this.results.filter(r => r.status === 'SUCCESS').length;
const failed = this.results.filter(r => r.status === 'ERROR').length;
console.log(`Всего тестов: ${total}`);
console.log(`Успешных: ${successful}`);
console.log(`Ошибок: ${failed}`);
console.log(`Процент успеха: ${((successful / total) * 100).toFixed(1)}%`);
// Группировка по слайсерам
const bySlicers = {};
this.results.forEach(r => {
if (!bySlicers[r.slicer]) bySlicers[r.slicer] = { success: 0, error: 0 };
bySlicers[r.slicer][r.status === 'SUCCESS' ? 'success' : 'error']++;
});
console.log('\nПо слайсерам:');
Object.entries(bySlicers).forEach(([slicer, stats]) => {
console.log(` ${slicer}: ${stats.success} успешных, ${stats.error} ошибок`);
});
// Ошибки
const errors = this.results.filter(r => r.status === 'ERROR');
if (errors.length > 0) {
console.log('\nОШИБКИ:');
errors.forEach(r => {
console.log(` ${r.slicer}/${r.printer}: ${r.error}`);
});
}
// Статистика по размерам файлов
const successful_results = this.results.filter(r => r.status === 'SUCCESS');
if (successful_results.length > 0) {
const sizes = successful_results.map(r => r.size);
const avgSize = sizes.reduce((a, b) => a + b, 0) / sizes.length;
const minSize = Math.min(...sizes);
const maxSize = Math.max(...sizes);
console.log('\nСтатистика размеров файлов:');
console.log(` Средний: ${Math.round(avgSize)} символов`);
console.log(` Минимальный: ${minSize} символов`);
console.log(` Максимальный: ${maxSize} символов`);
// Анализ плейсхолдеров
const withPlaceholders = successful_results.filter(r => r.placeholders && r.placeholders.unresolved.length > 0);
if (withPlaceholders.length > 0) {
console.log('\n⚠️ ФАЙЛЫ С НЕРАЗРЕШЕННЫМИ ПЛЕЙСХОЛДЕРАМИ:');
withPlaceholders.forEach(r => {
console.log(` ${r.filename}: ${r.placeholders.unresolved.length} плейсхолдеров`);
r.placeholders.unresolved.slice(0, 3).forEach(p => {
console.log(` - ${p.placeholder} (строка ${p.line})`);
});
if (r.placeholders.unresolved.length > 3) {
console.log(` ... и еще ${r.placeholders.unresolved.length - 3}`);
}
});
} else {
console.log('\n✓ Все плейсхолдеры успешно разрешены');
}
}
// Сохраняем отчет
const reportPath = path.join(this.testResultsPath, 'test_report.json');
fs.writeFileSync(reportPath, JSON.stringify({
summary: { total, successful, failed, successRate: (successful / total) * 100 },
bySlicers,
results: this.results
}, null, 2), 'utf8');
console.log(`\nОтчет сохранен: ${reportPath}`);
}
// Основной метод тестирования
async runTests() {
console.log('=== ЗАПУСК ТЕСТИРОВАНИЯ ГЕНЕРАТОРА G-CODE ===');
// Очищаем папку результатов
this.clearTestResults();
// Загружаем настройки
if (!fs.existsSync(this.settingsPath)) {
console.error('Файл settings.json не найден!');
return;
}
const settings = JSON.parse(fs.readFileSync(this.settingsPath, 'utf8'));
if (!settings.slicers) {
console.error('В settings.json отсутствует секция slicers!');
return;
}
// Проходим по всем слайсерам и принтерам
for (const [slicer, slicerData] of Object.entries(settings.slicers)) {
console.log(`\n--- Тестирование слайсера: ${slicer} ---`);
if (!slicerData.printers) {
console.log(`Нет принтеров для слайсера ${slicer}`);
continue;
}
for (const [printerName, config] of Object.entries(slicerData.printers)) {
await this.testConfiguration(slicer, printerName, config);
}
}
// Анализируем результаты
this.analyzeResults();
console.log('\n=== ТЕСТИРОВАНИЕ ЗАВЕРШЕНО ===');
}
}
// Запуск тестирования
if (require.main === module) {
const tester = new TestGenerator();
tester.runTests().catch(console.error);
}
module.exports = TestGenerator;