Kemal Elapsed time



examples/kemal/src/elapsed_time.cr
require "kemal"

class HTTP::Server::Context
  getter start_time : Time::Span = Time.monotonic
end

get "/" do |env|
  # spend some time
  x = 0
  loop do
    x += 1
    break if x > 10_000_000
  end
  text = "This is some text"
  render "src/views/time.ecr", "src/views/layouts/layout_with_elapsed_time.ecr"
end

Kemal.run

examples/kemal/src/views/layouts/layout_with_elapsed_time.ecr
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
  <title></title>
</head>
<body>
  <%= content %>
<hr>
<div>
Elapsed time <%= (Time.monotonic - env.start_time).total_milliseconds %> ms
</div>
</body>
</html>


examples/kemal/src/views/time.ecr

<%= text %>