Skip to content

Commit e4e7765

Browse files
authored
Add memory profile utils (#77)
1 parent def29e6 commit e4e7765

7 files changed

Lines changed: 74 additions & 31 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ __pycache__
44
*.pyc
55
build
66
Manifest.toml
7-
grpc_test_server
7+
grpc_test_server
8+
*.gz
9+
*.vscode

utils/gRPCClientUtils.jl/Project.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ authors = ["Carroll Vance <cs.vance@icloud.com>"]
55

66
[deps]
77
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
8-
gRPCClient = "aaca4a50-36af-4a1d-b878-4c443f2061ad"
8+
PProf = "e4faabce-9ead-11e9-39d9-4379958e3056"
99
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
10+
Profile = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
1011
ProgressBars = "49802e3a-d2f1-5c88-81d8-b72133a6f568"
1112
ProtoBuf = "3349acd9-ac6a-5e09-bcdb-63829b23a429"
13+
gRPCClient = "aaca4a50-36af-4a1d-b878-4c443f2061ad"
14+
15+
[sources]
16+
gRPCClient = {path = "../.."}
1217

1318
[compat]
1419
BenchmarkTools = "~1.6.0"
15-
gRPCClient = "~1.0.0"
20+
PProf = "3.2.0"
1621
PrettyTables = "~3.1.0"
22+
Profile = "1.11.0"
1723
ProgressBars = "~1.5.0"
1824
ProtoBuf = "~1.2"
19-
25+
gRPCClient = "~1.0.0"

utils/gRPCClientUtils.jl/src/Benchmark.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
benchmark_workload_smol() = @benchmark workload_smol(1_000)
2-
benchmark_workload_32_224_224_uint8() = @benchmark workload_32_224_224_uint8(100)
3-
benchmark_workload_streaming_request() = @benchmark workload_streaming_request(1_000)
4-
benchmark_workload_streaming_response() = @benchmark workload_streaming_response(1_000)
5-
benchmark_workload_streaming_bidirectional() =
6-
@benchmark workload_streaming_bidirectional(1_000)
1+
benchmark_workload_smol() = @benchmark workload_smol()
2+
benchmark_workload_32_224_224_uint8() = @benchmark workload_32_224_224_uint8()
3+
benchmark_workload_streaming_request() = @benchmark workload_streaming_request()
4+
benchmark_workload_streaming_response() = @benchmark workload_streaming_response()
5+
benchmark_workload_streaming_bidirectional() = @benchmark workload_streaming_bidirectional()
76

87

9-
function perform_benchmark(f, N)
8+
function perform_benchmark(f)
9+
# Hack to make the code cleaner (benchmarks return number of messages per workload)
10+
N = f()
11+
1012
b = @benchmark $f($N)
1113
timing = sum(b.times) / 1e9
1214
timings_us = b.times ./ 1e3
@@ -42,14 +44,14 @@ function benchmark_table()
4244
["", "KiB/message", "allocs/message", "messages/s", "μs", "μs", "μs", "μs"],
4345
]
4446
all_benchmarks = [
45-
(workload_smol, 1_000),
46-
(workload_32_224_224_uint8, 100),
47-
(workload_streaming_request, 1_000),
48-
(workload_streaming_response, 1_000),
49-
(workload_streaming_bidirectional, 1_000),
47+
workload_smol,
48+
workload_32_224_224_uint8,
49+
workload_streaming_request,
50+
workload_streaming_response,
51+
workload_streaming_bidirectional,
5052
]
5153

52-
all_results = [perform_benchmark(f, N) for (f, N) in ProgressBar(all_benchmarks)]
54+
all_results = [perform_benchmark(f) for f in ProgressBar(all_benchmarks)]
5355

