Skip to content

Commit

Permalink
add the returned response code statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
laibulle committed Mar 21, 2017
1 parent 9c390e7 commit a985452
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/slap/report.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
defmodule Slap.Report do
defstruct metrics: [], success: 0, total: 0, average_latency: 0, total_iterations: 0, current_iteration: 0
defstruct metrics: [], codes: %{}, success: 0, total: 0, average_latency: 0, total_iterations: 0, current_iteration: 0
end
8 changes: 8 additions & 0 deletions lib/slap/reporter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Slap.Reporter do
def init(_state) do
{:ok, %{
metrics: [],
codes: %{},
success: 0,
total: 0,
average_latency: 0,
Expand Down Expand Up @@ -38,6 +39,12 @@ defmodule Slap.Reporter do
state = %{state | success: state[:success] + 1 }
end

count = case Map.has_key?(state.codes, metric.status_code) do
false -> 0
true -> Map.get(state.codes, metric.status_code) + 1
end

state = %{ state | codes: Map.put(state.codes, metric.status_code, count) }
state = %{ state | total: state[:total] + 1 }
state = %{ state | metrics: state[:metrics] ++ [metric] }
state = %{ state | total_time: state[:total_time] + metric.latency }
Expand All @@ -62,6 +69,7 @@ defmodule Slap.Reporter do
%Slap.Report{
success: state.success,
metrics: state.metrics,
codes: state.codes,
total: state.total,
average_latency: state.average_latency,
total_iterations: state.total_iterations,
Expand Down
7 changes: 5 additions & 2 deletions lib/slap/reporter_cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Slap.ReporterCli do
if report.total_iterations > 0 do
IO.write "\e[1A"
IO.write "\r"
clean(80)
clean(200)
IO.write "\r"
write(report)
IO.write "\n"
Expand All @@ -32,6 +32,9 @@ defmodule Slap.ReporterCli do
end

defp write(report) do
IO.write " Total: #{report.total} | Success: #{report.success} | Fail: #{report.total - report.success} | Average latency: #{trunc(report.average_latency / 1000000)} ms"
IO.write " Total: #{report.total} | Success: #{report.success} | Fail: #{report.total - report.success} | Average latency: #{trunc(report.average_latency / 1000000)} ms "
Enum.each(report.codes, fn({k, x}) ->
IO.write(" | #{k} : #{x}")
end)
end
end

0 comments on commit a985452

Please sign in to comment.