Skip to content

Commit 9d9e377

Browse files
committed
Add MicroBenchmarking Logs
1 parent 68eae04 commit 9d9e377

File tree

4 files changed

+196
-21
lines changed

4 files changed

+196
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021-2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.fhir.gateway.plugin;
17+
18+
import org.slf4j.Logger;
19+
20+
public class BenchmarkingHelper {
21+
public static void printCompletedInDuration(long startTime, String methodDetails, Logger logger) {
22+
long nanoSecondsTaken = System.nanoTime() - startTime;
23+
long millSecondsTaken = nanoSecondsTaken / 1000000;
24+
logger.error(
25+
String.format(
26+
"########## %s completed in %s : Metric in seconds - %d : Metric in nanoseconds - %d",
27+
methodDetails,
28+
getHumanDuration(millSecondsTaken),
29+
millSecondsTaken / 1000,
30+
nanoSecondsTaken));
31+
}
32+
33+
public static String getHumanDuration(long milliseconds) {
34+
35+
long minutes = (milliseconds / 1000) / 60;
36+
long seconds = (milliseconds / 1000) % 60;
37+
String secondsStr = Long.toString(seconds);
38+
String secs;
39+
if (secondsStr.length() >= 2) {
40+
secs = secondsStr.substring(0, 2);
41+
} else {
42+
secs = "0" + secondsStr;
43+
}
44+
45+
return minutes == 0 && seconds == 0
46+
? "less than a second"
47+
: minutes + " mins " + secs + " secs";
48+
}
49+
50+
public static long startBenchmarking() {
51+
return System.nanoTime();
52+
}
53+
}

Diff for: plugins/src/main/java/com/google/fhir/gateway/plugin/OpenSRPHelper.java

+83-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2021-2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.google.fhir.gateway.plugin;
217

318
import static org.smartregister.utils.Constants.EMPTY_STRING;
@@ -66,6 +81,9 @@ public PractitionerDetails getPractitionerDetailsByKeycloakId(String keycloakUUI
6681
}
6782

6883
public Bundle getSupervisorPractitionerDetailsByKeycloakId(String keycloakUUID) {
84+
85+
long start = BenchmarkingHelper.startBenchmarking();
86+
6987
Bundle bundle = new Bundle();
7088

7189
logger.info("Searching for practitioner with identifier: " + keycloakUUID);
@@ -79,10 +97,18 @@ public Bundle getSupervisorPractitionerDetailsByKeycloakId(String keycloakUUID)
7997
logger.error("Practitioner with KC identifier: " + keycloakUUID + " not found");
8098
}
8199

100+
BenchmarkingHelper.printCompletedInDuration(
101+
start,
102+
"getSupervisorPractitionerDetailsByKeycloakId : params --> keycloakUUID=" + keycloakUUID,
103+
logger);
104+
82105
return bundle;
83106
}
84107

