This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_metrics.py
More file actions
247 lines (202 loc) · 8.83 KB
/
Copy pathtest_metrics.py
File metadata and controls
247 lines (202 loc) · 8.83 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import os
import unittest.mock
import google.cloud.bigquery as bigquery
import pytest
import bigframes.session.metrics as metrics
NOW = datetime.datetime.now(datetime.timezone.utc)
def test_count_job_stats_with_row_iterator():
row_iterator = unittest.mock.create_autospec(
bigquery.table.RowIterator, instance=True
)
row_iterator.total_bytes_processed = 1024
row_iterator.query = "SELECT * FROM table"
row_iterator.slot_millis = 1234
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(row_iterator=row_iterator)
assert execution_metrics.execution_count == 1
assert execution_metrics.bytes_processed == 1024
assert execution_metrics.query_char_count == 19
assert execution_metrics.slot_millis == 1234
def test_count_job_stats_with_row_iterator_missing_stats():
row_iterator = unittest.mock.create_autospec(
bigquery.table.RowIterator, instance=True
)
# Simulate properties not being present on the object
del row_iterator.total_bytes_processed
del row_iterator.query
del row_iterator.slot_millis
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(row_iterator=row_iterator)
assert execution_metrics.execution_count == 1
assert execution_metrics.bytes_processed == 0
assert execution_metrics.query_char_count == 0
assert execution_metrics.slot_millis == 0
def test_count_job_stats_with_row_iterator_none_stats():
row_iterator = unittest.mock.create_autospec(
bigquery.table.RowIterator, instance=True
)
row_iterator.total_bytes_processed = None
row_iterator.query = None
row_iterator.slot_millis = None
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(row_iterator=row_iterator)
assert execution_metrics.execution_count == 1
assert execution_metrics.bytes_processed == 0
assert execution_metrics.query_char_count == 0
assert execution_metrics.slot_millis == 0
def test_count_job_stats_with_dry_run():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = True
query_job.query = "SELECT * FROM table"
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(query_job=query_job)
# Dry run jobs shouldn't count as "executed"
assert execution_metrics.execution_count == 0
assert execution_metrics.bytes_processed == 0
assert execution_metrics.query_char_count == 0
assert execution_metrics.slot_millis == 0
def test_count_job_stats_with_valid_job():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.query = "SELECT * FROM table"
query_job.total_bytes_processed = 2048
query_job.slot_millis = 5678
query_job.created = NOW
query_job.ended = NOW + datetime.timedelta(seconds=2)
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(query_job=query_job)
assert execution_metrics.execution_count == 1
assert execution_metrics.bytes_processed == 2048
assert execution_metrics.query_char_count == 19
assert execution_metrics.slot_millis == 5678
assert execution_metrics.execution_secs == pytest.approx(2.0)
def test_count_job_stats_with_cached_job():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.query = "SELECT * FROM table"
# Cache hit jobs don't have total_bytes_processed or slot_millis
query_job.total_bytes_processed = None
query_job.slot_millis = None
query_job.created = NOW
query_job.ended = NOW + datetime.timedelta(seconds=1)
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(query_job=query_job)
assert execution_metrics.execution_count == 1
assert execution_metrics.bytes_processed == 0
assert execution_metrics.query_char_count == 19
assert execution_metrics.slot_millis == 0
assert execution_metrics.execution_secs == pytest.approx(1.0)
def test_count_job_stats_with_unsupported_job():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.query = "SELECT * FROM table"
# Some jobs, such as scripts, don't have these properties.
query_job.total_bytes_processed = None
query_job.slot_millis = None
query_job.created = None
query_job.ended = None
execution_metrics = metrics.ExecutionMetrics()
execution_metrics.count_job_stats(query_job=query_job)
# Don't count jobs if we can't get performance stats.
assert execution_metrics.execution_count == 0
assert execution_metrics.bytes_processed == 0
assert execution_metrics.query_char_count == 0
assert execution_metrics.slot_millis == 0
assert execution_metrics.execution_secs == pytest.approx(0.0)
def test_get_performance_stats_with_valid_job():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.query = "SELECT * FROM table"
query_job.total_bytes_processed = 2048
query_job.slot_millis = 5678
query_job.created = NOW
query_job.ended = NOW + datetime.timedelta(seconds=2)
stats = metrics.get_performance_stats(query_job)
assert stats is not None
query_char_count, bytes_processed, slot_millis, exec_seconds = stats
assert query_char_count == 19
assert bytes_processed == 2048
assert slot_millis == 5678
assert exec_seconds == pytest.approx(2.0)
def test_get_performance_stats_with_dry_run():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = True
stats = metrics.get_performance_stats(query_job)
assert stats is None
def test_get_performance_stats_with_missing_timestamps():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.created = None
query_job.ended = NOW
stats = metrics.get_performance_stats(query_job)
assert stats is None
query_job.created = NOW
query_job.ended = None
stats = metrics.get_performance_stats(query_job)
assert stats is None
def test_get_performance_stats_with_mocked_types():
query_job = unittest.mock.create_autospec(bigquery.QueryJob, instance=True)
query_job.configuration.dry_run = False
query_job.created = NOW
query_job.ended = NOW
query_job.total_bytes_processed = unittest.mock.Mock()
query_job.slot_millis = 123
stats = metrics.get_performance_stats(query_job)
assert stats is None
query_job.total_bytes_processed = 123
query_job.slot_millis = unittest.mock.Mock()
stats = metrics.get_performance_stats(query_job)
assert stats is None
@pytest.fixture
def mock_environ(monkeypatch):
"""Fixture to mock os.environ."""
monkeypatch.setenv(metrics.LOGGING_NAME_ENV_VAR, "my_test_case")
def test_write_stats_to_disk_writes_files(tmp_path, mock_environ):
os.chdir(tmp_path)
test_name = os.environ[metrics.LOGGING_NAME_ENV_VAR]
metrics.write_stats_to_disk(
query_char_count=100,
bytes_processed=200,
slot_millis=300,
exec_seconds=1.23,
)
slot_file = tmp_path / (test_name + ".slotmillis")
assert slot_file.exists()
with open(slot_file) as f:
assert f.read() == "300\n"
exec_time_file = tmp_path / (test_name + ".bq_exec_time_seconds")
assert exec_time_file.exists()
with open(exec_time_file) as f:
assert f.read() == "1.23\n"
query_char_count_file = tmp_path / (test_name + ".query_char_count")
assert query_char_count_file.exists()
with open(query_char_count_file) as f:
assert f.read() == "100\n"
bytes_file = tmp_path / (test_name + ".bytesprocessed")
assert bytes_file.exists()
with open(bytes_file) as f:
assert f.read() == "200\n"
def test_write_stats_to_disk_no_env_var(tmp_path, monkeypatch):
monkeypatch.delenv(metrics.LOGGING_NAME_ENV_VAR, raising=False)
os.chdir(tmp_path)
metrics.write_stats_to_disk(
query_char_count=100,
bytes_processed=200,
slot_millis=300,
exec_seconds=1.23,
)
assert len(list(tmp_path.iterdir())) == 0