5456
pretty_table(
5557
permutedims(reduce(hcat, all_results));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function profile_memory_fn(f::Function)
2+
grpc_init()
3+
4+
# Warmup
5+
_ = f()
6+
7+
Profile.Allocs.clear()
8+
Profile.Allocs.@profile sample_rate=0.1 f()
9+
PProf.Allocs.pprof()
10+
end
11+
12+
profile_memory_workload_smol() = profile_memory_fn(workload_smol)
13+
profile_memory_workload_32_224_224_uint8() = profile_memory_fn(workload_32_224_224_uint8)
14+
profile_memory_workload_streaming_request() = profile_memory_fn(workload_streaming_request)
15+
profile_memory_workload_streaming_response() =
16+
profile_memory_fn(workload_streaming_response)
17+
profile_memory_workload_streaming_bidirectional() =
18+
profile_memory_fn(workload_streaming_bidirectional)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function stress_workload(f::Function, n)
1+
function stress_workload(f::Function)
22
while true
3-
f(n)
3+
f()
44
end
55
end
66

7-
stress_workload_smol() = stress_workload(workload_smol, 1_000)
8-
stress_workload_32_224_224_uint8() = stress_workload(workload_32_224_224_uint8, 100)
9-
stress_workload_streaming_request() = stress_workload(workload_streaming_request, 1_000)
10-
stress_workload_streaming_response() = stress_workload(workload_streaming_response, 1_000)
7+
stress_workload_smol() = stress_workload(workload_smol)
8+
stress_workload_32_224_224_uint8() = stress_workload(workload_32_224_224_uint8)
9+
stress_workload_streaming_request() = stress_workload(workload_streaming_request)
10+
stress_workload_streaming_response() = stress_workload(workload_streaming_response)
1111
stress_workload_streaming_bidirectional() =
12-
stress_workload(workload_streaming_bidirectional, 1_000)
12+
stress_workload(workload_streaming_bidirectional)

utils/gRPCClientUtils.jl/src/Workloads.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function workload_32_224_224_uint8(n)
1+
function workload_32_224_224_uint8(n = 100)
22
client = TestService_TestRPC_Client("localhost", 8001)
33

44
reqs = Vector{gRPCRequest}()
@@ -14,9 +14,11 @@ function workload_32_224_224_uint8(n)
1414
for req in reqs
1515
grpc_async_await(req)
1616
end
17+
18+
n
1719
end
1820

19-
function workload_smol(n)
21+
function workload_smol(n = 1_000)
2022
client = TestService_TestRPC_Client("localhost", 8001)
2123

2224
# Since requests are lightweight, use async / await pattern to avoid creating an extra task per request
@@ -29,9 +31,11 @@ function workload_smol(n)
2931
for req in reqs
3032
grpc_async_await(req)
3133
end
34+
35+
n
3236
end
3337

34-
function workload_streaming_request(n)
38+
function workload_streaming_request(n = 1_000)
3539
client = TestService_TestClientStreamRPC_Client("localhost", 8001)
3640
requests_c = Channel{TestRequest}(16)
3741

@@ -47,10 +51,10 @@ function workload_streaming_request(n)
4751
response = grpc_async_await(req)
4852
end
4953

50-
nothing
54+
n
5155
end
5256

53-
function workload_streaming_response(n)
57+
function workload_streaming_response(n = 1_000)
5458
client = TestService_TestServerStreamRPC_Client("localhost", 8001)
5559
response_c = Channel{TestResponse}(16)
5660

@@ -61,11 +65,11 @@ function workload_streaming_response(n)
6165
end
6266
close(response_c)
6367

64-
nothing
68+
n
6569
end
6670

6771

68-
function workload_streaming_bidirectional(n)
72+
function workload_streaming_bidirectional(n = 1_000)
6973
client = TestService_TestBidirectionalStreamRPC_Client("localhost", 8001)
7074
requests_c = Channel{TestRequest}(16)
7175
response_c = Channel{TestResponse}(16)
@@ -91,4 +95,6 @@ function workload_streaming_bidirectional(n)
9195

9296
nothing
9397
end
98+
99+
n
94100
end

utils/gRPCClientUtils.jl/src/gRPCClientUtils.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ using BenchmarkTools
55
using PrettyTables
66
using ProgressBars
77
using ProtoBuf
8+
using Profile
9+
using PProf
810

911
include("../../../test/gen/test/test_pb.jl")
1012
include("Workloads.jl")
1113
include("Benchmark.jl")
1214
include("Stress.jl")
15+
include("Profile.jl")
1316

1417
export benchmark_table
1518

@@ -25,5 +28,11 @@ export stress_workload_streaming_request
2528
export stress_workload_streaming_response
2629
export stress_workload_streaming_bidirectional
2730

31+
export profile_memory_workload_smol
32+
export profile_memory_workload_32_224_224_uint8
33+
export profile_memory_workload_streaming_request
34+
export profile_memory_workload_streaming_response
35+
export profile_memory_workload_streaming_bidirectional
36+
2837

2938
end # module gRPCClientUtils

0 commit comments

Comments
 (0)