Skip to content

Commit

Permalink
update tests in Core.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
jfilhoGN committed Dec 9, 2024
1 parent 2eae902 commit 30b18e1
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 148 deletions.
13 changes: 12 additions & 1 deletion Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.11.2"
manifest_format = "2.0"
project_hash = "73e97e1e860af47c0406ba159f7490ea45df6a19"
project_hash = "7eab075625ba15bf446b221f49c83162bb294be8"

[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
Expand Down Expand Up @@ -189,6 +189,11 @@ git-tree-sha1 = "cc5231d52eb1771251fbd37171dbc408bcc8a1b6"
uuid = "2e619515-83b5-522b-bb60-26c02a35a201"
version = "2.6.4+0"

[[deps.ExprTools]]
git-tree-sha1 = "27415f162e6028e81c72b82ef756bf321213b6ec"
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
version = "0.1.10"

[[deps.FFMPEG]]
deps = ["FFMPEG_jll"]
git-tree-sha1 = "53ebe7511fa11d33bec688a9178fac4e49eeee00"
Expand Down Expand Up @@ -550,6 +555,12 @@ version = "1.2.0"
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
version = "1.11.0"

[[deps.Mocking]]
deps = ["Compat", "ExprTools"]
git-tree-sha1 = "2c140d60d7cb82badf06d8783800d0bcd1a7daa2"
uuid = "78c3b35d-d492-501b-9361-3d52fe80e533"
version = "0.8.1"

[[deps.MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2023.12.12"
Expand Down
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Mocking = "78c3b35d-d492-501b-9361-3d52fe80e533"
PkgDevTools = "2afb0842-1d6c-4499-8b39-71b06199676f"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand All @@ -17,18 +18,19 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "1.11"
CSV = "0.10.15"
Coverage = "1.6.1"
DataFrames = "1.7.0"
HTTP = "1.10.10"
JSON = "0.21.4"
Mocking = "0.8.1"
PkgDevTools = "0.1.0"
Plots = "1.40.9"
Printf = "1.11.0"
Random = "1.11.0"
Statistics = "1.11.1"
Test = "1.11.0"
julia = "1.11"

[extras]
Coverage = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
Expand Down
309 changes: 163 additions & 146 deletions test/test_core.jl
Original file line number Diff line number Diff line change
@@ -1,161 +1,178 @@
using Test
using HTTP
using Random
using Stressify
using Test
using Mocking
import .Core

@testset "HTTP Methods" begin
endpoint = "http://example.com"

@testset "http_get" begin
result = Stressify.http_get(endpoint)
@test result isa NamedTuple
@test result.method == HTTP.get
@test result.url == endpoint
@test result.payload === nothing
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_get with checks" begin
checks = [Stressify.Check("Check if status is 200", r -> r.status == 200)]
result = Stressify.http_get(endpoint, checks=checks)
@test result.checks == checks
end

@testset "http_post" begin
payload = "some data"
headers = Dict("Content-Type" => "application/json")
result = Stressify.http_post(endpoint, payload=payload, headers=headers)
@test result isa NamedTuple
@test result.method == HTTP.post
@test result.url == endpoint
@test result.payload == payload
@test result.headers == headers
@test result.checks == []
end

@testset "http_put" begin
payload = "updated data"
result = Stressify.http_put(endpoint, payload=payload)
@test result isa NamedTuple
@test result.method == HTTP.put
@test result.url == endpoint
@test result.payload == payload
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_patch" begin
payload = "partial update"
result = Stressify.http_patch(endpoint, payload=payload)
@test result isa NamedTuple
@test result.method == HTTP.patch
@test result.url == endpoint
@test result.payload == payload
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_delete" begin
headers = Dict("Authorization" => "Bearer token")
result = Stressify.http_delete(endpoint, headers=headers)
@test result isa NamedTuple
@test result.method == HTTP.delete
@test result.url == endpoint
@test result.payload === nothing
@test result.headers == headers
@test result.checks == []
end

@testset "Check with all HTTP methods" begin
checks = [Stressify.Check("Check if response is successful", r -> r.status < 400)]
for func in [Stressify.http_get, Stressify.http_post, Stressify.http_put, Stressify.http_patch, Stressify.http_delete]
result = func(endpoint, checks=checks)
@test result.checks == checks
end
end
endpoint = "http://example.com"

@testset "http_get" begin
result = Stressify.http_get(endpoint)
@test result isa NamedTuple
@test result.method == HTTP.get
@test result.url == endpoint
@test result.payload === nothing
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_get with checks" begin
checks = [Stressify.Check("Check if status is 200", r -> r.status == 200)]
result = Stressify.http_get(endpoint, checks = checks)
@test result.checks == checks
end

@testset "http_post" begin
payload = "some data"
headers = Dict("Content-Type" => "application/json")
result = Stressify.http_post(endpoint, payload = payload, headers = headers)
@test result isa NamedTuple
@test result.method == HTTP.post
@test result.url == endpoint
@test result.payload == payload
@test result.headers == headers
@test result.checks == []
end

@testset "http_put" begin
payload = "updated data"
result = Stressify.http_put(endpoint, payload = payload)
@test result isa NamedTuple
@test result.method == HTTP.put
@test result.url == endpoint
@test result.payload == payload
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_patch" begin
payload = "partial update"
result = Stressify.http_patch(endpoint, payload = payload)
@test result isa NamedTuple
@test result.method == HTTP.patch
@test result.url == endpoint
@test result.payload == payload
@test result.headers == Dict()
@test result.checks == []
end

@testset "http_delete" begin
headers = Dict("Authorization" => "Bearer token")
result = Stressify.http_delete(endpoint, headers = headers)
@test result isa NamedTuple
@test result.method == HTTP.delete
@test result.url == endpoint
@test result.payload === nothing
@test result.headers == headers
@test result.checks == []
end

@testset "Check with all HTTP methods" begin
checks = [Stressify.Check("Check if response is successful", r -> r.status < 400)]
for func in [Stressify.http_get, Stressify.http_post, Stressify.http_put, Stressify.http_patch, Stressify.http_delete]
result = func(endpoint, checks = checks)
@test result.checks == checks
end
end
end

@testset "format_results" begin
mock_results = Dict(
"vus" => 5,
"iterations" => 100,
"success_rate" => 98.5,
"error_rate" => 1.5,
"rps" => 15.2,
"tps" => 14.9,
"errors" => 2,
"min_time" => 0.01,
"max_time" => 2.5,
"mean_time" => 0.5,
"median_time" => 0.3,
"p90_time" => 1.0,
"p95_time" => 1.5,
"p99_time" => 2.0,
"std_time" => 0.4,
"all_times" => [0.1, 0.2, 0.3, 0.4, 0.5]
)

formatted = Stressify.format_results(mock_results)
@testset "Header Format" begin
@test occursin("================== Stressify ==================", formatted)
end

@testset "Basic Information" begin
@test occursin("VUs : 5", formatted)
@test occursin("Iterações Totais : 100", formatted)
@test occursin("Taxa de Sucesso (%) : 98.5", formatted)
@test occursin("Taxa de Erros (%) : 1.5", formatted)
@test occursin("Requisições por Segundo: 15.2", formatted)
@test occursin("Transações por Segundo : 14.9", formatted)
@test occursin("Número de Erros : 2", formatted)
end

@testset "Time Metrics" begin
@test occursin("Tempo Mínimo : 0.01", formatted)
@test occursin("Tempo Máximo : 2.5", formatted)
@test occursin("Tempo Médio : 0.5", formatted)
@test occursin("Mediana : 0.3", formatted)
@test occursin("P90 : 1.0", formatted)
@test occursin("P95 : 1.5", formatted)
@test occursin("P99 : 2.0", formatted)
@test occursin("Desvio Padrão : 0.4", formatted)
end

@testset "All Times" begin
@test occursin("Todos os Tempos (s) : 0.1, 0.2, 0.3, 0.4, 0.5", formatted)
end

@testset "Footer Format" begin
@test occursin("==========================================================", formatted)
end
mock_results = Dict(
"vus" => 5,
"iterations" => 100,
"success_rate" => 98.5,
"error_rate" => 1.5,
"rps" => 15.2,
"tps" => 14.9,
"errors" => 2,
"min_time" => 0.01,
"max_time" => 2.5,
"mean_time" => 0.5,
"median_time" => 0.3,
"p90_time" => 1.0,
"p95_time" => 1.5,
"p99_time" => 2.0,
"std_time" => 0.4,
"all_times" => [0.1, 0.2, 0.3, 0.4, 0.5],
)

formatted = Stressify.format_results(mock_results)

@testset "Header Format" begin
@test occursin("================== Stressify ==================", formatted)
end

@testset "Basic Information" begin
@test occursin("VUs : 5", formatted)
@test occursin("Iterações Totais : 100", formatted)
@test occursin("Taxa de Sucesso (%) : 98.5", formatted)
@test occursin("Taxa de Erros (%) : 1.5", formatted)
@test occursin("Requisições por Segundo: 15.2", formatted)
@test occursin("Transações por Segundo : 14.9", formatted)
@test occursin("Número de Erros : 2", formatted)
end

@testset "Time Metrics" begin
@test occursin("Tempo Mínimo : 0.01", formatted)
@test occursin("Tempo Máximo : 2.5", formatted)
@test occursin("Tempo Médio : 0.5", formatted)
@test occursin("Mediana : 0.3", formatted)
@test occursin("P90 : 1.0", formatted)
@test occursin("P95 : 1.5", formatted)
@test occursin("P99 : 2.0", formatted)
@test occursin("Desvio Padrão : 0.4", formatted)
end

@testset "All Times" begin
@test occursin("Todos os Tempos (s) : 0.1, 0.2, 0.3, 0.4, 0.5", formatted)
end

@testset "Footer Format" begin
@test occursin("==========================================================", formatted)
end
end


@testset "compute_statistics" begin
# Teste com dados
@testset "with data" begin
all_times = [0.1, 0.2, 0.5, 0.1, 0.3]
total_errors = Base.Threads.Atomic{Int}(1)
total_requests = 5
total_duration = 1.0
vus = 5

stats = Stressify.compute_statistics(all_times, total_errors, total_requests, total_duration, vus)

@test stats["iterations"] == 5
@test stats["errors"] == 1
@test stats["success_rate"] 80.0
@test stats["error_rate"] 20.0
@test stats["min_time"] 0.1
@test stats["max_time"] 0.5
@test stats["mean_time"] (0.1 + 0.2 + 0.5 + 0.1 + 0.3) / 5
@test stats["median_time"] 0.2
@test stats["vus"] == vus
@test stats["rps"] 5.0
@test stats["tps"] 4.0
@test stats["all_times"] == all_times
end
# Teste com dados
@testset "with data" begin
all_times = [0.1, 0.2, 0.5, 0.1, 0.3]
total_errors = Base.Threads.Atomic{Int}(1)
total_requests = 5
total_duration = 1.0
vus = 5

stats = Stressify.compute_statistics(all_times, total_errors, total_requests, total_duration, vus)

@test stats["iterations"] == 5
@test stats["errors"] == 1
@test stats["success_rate"] 80.0
@test stats["error_rate"] 20.0
@test stats["min_time"] 0.1
@test stats["max_time"] 0.5
@test stats["mean_time"] (0.1 + 0.2 + 0.5 + 0.1 + 0.3) / 5
@test stats["median_time"] 0.2
@test stats["vus"] == vus
@test stats["rps"] 5.0
@test stats["tps"] 4.0
@test stats["all_times"] == all_times
end
end


@testset "Testing options with mocked GLOBAL_OPTIONS" begin

Stressify.options()

Stressify.options(vus = 10, format = "vus-ramping", max_vus = 50, ramp_duration = 5.0, iterations = 100, duration = 60.0)
@test_throws ErrorException Stressify.options(format = "vus-ramping", max_vus = nothing, ramp_duration = 5.0, duration = 60.0)
@test_throws ErrorException Stressify.options(format = "vus-ramping", max_vus = 50, ramp_duration = nothing, duration = 60.0)
@test_throws ErrorException Stressify.options(format = "vus-ramping", max_vus = 50, ramp_duration = 5.0, duration = nothing)

Stressify.options(format="default", max_vus=nothing, ramp_duration=nothing, duration=nothing)

end

Loading

0 comments on commit 30b18e1

Please sign in to comment.