LoginSignup
18
14

More than 5 years have passed since last update.

Apache BenchでJSONをPHPに投げてテスト

Posted at

テスト

PHPでJSONを受け取る処理のテストに困りました。
JSONを簡単に投げる方法知らないのですもの。
ab(Apache Bench)を使うと楽ちんです。

ab - Apache Bench

テスト用のJSONファイル

PHPへの投入データです

json.json
{
    "hoge":123456,
    "hege":999
}

Apacheの入っているサーバでぶん投げます。
※ ab(ApacheBench)コマンドが入ってるので…
最初に100スレッド投げてますが1スレッドで良いです。

ekaneko@bibian ~/work/json % cat >./kick_ab.sh
#!/bin/sh
ab -n 100 -c 100 -p ./json.json -T "application/json; charset=utf-8" "http://localhost/tam/getJson.php"
[ctrl]+[d]
ekaneko@bibian ~/work/json % sh ./kick_ab.sh  
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache
Server Hostname:        localhost
Server Port:            80

Document Path:          /tam/getJson.php
Document Length:        5 bytes

Concurrency Level:      100
Time taken for tests:   0.048 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      22800 bytes
Total POSTed:           19800
HTML transferred:       500 bytes
Requests per second:    2064.96 [#/sec] (mean)
Time per request:       48.427 [ms] (mean)
Time per request:       0.484 [ms] (mean, across all concurrent requests)
Transfer rate:          459.78 [Kbytes/sec] received
                        399.28 kb/s sent
                        859.06 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        7   17   2.1     18      19
Processing:     5   17   5.0     17      25
Waiting:        4   17   5.2     17      25
Total:         23   35   5.0     35      42

Percentage of the requests served within a certain time (ms)
  50%     35
  66%     38
  75%     39
  80%     40
  90%     41
  95%     41
  98%     42
  99%     42
 100%     42 (longest request)
ekaneko@bibian ~/work/json % # 1スレッドだけポスト
ekaneko@bibian ~/work/json % ab -n 1 -c 1 -p ./json.json -T "application/json; charset=utf-8" "http://localhost/tam/getJson.php"
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache
Server Hostname:        localhost
Server Port:            80

Document Path:          /tam/getJson.php
Document Length:        5 bytes

Concurrency Level:      1
Time taken for tests:   0.002 seconds
Complete requests:      1
Failed requests:        0
Write errors:           0
Total transferred:      228 bytes
Total POSTed:           198
HTML transferred:       5 bytes
Requests per second:    528.26 [#/sec] (mean)
Time per request:       1.893 [ms] (mean)
Time per request:       1.893 [ms] (mean, across all concurrent requests)
Transfer rate:          117.62 [Kbytes/sec] received
                        102.14 kb/s sent
                        219.77 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     2    2   0.0      2       2
Waiting:        2    2   0.0      2       2
Total:          2    2   0.0      2       2
ekaneko@bibian ~/work/json % 

参考

受け取り側PHPソース

適当にコピペして書いたので違ってたらごめん

getJson.php
<?php

define('DEBUGOUT', './debug_out.log');

function debugPuts($str)
{
    $buff = sprintf("%s %s\n", date("Y-m-d H:i:s"), $str);
    error_log($buff, 3, DEBUGOUT);
}


$raw = file_get_contents( 'php://input');
// if(isset($HTTP_RAW_POST_DATA))
// {
//     $raw = trim($HTTP_RAW_POST_DATA);
// }
$mess = 'getdata:'.var_export($raw, true);
debugPuts($mess);

$insDataArray =  json_decode($raw, true);
$mess = '[json_decode]'.var_export($insDataArray, true);
debugPuts($mess);

// 成功 失敗返却
// $data = array('some_key' => 'some_value');
$data = array(1,0);

header('Access-Control-Allow-Origin: *');
$callback = isset($_GET['callback']) ? preg_replace('/[^a-z0-9$_]/si', '', $_GET['callback']) : false;
if($callback)
{
    $callback = htmlspecialchars(strip_tags($callback));
    header('Content-Type: application/javascript;charset=utf-8');
    echo $callback.'('.json_encode($data).')';
}
else
{
    // header('Content-Type: text/javascript; charset=utf-8');
    header('Content-Type: application/json;charset=utf-8');
    // $data = array('some_key' => 'some_value');
    echo json_encode($data);
}

JSONを受け取った状況

ekaneko@bibian /var/www/tam % ll
合計 20
-rw-r--r-- 1 www-data www-data 15118  6月  2 17:57 debug_out.log
-rw-r--r-- 1 ekaneko  ekaneko   1155  6月  2 17:43 getJson.php
ekaneko@bibian /var/www/tam % 
ekaneko@bibian /var/www/tam % tail -n 20 debug_out.log
  'hege' => 999,
)
2014-06-02 17:46:26 getdata:'{
    "hoge":123456,
    "hege":999
}
'
2014-06-02 17:46:26 [json_decode]array (
  'hoge' => 123456,
  'hege' => 999,
)
2014-06-02 17:57:18 getdata:'{
    "hoge":123456,
    "hege":999
}
'
2014-06-02 17:57:18 [json_decode]array (
  'hoge' => 123456,
  'hege' => 999,
)
ekaneko@bibian /var/www/tam % 
18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14