Skip to content

Commit

Permalink
Merge pull request #72 from matsumotory/add-response-time-method
Browse files Browse the repository at this point in the history
Add response time method
  • Loading branch information
matsumotory committed Jan 25, 2017
2 parents efc55aa + 048de1a commit 2298ee5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -7,7 +7,7 @@ before_install:
install:
- sudo apt-get -qq install rake bison libcurl4-openssl-dev libmarkdown2-dev
env:
- HTTPD_VERSION=httpd-2.2.31
- HTTPD_VERSION=httpd-2.2.32
APR=apr-1.5.2
APR_UTIL=apr-util-1.5.4
HTTPD_CONFIG_OPT='--enable-module=all --enable-mods-shared=all --enable-proxy --enable-proxy-http'
Expand Down
10 changes: 10 additions & 0 deletions src/ap_mrb_request.c
Expand Up @@ -14,6 +14,7 @@
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "util_time.h"

request_rec *mrb_request_rec_state = NULL;

Expand Down Expand Up @@ -656,6 +657,14 @@ static mrb_value ap_mrb_get_request_no_local_copy(mrb_state *mrb, mrb_value str)
return mrb_fixnum_value(val);
}

static mrb_value ap_mrb_get_request_response_time(mrb_state *mrb, mrb_value self)
{
request_rec *r = ap_mrb_get_request();
apr_time_t duration = apr_time_now() - r->request_time;

return mrb_float_value(mrb, ((float)duration / (float)1000000));
}

static mrb_value ap_mrb_run_handler(mrb_state *mrb, mrb_value self)
{
request_rec *r = ap_mrb_get_request();
Expand Down Expand Up @@ -791,6 +800,7 @@ void ap_mruby_request_init(mrb_state *mrb, struct RClass *class_core)
mrb_define_method(mrb, class_request, "eos_sent", ap_mrb_get_request_eos_sent, MRB_ARGS_NONE());
mrb_define_method(mrb, class_request, "no_cache", ap_mrb_get_request_no_cache, MRB_ARGS_NONE());
mrb_define_method(mrb, class_request, "no_local_copy", ap_mrb_get_request_no_local_copy, MRB_ARGS_NONE());
mrb_define_method(mrb, class_request, "response_time", ap_mrb_get_request_response_time, MRB_ARGS_NONE());

mrb_define_method(mrb, class_request, "main?", ap_mrb_get_request_main, MRB_ARGS_NONE());
mrb_define_method(mrb, class_request, "sub_request?", ap_mrb_get_request_main, MRB_ARGS_NONE());
Expand Down
5 changes: 5 additions & 0 deletions test/conf/mod_mruby_test.conf
Expand Up @@ -114,6 +114,11 @@ Listen 127.0.0.1:8081
mrubyHandlerMiddleCode 'Apache.rputs get_server_class.to_s'
</Location>

# test for response time
<Location /response_time>
mrubyHandlerMiddleCode 'sleep 2; Apache.rputs Apache::Request.new.response_time.to_i.to_s'
</Location>

# test for logger
<Location /logger>
mrubyHandlerMiddleCode 'Apache.log Apache::APLOG_DEBUG, "FJRRWZQJUQTKPAUP"'
Expand Down
5 changes: 5 additions & 0 deletions test/t/mod_mruby.rb
Expand Up @@ -203,4 +203,9 @@ def base
t.assert_equal "</index.js>; rel=preload", res["link"]
end

t.assert('mod_mruby', 'location /response_time') do
res = HttpRequest.new.get base + '/response_time'
t.assert_equal "2", res["body"]
end

t.report

0 comments on commit 2298ee5

Please sign in to comment.