Help us understand the problem. What is going on with this article?

Apache JMeterで任意のタイムゾーンの現在時刻文字列変数を設定する

More than 5 years have passed since last update.

JMeterのBSF Pre Processorでjavascriptを用いてゴリゴリする。

環境

  • Mac OS/X 10.7.5
  • JMeter 2.9

設定

BSF Pre Processorをこんな感じで
kobito.1381554878.157791.png

BSF Pre Processor Script の中身をこんな感じでゴリゴリ

// Parameters 第一引数 (変換後 のタイムゾーンオフセット [時])
var tzHoursOffset = args[0];
// Parameters 第二引数 (変換後 のタイムゾーンオフセット [分])
var tzMinutesOffset = args[1];

// 引数のタイムゾーンオフセットをミリ秒単位に変換
var tzOffset = tzHoursOffset*60*60*1000 + tzMinutesOffset*60*1000;

var localDate = new Date();
var localOffset = localDate.getTimezoneOffset() * 60000;
var utc = localDate.getTime() + localOffset;

var targetTime = utc + tzOffset;

var now = new Date(targetTime);
var year = now.getFullYear();
var month = zeroPadding(now.getMonth() + 1);
var date = zeroPadding(now.getDate());
var hour = zeroPadding(now.getHours());
var minutes = zeroPadding(now.getMinutes());
var seconds = zeroPadding(now.getSeconds());

// 変数'utcDate'に "YYYY-MM-DD'T'HH:mm;ss +HH:mm"な文字列を設定する
vars.put('utcDate', year + "-" + month + "-" + date + 'T' + hour + ":" + minutes + ":" + seconds + getTimeZoneString(tzHoursOffset, tzMinutesOffset));

/**
   * 時、分を"+HH:MM" フォーマットに変換
   * @param hourOffset {Number} 時
   * @param minutesOffset {Number} 分
   * @return {String} "+HH:MM"
   */
function getTimeZoneString(hourOffset, minutesOffset){
  var sign = hourOffset >= 0 ? "+" : "-";
  var hours = zeroPadding(""+hourOffset);
  var minutes = zeroPadding(""+minutesOffset);
  return sign + hours + ":" + minutes;
}

/**
   * 1桁の数値を 0埋め
   */
function zeroPadding(target){
    return ('00'+target).slice(-2);
}

出力イメージ

HTTP Requestの設定
kobito.1381555050.585914.png

出力結果

Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 124
User-Agent: Java/1.7.0_09
Host: localhost:8000
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

<RequestBody>
  <element sequence="3">
     <timestamp>2013-10-12T05:06:41+00:00</timestamp>
  </element>
</RequestBody>POST / HTTP/1.1
Connection: close
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away