85108
private Bundle getAttributedPractitionerDetailsByPractitioner(Practitioner practitioner) {
109+
110+
long start = BenchmarkingHelper.startBenchmarking();
111+
86112
Bundle responseBundle = new Bundle();
87113
List<Practitioner> attributedPractitioners = new ArrayList<>();
88114
PractitionerDetails practitionerDetails = getPractitionerDetailsByPractitioner(practitioner);
@@ -112,6 +138,10 @@ private Bundle getAttributedPractitionerDetailsByPractitioner(Practitioner pract
112138

113139
careTeamList.addAll(attributedCareTeams);
114140

141+
logger.error(
142+
"##### OpenSRPHelper.getAttributedPractitionerDetailsByPractitioner() -> CareTeam List"
143+
+ attributedCareTeams);
144+
115145
for (CareTeam careTeam : careTeamList) {
116146
// Add current supervisor practitioners
117147
attributedPractitioners.addAll(
@@ -139,11 +169,18 @@ private Bundle getAttributedPractitionerDetailsByPractitioner(Practitioner pract
139169

140170
responseBundle.setEntry(bundleEntryComponentList);
141171
responseBundle.setTotal(bundleEntryComponentList.size());
172+
173+
BenchmarkingHelper.printCompletedInDuration(
174+
start, "getAttributedPractitionerDetailsByPractitioner: params " + practitioner, logger);
175+
142176
return responseBundle;
143177
}
144178

145179
@NotNull
146180
public static List<String> getAttributedLocations(List<LocationHierarchy> locationHierarchies) {
181+
182+
long start = BenchmarkingHelper.startBenchmarking();
183+
147184
List<ParentChildrenMap> parentChildrenList =
148185
locationHierarchies.stream()
149186
.flatMap(
@@ -159,6 +196,10 @@ public static List<String> getAttributedLocations(List<LocationHierarchy> locati
159196
.flatMap(parentChildren -> parentChildren.getChildIdentifiers().stream())
160197
.map(it -> getReferenceIDPart(it.toString()))
161198
.collect(Collectors.toList());
199+
200+
BenchmarkingHelper.printCompletedInDuration(
201+
start, "getAttributedLocations " + locationHierarchies, logger);
202+
162203
return attributedLocationsList;
163204
}
164205

@@ -167,6 +208,8 @@ private List<String> getOrganizationIdsByLocationIds(List<String> attributedLoca
167208
return new ArrayList<>();
168209
}
169210

211+
long start = BenchmarkingHelper.startBenchmarking();
212+
170213
Bundle organizationAffiliationsBundle =
171214
getFhirClientForR4()
172215
.search()
@@ -175,15 +218,21 @@ private List<String> getOrganizationIdsByLocationIds(List<String> attributedLoca
175218
.returnBundle(Bundle.class)
176219
.execute();
177220

178-
return organizationAffiliationsBundle.getEntry().stream()
179-
.map(
180-
bundleEntryComponent ->
181-
getReferenceIDPart(
182-
((OrganizationAffiliation) bundleEntryComponent.getResource())
183-
.getOrganization()
184-
.getReference()))
185-
.distinct()
186-
.collect(Collectors.toList());
221+
List<String> organizationIDs =
222+
organizationAffiliationsBundle.getEntry().stream()
223+
.map(
224+
bundleEntryComponent ->
225+
getReferenceIDPart(
226+
((OrganizationAffiliation) bundleEntryComponent.getResource())
227+
.getOrganization()
228+
.getReference()))
229+
.distinct()
230+
.collect(Collectors.toList());
231+
232+
BenchmarkingHelper.printCompletedInDuration(
233+
start, "getOrganizationIdsByLocationIds : params " + attributedLocationsList, logger);
234+
235+
return organizationIDs;
187236
}
188237

189238
private String getPractitionerIdentifier(Practitioner practitioner) {
@@ -196,31 +245,33 @@ private String getPractitionerIdentifier(Practitioner practitioner) {
196245

197246
private PractitionerDetails getPractitionerDetailsByPractitioner(Practitioner practitioner) {
198247

248+
long start = BenchmarkingHelper.startBenchmarking();
249+
199250
PractitionerDetails practitionerDetails = new PractitionerDetails();
200251
FhirPractitionerDetails fhirPractitionerDetails = new FhirPractitionerDetails();
201252
String practitionerId = getPractitionerIdentifier(practitioner);
202253

203-
logger.info("Searching for care teams for practitioner with id: " + practitioner);
254+
logger.error("Searching for care teams for practitioner with id: " + practitioner);
204255
Bundle careTeams = getCareTeams(practitionerId);
205256
List<CareTeam> careTeamsList = mapBundleToCareTeams(careTeams);
206257
fhirPractitionerDetails.setCareTeams(careTeamsList);
207258
fhirPractitionerDetails.setPractitioners(Arrays.asList(practitioner));
208259

209-
logger.info("Searching for Organizations tied with CareTeams: ");
260+
logger.error("Searching for Organizations tied with CareTeams: ");
210261
List<String> careTeamManagingOrganizationIds =
211262
getManagingOrganizationsOfCareTeamIds(careTeamsList);
212263

213264
Bundle careTeamManagingOrganizations = getOrganizationsById(careTeamManagingOrganizationIds);
214-
logger.info("Managing Organization are fetched");
265+
logger.error("Managing Organization are fetched");
215266

216267
List<Organization> managingOrganizationTeams =
217268
mapBundleToOrganizations(careTeamManagingOrganizations);
218269

219-
logger.info("Searching for organizations of practitioner with id: " + practitioner);
270+
logger.error("Searching for organizations of practitioner with id: " + practitioner);
220271

221272
List<PractitionerRole> practitionerRoleList =
222273
getPractitionerRolesByPractitionerId(practitionerId);
223-
logger.info("Practitioner Roles are fetched");
274+
logger.error("Practitioner Roles are fetched");
224275

225276
List<String> practitionerOrganizationIds =
226277
getOrganizationIdsByPractitionerRoles(practitionerRoleList);
@@ -237,13 +288,13 @@ private PractitionerDetails getPractitionerDetailsByPractitioner(Practitioner pr
237288
fhirPractitionerDetails.setPractitionerRoles(practitionerRoleList);
238289

239290
Bundle groupsBundle = getGroupsAssignedToPractitioner(practitionerId);
240-
logger.info("Groups are fetched");
291+
logger.error("Groups are fetched");
241292

242293
List<Group> groupsList = mapBundleToGroups(groupsBundle);
243294
fhirPractitionerDetails.setGroups(groupsList);
244295
fhirPractitionerDetails.setId(practitionerId);
245296

246-
logger.info("Searching for locations by organizations");
297+
logger.error("Searching for locations by organizations");
247298

248299
Bundle organizationAffiliationsBundle =
249300
getOrganizationAffiliationsByOrganizationIdsBundle(
@@ -259,17 +310,20 @@ private PractitionerDetails getPractitionerDetailsByPractitioner(Practitioner pr
259310

260311
List<String> locationIds = getLocationIdsByOrganizationAffiliations(organizationAffiliations);
261312

262-
logger.info("Searching for location hierarchy list by locations identifiers");
313+
logger.error("Searching for location hierarchy list by locations identifiers");
263314
List<LocationHierarchy> locationHierarchyList = getLocationsHierarchyByLocationIds(locationIds);
264315
fhirPractitionerDetails.setLocationHierarchyList(locationHierarchyList);
265316

266-
logger.info("Searching for locations by ids");
317+
logger.error("Searching for locations by ids");
267318
List<Location> locationsList = getLocationsByIds(locationIds);
268319
fhirPractitionerDetails.setLocations(locationsList);
269320

270321
practitionerDetails.setId(practitionerId);
271322
practitionerDetails.setFhirPractitionerDetails(fhirPractitionerDetails);
272323

324+
BenchmarkingHelper.printCompletedInDuration(
325+
start, "getPractitionerDetailsByPractitioner : params " + practitioner, logger);
326+
273327
return practitionerDetails;
274328
}
275329

@@ -476,6 +530,8 @@ private List<OrganizationAffiliation> mapBundleToOrganizationAffiliation(
476530
private List<LocationHierarchy> getLocationsHierarchyByLocationIds(List<String> locationIds) {
477531
if (locationIds.isEmpty()) return new ArrayList<>();
478532

533+
long start = BenchmarkingHelper.startBenchmarking();
534+
479535
Bundle bundle =
480536
getFhirClientForR4()
481537
.search()
@@ -484,9 +540,15 @@ private List<LocationHierarchy> getLocationsHierarchyByLocationIds(List<String>
484540
.returnBundle(Bundle.class)
485541
.execute();
486542

487-
return bundle.getEntry().stream()
488-
.map(it -> ((LocationHierarchy) it.getResource()))
489-
.collect(Collectors.toList());
543+
List<LocationHierarchy> locationHierarchies =
544+
bundle.getEntry().stream()
545+
.map(it -> ((LocationHierarchy) it.getResource()))
546+
.collect(Collectors.toList());
547+
548+
BenchmarkingHelper.printCompletedInDuration(
549+
start, "getLocationsHierarchyByLocationIds : params " + locationIds, logger);
550+
551+
return locationHierarchies;
490552
}
491553

492554
public static String createSearchTagValues(Map.Entry<String, String[]> entry) {

Diff for: plugins/src/main/java/com/google/fhir/gateway/plugin/OpenSRPSyncAccessDecision.java

+18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public boolean canAccess() {
108108
@Override
109109
public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader) {
110110

111+
long start = BenchmarkingHelper.startBenchmarking();
112+
111113
RequestMutation requestMutation = null;
112114
if (isSyncUrl(requestDetailsReader)) {
113115
if (locationIds.isEmpty() && careTeamIds.isEmpty() && organizationIds.isEmpty()) {
@@ -137,6 +139,14 @@ public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsRea
137139
}
138140
}
139141

142+
BenchmarkingHelper.printCompletedInDuration(
143+
start,
144+
"getRequestMutation: params by RequestDetailReader "
145+
+ requestDetailsReader.getRequestType()
146+
+ " /"
147+
+ requestDetailsReader.getRequestPath(),
148+
logger);
149+
140150
return requestMutation;
141151
}
142152

@@ -162,6 +172,8 @@ private List<String> addSyncFilters(Map<String, String[]> syncTags) {
162172
public String postProcess(RequestDetailsReader request, HttpResponse response)
163173
throws IOException {
164174

175+
long start = BenchmarkingHelper.startBenchmarking();
176+
165177
String resultContent = null;
166178
Resource resultContentBundle;
167179
String gatewayMode = request.getHeader(Constants.FHIR_GATEWAY_MODE);
@@ -197,6 +209,12 @@ public String postProcess(RequestDetailsReader request, HttpResponse response)
197209
fhirR4Context.newJsonParser().encodeResourceToString(practitionerDetailsBundle);
198210
}
199211

212+
BenchmarkingHelper.printCompletedInDuration(
213+
start,
214+
"postProcess : params include Gateway mode "
215+
+ (gatewayMode != null ? gatewayMode : "Default"),
216+
logger);
217+
200218
return resultContent;
201219
}
202220

0 commit comments

Comments
 (0)