whitphx HF Staff commited on
Commit
a2ce515
·
1 Parent(s): 4f0e494

fix for json error

Browse files
Files changed (1) hide show
  1. bench/src/server/queue.ts +93 -24
bench/src/server/queue.ts CHANGED
@@ -29,6 +29,53 @@ export class BenchmarkQueue extends EventEmitter {
29
  private queue: QueuedBenchmark[] = [];
30
  private isProcessing = false;
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  addBenchmark(request: BenchmarkRequest): void {
33
  const queued: QueuedBenchmark = {
34
  ...request,
@@ -139,11 +186,21 @@ export class BenchmarkQueue extends EventEmitter {
139
  }, BENCHMARK_TIMEOUT);
140
 
141
  proc.stdout.on("data", (data) => {
142
- stdout += data.toString();
 
 
 
 
 
143
  });
144
 
145
  proc.stderr.on("data", (data) => {
146
- stderr += data.toString();
 
 
 
 
 
147
  });
148
 
149
  // Error handler for spawn failures
@@ -170,17 +227,18 @@ export class BenchmarkQueue extends EventEmitter {
170
  return;
171
  }
172
 
173
- // Extract JSON from stdout (last JSON object)
174
- const jsonMatch = stdout.match(/\{[\s\S]*"platform"[\s\S]*\}/);
175
- if (jsonMatch) {
176
- try {
177
- const result = JSON.parse(jsonMatch[0]);
178
- resolve(result);
179
- } catch (e) {
180
- reject(new Error(`Failed to parse benchmark result: ${e}`));
181
- }
182
  } else {
183
- reject(new Error("No benchmark result found in output"));
 
 
 
 
 
 
184
  }
185
  }
186
  });
@@ -224,11 +282,21 @@ export class BenchmarkQueue extends EventEmitter {
224
  }, BENCHMARK_TIMEOUT);
225
 
226
  proc.stdout.on("data", (data) => {
227
- stdout += data.toString();
 
 
 
 
 
228
  });
229
 
230
  proc.stderr.on("data", (data) => {
231
- stderr += data.toString();
 
 
 
 
 
232
  });
233
 
234
  // Error handler for spawn failures
@@ -255,17 +323,18 @@ export class BenchmarkQueue extends EventEmitter {
255
  return;
256
  }
257
 
258
- // Extract JSON from stdout (last JSON object)
259
- const jsonMatch = stdout.match(/\{[\s\S]*"platform"[\s\S]*\}/);
260
- if (jsonMatch) {
261
- try {
262
- const result = JSON.parse(jsonMatch[0]);
263
- resolve(result);
264
- } catch (e) {
265
- reject(new Error(`Failed to parse benchmark result: ${e}`));
266
- }
267
  } else {
268
- reject(new Error("No benchmark result found in output"));
 
 
 
 
 
 
269
  }
270
  }
271
  });
 
29
  private queue: QueuedBenchmark[] = [];
30
  private isProcessing = false;
31
 
32
+ /**
33
+ * Extract JSON from stdout by finding the last valid JSON object
34
+ * that contains "platform" field
35
+ */
36
+ private extractJsonResult(stdout: string): any {
37
+ // Try to find all potential JSON objects (lines that start with { and contain "platform")
38
+ const lines = stdout.split('\n');
39
+
40
+ // Search backwards for the last valid JSON object with "platform"
41
+ for (let i = lines.length - 1; i >= 0; i--) {
42
+ const line = lines[i].trim();
43
+ if (line.startsWith('{') && line.includes('"platform"')) {
44
+ try {
45
+ const parsed = JSON.parse(line);
46
+ if (parsed.platform) {
47
+ return parsed;
48
+ }
49
+ } catch (e) {
50
+ // Not valid JSON on this line, continue searching
51
+ continue;
52
+ }
53
+ }
54
+ }
55
+
56
+ // Fallback: try to extract a multi-line JSON object
57
+ // Find the last occurrence of a JSON block that contains "platform"
58
+ const jsonMatches = stdout.match(/\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}/g);
59
+ if (jsonMatches) {
60
+ // Search backwards through matches
61
+ for (let i = jsonMatches.length - 1; i >= 0; i--) {
62
+ const match = jsonMatches[i];
63
+ if (match.includes('"platform"')) {
64
+ try {
65
+ const parsed = JSON.parse(match);
66
+ if (parsed.platform) {
67
+ return parsed;
68
+ }
69
+ } catch (e) {
70
+ continue;
71
+ }
72
+ }
73
+ }
74
+ }
75
+
76
+ return null;
77
+ }
78
+
79
  addBenchmark(request: BenchmarkRequest): void {
80
  const queued: QueuedBenchmark = {
81
  ...request,
 
186
  }, BENCHMARK_TIMEOUT);
