You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用NewRelic Synthetics脚本浏览器准确统计测试执行时长并实现可视化?

Hey there, let's tackle your New Relic Synthetics issues step by step. I've worked through similar scenarios before, so here's what you can try to capture the full test duration (including async lazy loading) and fix your metric tracking problems:

1. Capture Full Test Execution Duration (Including Async Lazy Loading)

The core issue is that your test is finishing as soon as the initial page loads, not waiting for the lazy-loaded dataset to complete. You need to explicitly wait for the async content to render before ending your timer.

Here's how to adjust your script:

// Start timer at the very beginning of your script
const startTime = Date.now();

// Navigate to your target page
await $browser.get('https://your-target-url.com');

// Wait for the lazy-loaded element to appear (adjust the CSS selector to match your UI)
await $browser.wait(
  async () => {
    const lazyLoadedElement = await $browser.findElement($driver.By.css('.lazy-loaded-data-container'));
    return await lazyLoadedElement.isDisplayed();
  },
  45000, // Wait up to 45 seconds (match your dataset's typical load time)
  'Timed out waiting for lazy-loaded dataset to load'
);

// Calculate full duration AFTER async content is loaded
const fullDuration = Date.now() - startTime;

This ensures your timer runs from script start until the lazy-loaded content is fully visible, capturing the full 40-second load time you're expecting.

2. Fix Custom Insight Attribute Type (String → Numeric)

Your testDuration is being stored as a string because $util.insights.set() can sometimes coerce numbers to strings, or existing field types get locked in New Relic. Here's how to fix this:

Step 1: Use Numeric-Specific Setter (If Available)

New Relic's Synthetic runtime has a setNumeric() method that ensures the attribute is stored as a number:

// After calculating fullDuration
$util.insights.setNumeric('fullTestDuration', fullDuration);

Step 2: If setNumeric() Isn't Available

Force the value to be a number when setting it:

$util.insights.set('fullTestDuration', Number(fullDuration));

Step 3: Query Correctly in NRQL

Make sure you use the numeric() function in your query, and avoid typos (you had testDurration instead of testDuration earlier):

FROM SyntheticCheck SELECT average(numeric(custom.fullTestDuration)) WHERE numeric(custom.fullTestDuration) > 0 SINCE 1 day ago

Important: If your old testDuration field is already registered as a string, you can't change its type for existing data. Use a new attribute name (like fullTestDuration) to ensure it's stored as numeric from the start.

3. Correctly Using $har for Duration Tracking

The $har object is only available in Chrome-based Synthetic Scripted Browser tests (not the old Firefox ESR runtime). First, confirm your test's runtime is set to Chrome:

  • Go to your Synthetic test settings
  • Under "Runtime", select "Chrome"

Once that's configured, use this syntax to track custom timers with $har:

// Start the custom HAR timer at script launch
const testTimer = $har.addEntry('Full Test Duration');
testTimer.start();

// ... your navigation and wait logic here ...

// End the timer after async content loads
testTimer.end();

You can query this data with NRQL:

FROM SyntheticCheck SELECT average(custom.timers.'Full Test Duration') SINCE 1 day ago
4. Additional Troubleshooting & Best Practices
  • Add Debug Logs: Use $util.log() to verify your timer and wait logic are working:
    $util.log(`Script started at: ${startTime}`);
    $util.log(`Lazy load completed at: ${Date.now()}`);
    $util.log(`Total duration: ${fullDuration}ms`);
    
    Check these logs in your Synthetic test results to confirm the timing matches expectations.
  • Wait for Network Idle: If your lazy load relies on API calls, wait for network activity to stop instead of just UI elements:
    await $browser.waitForNetworkIdle(5000); // Wait 5 seconds with no network requests
    
  • Verify Attribute Type: Run FROM SyntheticCheck SELECT keyset() again after implementing the fix—your new fullTestDuration should appear in numericKeys instead of stringKeys.

内容的提问来源于stack exchange,提问作者Manuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:47:50