测试工具代码是发送http request部分,出于安全考虑,重要部分略过:
while(System.currentTimeMillis() - start <= runtime){
StringBuffer sb = new StringBuffer();
List<String> data = new ArrayList<String>();
HttpURLConnection httpurlconnection = null;
try{
URL url = new URL(this.reportAd);
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setConnectTimeout(5000);
httpurlconnection.setReadTimeout(5000);
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setRequestProperty("Content-type", "text/plain");
for(long i=0; i<this.recordnum; i++){
。。。。。。
s = Math.abs(R.nextLong())%102400000+1024;
staticWriteSize += s;
reporter.incrCounter("TestTool", "Write Size", s);
staticWriteTime += (endTime - startTime);
reporter.incrCounter("TestTool", "Write Time", endTime - startTime);
。。。。。。
}else{
。。。。。。
reporter.incrCounter("TestTool", "Read Size", s);
staticReadTime += (endTime - startTime);
reporter.incrCounter("TestTool", "Read Time", endTime - startTime);
。。。。。。
}
Pair p = value.get(R.nextInt(value.size()));
。。。。。。
staticCount++;
}
reporter.incrCounter("TestTool", "Record num", this.recordnum);
reporter.setStatus("Record: "+staticCount+"("+staticWrite+"w, "+staticRead+"r), Write Size: "
+staticWriteSize+", Write Time: "+staticWriteTime
+", Read Size: "+staticReadSize+", Read Time: "+staticReadTime);
httpurlconnection.getOutputStream().write(sb.toString().getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
int code = httpurlconnection.getResponseCode();
if(code != 200) {
LOG.warn("send data to master server failed, code=" + code);
}
reporter.incrCounter("TestTool", "Http Post num", 1);
map.staticPost.addAndGet(1);
Thread.sleep(interval);
} catch (Exception e) {
map.staticPost.addAndGet(1);
reporter.incrCounter("TestTool", e.getClass().toString(), 1);
LOG.warn(e.getMessage(), e);
} finally {
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
}
  有了工具代码之后,我们通过实现Mapper来封装该工具,因此上述代码中我使用了“org.apache.hadoop.mapred.Reporter”的方法“incrCounter(Stringarg0, String arg1, long arg2)”来对测试中的重要过程数据进行计数,该方法会将所有map/reduce task中汇报的arg0|arg1定义的值arg2进行相加,输出到MR的jobtracker页面上,通过观察作业执行页面可以实时获取这些测试执行过程信息。此外,我还调度了“setStatus(String arg0)”方法,该方法可以实时更新当前所处task的页面信息,提供更详细的单个task执行情况信息。jobdetails.jsp页面观察结果如下所示: