2016-09-14 156 views
0

需要測試的應用程序,但我得到以下錯誤:的JMeter的示例代碼

ERROR - jmeter.samplers.SampleResult: setEndTime must be called after setStartTime java.lang.Throwable: Invalid call sequence 
    at org.apache.jmeter.samplers.SampleResult.setEndTime(SampleResult.java:1023) 
    at org.apache.jmeter.samplers.SampleResult.sampleEnd(SampleResult.java:1062) 
    at infy.dop.rsi.jmeter.EmoDb.runTest(EmoDb.java:94) 
    at org.apache.jmeter.protocol.java.sampler.JavaSampler.sample(JavaSampler.java:191) 
    at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434) 
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261) 
    at java.lang.Thread.run(Thread.java:745) 

他是我的代碼:

public class EmoDb extends AbstractJavaSamplerClient { 
    private static final Logger LOGGER = LoggerFactory.getLogger(EmoDb.class); 
    private Properties prop = new Properties(); 
    public Arguments getDefaultParameters() { 
     Arguments defaultParameters = new Arguments(); 
     return defaultParameters; 
    } 
    public void setupTest(JavaSamplerContext context) { 
     for (Iterator<String> it = context.getParameterNamesIterator(); it.hasNext();) { 
      String paramName = it.next(); 
      prop.put(paramName, context.getParameter(paramName)); 
     } 
    } 
    @Override 
    public SampleResult runTest(JavaSamplerContext arg0) { 
     SampleResult result = new SampleResult(); 
     try { 
      URL url=new URL(" "); 
      HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 
      result.sampleStart(); 
      int code = connection.getResponseCode(); 
      if (code==200) 
      { 
       System.out.println("OK"); 
       result.sampleEnd(); 
      } 
      else { 
       throw new Exception("status is not=200"); 
      } 

     } catch (Exception e) { 
      LOGGER.error(ExceptionUtils.getStackTrace(e)); 
      result.sampleEnd(); 
      result.setSuccessful(false); 
      result.setResponseMessage("Exception: " + e); 
      result.setSampleLabel(e.getMessage()); 
      return result; 
     } 
     result.setSuccessful(true); 
     result.setResponseMessage("Successfully completed"); 
     result.setResponseOK(); 
     return result; 
    } 

} 
+0

什麼是'SampleResult.java:1023' –

回答

0

如果你有塊中的任何異常:

URL url=new URL(" "); 
    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.connect(); 

catch-block被調用。在那裏,你叫

result.sampleEnd(); 

result.sampleStart(); 

不叫,因爲異常。

+0

。我強烈地刪除了這段代碼中的url。我實際上在我的系統中使用,因爲我不應該公開這個url,所以我做了。 – user3619264

+0

@ user3619264不要將它留空。添加一個僞URL像http://example.com – Jens

+0

@ user3619264你確信你不是之前'result.sampleStart()得到任何異常;' – Jens