FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sequin/scripts/load_postgres.exs at main · sequinstream/sequin · GitHub
sequinstream
sequin
Repository navigation
Code
Issues
58
(58)
Pull requests
14
(14)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sequin
/
scripts
/
load_postgres.exs
Copy path
More file actions
More file actions
Latest commit
History
History
History
184 lines (154 loc) · 5.01 KB
Breadcrumbs
sequin
/
scripts
/
load_postgres.exs
Copy path
File metadata and controls
184 lines (154 loc) · 5.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Mix
.
install
(
[
# Database and Ecto
{
:ecto_sql
,
"~> 3.10"
}
,
{
:postgrex
,
">= 0.0.0"
}
]
)
# Configuration constants
defmodule
LoadPostgres
do
@
moduledoc
false
@
target_accumulated_messages
100_000
@
batch_size
100
@
total_throughput
10000
@
duration_seconds
300
@
concurrency
20
# Generate random string for names
defp
random_string
(
length
\\
10
)
do
length
|>
:crypto
.
strong_rand_bytes
(
)
|>
Base
.
encode16
(
)
|>
binary_part
(
0
,
length
)
end
# Insert new rows with random names
defp
insert_batch
(
conn
,
batch_size
)
do
names
=
for
_
<-
1
..
batch_size
,
do:
random_string
(
)
values
=
Enum
.
map_join
(
names
,
", "
,
fn
name
->
"(DEFAULT, '
#{
name
}
', 0)"
end
)
query
=
"INSERT INTO load_test_table_1 (id, name, idx) VALUES
#{
values
}
"
Postgrex
.
query!
(
conn
,
query
,
[
]
)
batch_size
end
# Update existing rows by incrementing idx
defp
update_batch
(
conn
,
batch_size
)
do
query
=
"""
UPDATE load_test_table_1
SET idx = idx + 1
WHERE id IN (
SELECT id FROM load_test_table_1
ORDER BY RANDOM()
LIMIT
#{
batch_size
}
)
"""
result
=
Postgrex
.
query!
(
conn
,
query
,
[
]
)
result
.
num_rows
end
# Delete rows to maintain target count
defp
delete_batch
(
conn
,
batch_size
)
do
query
=
"""
DELETE FROM load_test_table_1
WHERE id IN (
SELECT id FROM load_test_table_1
ORDER BY RANDOM()
LIMIT
#{
batch_size
}
)
"""
result
=
Postgrex
.
query!
(
conn
,
query
,
[
]
)
result
.
num_rows
end
# Get current row count
defp
get_row_count
(
conn
)
do
result
=
Postgrex
.
query!
(
conn
,
"SELECT COUNT(*) FROM load_test_table_1"
,
[
]
)
result
.
rows
|>
List
.
first
(
)
|>
List
.
first
(
)
end
# Main load testing function
def
run_load_test
(
conn
)
do
IO
.
puts
(
"Starting load test..."
)
IO
.
puts
(
"Target accumulated messages:
#{
@
target_accumulated_messages
}
"
)
IO
.
puts
(
"Batch size:
#{
@
batch_size
}
"
)
IO
.
puts
(
"Total throughput:
#{
@
total_throughput
}
"
)
IO
.
puts
(
"Concurrency:
#{
@
concurrency
}
"
)
# Start by truncating the table
IO
.
puts
(
"
\n
Truncating table..."
)
Postgrex
.
query!
(
conn
,
"TRUNCATE load_test_table_1 RESTART IDENTITY"
,
[
]
)
# Phase 1: Insert until we reach target accumulated messages
IO
.
puts
(
"
\n
Phase 1: Inserting messages to reach target..."
)
insert_until_target
(
conn
)
# Phase 2: Maintain target count while processing total throughput
IO
.
puts
(
"
\n
Phase 2: Processing total throughput while maintaining target count..."
)
process_throughput
(
conn
)
IO
.
puts
(
"
\n
Load test completed!"
)
end
defp
insert_until_target
(
conn
)
do
current_count
=
get_row_count
(
conn
)
if
current_count
<
@
target_accumulated_messages
do
needed_messages
=
@
target_accumulated_messages
-
current_count
batches_needed
=
ceil
(
needed_messages
/
@
batch_size
)
IO
.
puts
(
"Need
#{
batches_needed
}
batches to reach target..."
)
for
batch_num
<-
1
..
batches_needed
do
inserted
=
insert_batch
(
conn
,
@
batch_size
)
current_count
=
get_row_count
(
conn
)
IO
.
puts
(
"Batch
#{
batch_num
}
: Inserted
#{
inserted
}
rows. Total:
#{
current_count
}
"
)
end
end
end
defp
process_throughput
(
conn
)
do
operations_per_second
=
@
total_throughput
/
@
batch_size
total_operations
=
round
(
@
duration_seconds
*
operations_per_second
)
ms_per_operation
=
round
(
1000
/
operations_per_second
*
@
concurrency
)
# Process operations with concurrency
1
..
total_operations
|>
Stream
.
map
(
fn
_
->
case
:rand
.
uniform
(
)
do
x
when
x
<=
0.6
->
:update
x
when
x
<=
0.9
->
:insert
_
->
:delete
end
end
)
|>
Task
.
async_stream
(
fn
operation
->
start_time
=
System
.
monotonic_time
(
:millisecond
)
case
operation
do
:insert
->
insert_batch
(
conn
,
@
batch_size
)
:update
->
update_batch
(
conn
,
@
batch_size
)
:delete
->
delete_batch
(
conn
,
@
batch_size
)
end
end_time
=
System
.
monotonic_time
(
:millisecond
)
duration
=
end_time
-
start_time
to_sleep
=
max
(
0
,
ms_per_operation
-
duration
)
Process
.
sleep
(
to_sleep
)
end
,
max_concurrency:
@
concurrency
,
ordered:
false
,
timeout:
30_000
)
|>
Stream
.
map
(
fn
{
:ok
,
result
}
->
result
end
)
|>
Stream
.
chunk_every
(
ceil
(
operations_per_second
*
@
concurrency
)
)
|>
Enum
.
each
(
fn
_chunk
->
current_count
=
get_row_count
(
conn
)
IO
.
puts
(
"Processed batch. Current row count:
#{
current_count
}
"
)
end
)
# Keep connection alive for a moment to see final results
Process
.
sleep
(
1000
)
final_count
=
get_row_count
(
conn
)
IO
.
puts
(
"
\n
Final row count:
#{
final_count
}
"
)
end
end
{
:ok
,
conn
}
=
Postgrex
.
start_link
(
database:
"dune"
,
username:
"postgres"
,
password:
"postgres"
,
hostname:
"localhost"
,
port:
5432
)
# Setup the load test table with proper schema
Postgrex
.
query!
(
conn
,
"CREATE TABLE IF NOT EXISTS load_test_table_1 (id SERIAL PRIMARY KEY, name VARCHAR(255), idx INTEGER DEFAULT 0)"
,
[
]
)
# Run the load test
LoadPostgres
.
run_load_test
(
conn
)
# Close connection
GenServer
.
stop
(
conn
)
Back
|
FazBrowse Home
|
New Git URL