Webサーバのレスポンスを計測してみる(若葉マーク編)
戻る
とりあえず、WEBサーバが返すベタなテキストだけ取得します。
あとは、画像ファイルつまりIMG要素の部分をパースして、そのURLに対してGETする部分を追加すればいいだけです。
こちらのほうがレスポンス速度に対する影響が大のはずですので、下記はまったくご参考までに。。。
ということで。
$ tail *.txt
==> asahi.txt <==
0.015 0.392
** 0.031 sec 0.016 sec 0.015 sec 0.032 sec 0.015 sec [ totalAverage = 0.054 sec ] (success=28, failure=0)
0.047 0.557
** 0.016 sec 0.015 sec 0.032 sec 0.015 sec 0.047 sec [ totalAverage = 0.054 sec ] (success=29, failure=0)
0.015 0.392
** 0.015 sec 0.032 sec 0.015 sec 0.047 sec 0.015 sec [ totalAverage = 0.053 sec ] (success=30, failure=0)
==> yomiuri.txt <==
0.032 0.502
** 0.032 sec 0.031 sec 0.031 sec 0.031 sec 0.032 sec [ totalAverage = 0.031 sec ] (success=28, failure=0)
0.015 0.392
** 0.031 sec 0.031 sec 0.031 sec 0.032 sec 0.015 sec [ totalAverage = 0.031 sec ] (success=29, failure=0)
0.016 0.401
** 0.031 sec 0.031 sec 0.032 sec 0.015 sec 0.016 sec [ totalAverage = 0.030 sec ] (success=30, failure=0)
::::::::::::::
ContentRetriever.java
::::::::::::::
/**
$Id: SpeedTest.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
import java.io.*;
import java.net.*;
public class ContentRetriever {
/** 処理対象URLの文字列 */
private String targetURL;
private URL url;
private long elapsedTime;
public ContentRetriever( String targetURL ) {
this.targetURL = targetURL;
}
public void execute() throws Exception {
try {
url = new URL( targetURL );
long start;
long end;
start = System.currentTimeMillis();
// コネクトして、HttpURLConnection の作成
HttpURLConnection connection = ( HttpURLConnection ) url.openConnection();
connection.setRequestProperty( "User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)" );
// キャッシュを使わない
connection.setUseCaches( false );
connection.setRequestMethod( "GET" );
// System.out.println( "レスポンスコードは " + connection.getResponseCode() + " です。" );
// System.out.println( "コンテントレングスは " + connection.getContentLength() + " です。" );
BufferedReader br = new BufferedReader( new InputStreamReader(
connection.getInputStream() ) );
String line;
while ( ( line = br.readLine() ) != null ) {
// 空読み
// System.out.println(line);
}
br.close();
// ディスコネクトする
connection.disconnect();
end = System.currentTimeMillis();
elapsedTime = ( end - start ) ;
} catch ( Exception exception ) {
throw exception;
}
}
public long getElapsedTime() {
return elapsedTime;
}
/** メインメソッド */
public static void main( String args[] ) throws Exception {
if ( args.length != 3 ) {
System.out.println( "Usage: java ContentRetriever [url] [conntectTimeOut] [readTimeOut]" );
System.exit( 1 );
}
/******************************************************************************/
/* 接続タイムアウトの設定; 設定された時間でコネクトできない場合はタイムアウト */
/******************************************************************************/
System.setProperty ( "sun.net.client.defaultConnectTimeout", args[ 1 ] );
/**********************************************************************************/
/* 読み取りタイムアウトの設定; 設定された時間で読み取りできない場合はタイムアウト */
/**********************************************************************************/
System.setProperty ( "sun.net.client.defaultReadTimeout", args[ 2 ] );
try {
ContentRetriever retriever = new ContentRetriever( args[ 0 ] );
retriever.execute();
System.out.printf( "所要時間: " + retriever.getElapsedTime() + " msec" );
} catch ( Exception e ) {
throw e;
}
}
}
::::::::::::::
Logarithm.java
::::::::::::::
/**
$Id: SpeedTest.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
public class Logarithm {
static double getValueByBaseOf( double number, double base ) {
return Math.log( number ) / Math.log( base );
}
static public void main( String[] args ) {
double a = 100.0;
System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );
a = 200.0;
System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );
a = 10000.0;
System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );
a = 10.0;
System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );
a = 1.0;
System.out.printf( "%10.3f %10.3f %10.3f\n", a, Math.log( a ), Logarithm.getValueByBaseOf( a, 100.0 ) );
}
}
::::::::::::::
ResultManager.java
::::::::::::::
import java.util.*;
import java.util.concurrent.*;
/**
$Id: SpeedTest.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class ResultManager implements Runnable {
private String topURL;
/** 測定時間を格納するキュー */
private ArrayBlockingQueue queue;
private int queueSize;
private long interval;
private ContentRetriever retriever;
private double totalAverage;
// 例外発生回数
private int exceptionOccurrence = 0;
// 正常終了回数
private int successCount = 0;
public ResultManager( String topURL, int queueSize, long interval ) {
this.topURL = topURL;
this.interval = interval;
this.queueSize = queueSize;
queue = new ArrayBlockingQueue( queueSize );
retriever = new ContentRetriever( topURL );
}
public void start() {
( new Thread( this ) ).start();
}
public void run() {
while ( true ) {
try {
// URLにアクセス
retriever.execute();
// 経過時間を取得
long time = retriever.getElapsedTime();
successCount++;
// DEBUG
System.out.printf( "%10.3f %10.3f\n",
( double ) time / 1000.0,
Logarithm.getValueByBaseOf( ( double ) time, 1000.0 ) );
// 結果をキューに追加
addResult( time );
} catch ( Exception e ) {
exceptionOccurrence++;
e.printStackTrace();
}
// 統計情報を表示
showStatistics();
try {
// 一定時間スリープ
Thread.sleep( interval );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
/** 結果をキューに追加 */
private void addResult( double time ) {
if ( queue.size() == queueSize ) {
// 要素数が最大値に達したら先頭の要素を取り除く
try {
queue.take();
} catch ( Exception e ) {
e.printStackTrace();
}
}
// 要素を追加
boolean status = queue.offer( new Double( time ) );
// アクセス時間平均値の最新情報を計算する
refreshTotalAverage( time );
}
private void refreshTotalAverage( double time ) {
if ( successCount == 1 ) {
totalAverage = time;
} else {
// 現在の母数がNなら、前回までの平均値にN-1を掛けたものと最新の値を足したものに対してNで割る
totalAverage = ( totalAverage * ( successCount - 1 ) + time ) / successCount;
}
}
/** 統計情報を表示 */
private void showStatistics() {
Iterator iterator = queue.iterator();
System.out.print( "**" );
while ( iterator.hasNext() ) {
Double element = ( Double ) iterator.next();
System.out.printf( "%8.3f sec" , element.doubleValue() / 1000.0 );
}
// 平均所要時間
System.out.printf( " [ totalAverage =%6.3f sec ]", totalAverage / 1000.0 );
// 成功・失敗の回数
System.out.printf( " (success=%d, failure=%d)",
successCount, exceptionOccurrence );
System.out.println();
System.out.println();
}
public static void main( String[] args ) {
if ( args.length != 3 ) {
System.out.println( "Usage: java ResultManager [url] [queueSize] [interval]" );
System.exit( 1 );
}
String targetURL = args[ 0 ];
int queueSize = new Integer( args[ 1 ] ).intValue();
long interval = new Long( args[ 2 ] ).longValue();
/******************************************************************************/
/* 接続タイムアウトの設定; 設定された時間でコネクトできない場合はタイムアウト */
/******************************************************************************/
System.setProperty ( "sun.net.client.defaultConnectTimeout", "8000" );
/**********************************************************************************/
/* 読み取りタイムアウトの設定; 設定された時間で読み取りできない場合はタイムアウト */
/**********************************************************************************/
System.setProperty ( "sun.net.client.defaultReadTimeout", "8000" );
ResultManager manager = new ResultManager( targetURL, queueSize, interval );
manager.start();
}
}
戻る