187
 
188
  proc.stdout.on("data", (data) => {
189
+ const chunk = data.toString();
190
+ stdout += chunk;
191
+ // Log stdout in real-time for debugging
192
+ if (process.env.DEBUG_BENCHMARK) {
193
+ logger.log(`[stdout] ${chunk}`);
194
+ }
195
  });
196
 
197
  proc.stderr.on("data", (data) => {
198
+ const chunk = data.toString();
199
+ stderr += chunk;
200
+ // Log stderr in real-time for debugging
201
+ if (process.env.DEBUG_BENCHMARK) {
202
+ logger.error(`[stderr] ${chunk}`);
203
+ }
204
  });
205
 
206
  // Error handler for spawn failures
 
227
  return;
228
  }
229
 
230
+ // Extract JSON from stdout
231
+ const result = this.extractJsonResult(stdout);
232
+ if (result) {
233
+ resolve(result);
 
 
 
 
 
234
  } else {
235
+ logger.error(`[Queue] Failed to extract JSON from stdout.`);
236
+ logger.error(`[Queue] stdout (first 500 chars): ${stdout.substring(0, 500)}`);
237
+ logger.error(`[Queue] stdout (last 500 chars): ${stdout.substring(Math.max(0, stdout.length - 500))}`);
238
+ if (stderr) {
239
+ logger.error(`[Queue] stderr: ${stderr.substring(0, 500)}`);
240
+ }
241
+ reject(new Error("No valid benchmark result found in output"));
242
  }
243
  }
244
  });
 
282
  }, BENCHMARK_TIMEOUT);
283
 
284
  proc.stdout.on("data", (data) => {
285
+ const chunk = data.toString();
286
+ stdout += chunk;
287
+ // Log stdout in real-time for debugging
288
+ if (process.env.DEBUG_BENCHMARK) {
289
+ logger.log(`[stdout] ${chunk}`);
290
+ }
291
  });
292
 
293
  proc.stderr.on("data", (data) => {
294
+ const chunk = data.toString();
295
+ stderr += chunk;
296
+ // Log stderr in real-time for debugging
297
+ if (process.env.DEBUG_BENCHMARK) {
298
+ logger.error(`[stderr] ${chunk}`);
299
+ }
300
  });
301
 
302
  // Error handler for spawn failures
 
323
  return;
324
  }
325
 
326
+ // Extract JSON from stdout
327
+ const result = this.extractJsonResult(stdout);
328
+ if (result) {
329
+ resolve(result);
 
 
 
 
 
330
  } else {
331
+ logger.error(`[Queue] Failed to extract JSON from stdout.`);
332
+ logger.error(`[Queue] stdout (first 500 chars): ${stdout.substring(0, 500)}`);
333
+ logger.error(`[Queue] stdout (last 500 chars): ${stdout.substring(Math.max(0, stdout.length - 500))}`);
334
+ if (stderr) {
335
+ logger.error(`[Queue] stderr: ${stderr.substring(0, 500)}`);
336
+ }
337
+ reject(new Error("No valid benchmark result found in output"));
338
  }
339
  }
340
  });