Skip to content

Commit 435669b

Browse files
authored
perf: use orjson on bin, fixtures and scripts (#70453)
The goal is to get rid of all `sentry.utils.json` usages, and this is a step towards that goal.
1 parent 21a2f1c commit 435669b

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

bin/mock-outcomes

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python
2+
import orjson
3+
24
from sentry.runner import configure
35

46
configure()
@@ -10,7 +12,6 @@ import requests
1012
from django.conf import settings
1113

1214
from sentry.constants import DataCategory
13-
from sentry.utils import json
1415
from sentry.utils.outcomes import Outcome
1516

1617
# import random
@@ -24,7 +25,7 @@ def store_outcomes(outcome, num_times=1):
2425
outcomes.append(outcome_copy)
2526

2627
req = requests.post(
27-
settings.SENTRY_SNUBA + "/tests/entities/outcomes/insert", data=json.dumps(outcomes)
28+
settings.SENTRY_SNUBA + "/tests/entities/outcomes/insert", data=orjson.dumps(outcomes)
2829
)
2930
req.raise_for_status()
3031

bin/run-model-tests

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import os
33
import os.path
44

55
import click
6-
7-
from sentry.utils import json
6+
import orjson
87

98

109
def find_test_cases_matching(model_name: str):
11-
manifest = json.loads(open(os.environ["SENTRY_MODEL_MANIFEST_FILE_PATH"]).read())
10+
manifest = orjson.loads(open(os.environ["SENTRY_MODEL_MANIFEST_FILE_PATH"], "rb").read())
1211
for test_node_id, hits in manifest.items():
1312
if model_name in hits:
1413
yield test_node_id.split("::")[1]

fixtures/apidocs_test_case.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import os
33

4+
import orjson
45
from django.conf import settings
56
from openapi_core.contrib.django import DjangoOpenAPIRequest, DjangoOpenAPIResponse
67
from openapi_core.spec import Spec
@@ -9,16 +10,15 @@
910
from sentry.testutils.cases import APITestCase
1011
from sentry.testutils.helpers.datetime import before_now, iso_format
1112
from sentry.testutils.skips import requires_snuba
12-
from sentry.utils import json
1313

1414

1515
@requires_snuba
1616
class APIDocsTestCase(APITestCase):
1717
@functools.cached_property
1818
def cached_schema(self):
1919
path = os.path.join(os.path.dirname(__file__), "../tests/apidocs/openapi-derefed.json")
20-
with open(path) as json_file:
21-
data = json.load(json_file)
20+
with open(path, "rb") as json_file:
21+
data = orjson.loads(json_file.read())
2222
data["servers"][0]["url"] = settings.SENTRY_OPTIONS["system.url-prefix"]
2323
del data["components"]
2424

fixtures/integrations/mock_service.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from collections import defaultdict
66
from typing import Any
77

8+
import orjson
9+
810
from fixtures.integrations import FIXTURE_DIRECTORY
911
from fixtures.integrations.stub_service import StubService
10-
from sentry.utils import json
1112
from sentry.utils.numbers import base32_encode
1213

1314

@@ -99,8 +100,8 @@ def _set_data(self, project, name, data):
99100
return
100101

101102
path = os.path.join(self._get_project_path(project), f"{name}.json")
102-
with open(path, "w") as f:
103-
f.write(json.dumps(data))
103+
with open(path, "wb") as f:
104+
f.write(orjson.dumps(data))
104105

105106
def _get_data(self, project, name):
106107
if self.mode == "memory":
@@ -112,5 +113,5 @@ def _get_data(self, project, name):
112113
if not os.path.exists(path):
113114
return None
114115

115-
with open(path) as f:
116-
return json.loads(f.read())
116+
with open(path, "rb") as f:
117+
return orjson.loads(f.read())

fixtures/integrations/stub_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from copy import deepcopy
55
from typing import Any
66

7+
import orjson
8+
79
from fixtures.integrations import FIXTURE_DIRECTORY
8-
from sentry.utils import json
910

1011

1112
class StubService:
@@ -48,7 +49,7 @@ def get_stub_data(service_name, name):
4849
if cached:
4950
data = cached
5051
else:
51-
data = json.loads(StubService.get_stub_json(service_name, name))
52+
data = orjson.loads(StubService.get_stub_json(service_name, name))
5253
StubService.stub_data_cache[cache_key] = data
5354
return deepcopy(data)
5455

scripts/appconnect_cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import sys
1111
from pprint import pprint
1212

13+
import orjson
14+
1315
from sentry.lang.native.appconnect import SYMBOL_SOURCES_PROP_NAME
1416
from sentry.models.appconnectbuilds import AppConnectBuild
1517
from sentry.models.project import Project
1618
from sentry.tasks import app_store_connect
17-
from sentry.utils import json
1819

1920
PROJECT_ID = 2
2021

@@ -24,7 +25,7 @@ def main(argv):
2425
# Dumps the symbolSource configuration as stored in the project option.
2526
project = Project.objects.get(pk=PROJECT_ID)
2627
raw_config = project.get_option(SYMBOL_SOURCES_PROP_NAME, default="[]")
27-
config = json.loads(raw_config)
28+
config = orjson.loads(raw_config)
2829
pprint(config)
2930

3031
elif argv[0] == "dsyms":
@@ -65,7 +66,7 @@ def main(argv):
6566
def appconnect_config():
6667
project = Project.objects.get(pk=PROJECT_ID)
6768
raw_config = project.get_option(SYMBOL_SOURCES_PROP_NAME, default="[]")
68-
symbol_sources = json.loads(raw_config)
69+
symbol_sources = orjson.loads(raw_config)
6970
for config in symbol_sources:
7071
if config["type"] == "appStoreConnect":
7172
return config

0 commit comments

Comments
 (0)