Skip to content

Commit c51ddc8

Browse files
committed
emasure create3
1 parent 2b2e768 commit c51ddc8

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/main/java/io/getstream/chat/java/services/framework/DefaultClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class DefaultClient implements Client {
3232
@NotNull private OkHttpClient okHttpClient;
3333
@NotNull private Retrofit retrofit;
3434
@NotNull private UserServiceFactory serviceFactory;
35+
@NotNull private UserServiceFactory serviceFactory2;
36+
@NotNull private UserServiceFactory serviceFactory3;
3537
@NotNull private final String apiSecret;
3638
@NotNull private final String apiKey;
3739
@NotNull private final Properties extendedProperties;
@@ -77,6 +79,8 @@ public DefaultClient(Properties properties) {
7779
this.apiKey = apiKey.toString();
7880
this.retrofit = buildRetrofitClient();
7981
this.serviceFactory = new UserServiceFactorySelector(retrofit);
82+
this.serviceFactory2 = new UserServiceFactoryTagging(retrofit);
83+
this.serviceFactory3 = new UserServiceFactoryCall(retrofit);
8084
}
8185

8286
private Retrofit buildRetrofitClient() {
@@ -153,20 +157,20 @@ public <TService> TService create(Class<TService> svcClass) {
153157
return retrofit.create(svcClass);
154158
}
155159

156-
@Override
160+
// @Override
157161
@NotNull
158162
public <TService> TService create(Class<TService> svcClass, String userToken) {
159163
return serviceFactory.create(svcClass, new UserToken(userToken));
160164
}
161165

162166
@NotNull
163167
public <TService> TService create2(Class<TService> svcClass, String userToken) {
164-
return new UserServiceFactoryTagging(retrofit).create(svcClass, new UserToken(userToken));
168+
return serviceFactory2.create(svcClass, new UserToken(userToken));
165169
}
166170

167171
@NotNull
168172
public <TService> TService create3(Class<TService> svcClass, String userToken) {
169-
return new UserServiceFactoryCall(retrofit).create(svcClass, new UserToken(userToken));
173+
return serviceFactory3.create(svcClass, new UserToken(userToken));
170174
}
171175

172176
@NotNull

src/test/java/io/getstream/chat/java/CustomTest.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void measureClientCreate() throws Exception {
5252
for (int i = 0; i < 10_000; i++) {
5353
defaultClient.create(UserService.class, userToken);
5454
defaultClient.create2(UserService.class, userToken);
55+
defaultClient.create3(UserService.class, userToken);
5556
}
5657

5758
// Get ThreadMXBean for accurate memory allocation tracking
@@ -92,22 +93,45 @@ void measureClientCreate() throws Exception {
9293
System.out.println("> Second loop avg time per call: " + (elapsedTimeInNs2 / (double) iterations) + " ns");
9394
System.out.println("> Second loop avg memory per call: " + (allocated2 / (double) iterations) + " bytes");
9495

95-
// Performance comparison
96-
if (elapsedTimeInNs1 < elapsedTimeInNs2) {
97-
double timesFaster = (double) elapsedTimeInNs2 / elapsedTimeInNs1;
98-
System.out.println("> create is " + String.format("%.2fx", timesFaster) + " faster than create2");
99-
} else {
100-
double timesFaster = (double) elapsedTimeInNs1 / elapsedTimeInNs2;
101-
System.out.println("> create2 is " + String.format("%.2fx", timesFaster) + " faster than create");
96+
// Measure third test
97+
long allocatedBefore3 = threadBean.getCurrentThreadAllocatedBytes();
98+
startTime = System.nanoTime();
99+
for (int i = 0; i < iterations; i++) {
100+
defaultClient.create3(UserService.class, userToken);
102101
}
102+
endTime = System.nanoTime();
103+
long allocatedAfter3 = threadBean.getCurrentThreadAllocatedBytes();
104+
long elapsedTimeInNs3 = endTime - startTime;
105+
long allocated3 = allocatedAfter3 - allocatedBefore3;
106+
107+
System.out.println("> Third loop elapsed time: " + TimeUnit.NANOSECONDS.toMillis(elapsedTimeInNs3) + " ms");
108+
System.out.println("> Third loop memory allocated: " + (allocated3 / 1024 / 1024) + " MB");
109+
System.out.println("> Third loop avg time per call: " + (elapsedTimeInNs3 / (double) iterations) + " ns");
110+
System.out.println("> Third loop avg memory per call: " + (allocated3 / (double) iterations) + " bytes");
111+
112+
// Performance comparison - Time
113+
long fastestTime = Math.min(elapsedTimeInNs1, Math.min(elapsedTimeInNs2, elapsedTimeInNs3));
114+
String fastestMethod = "";
115+
if (fastestTime == elapsedTimeInNs1) fastestMethod = "create";
116+
else if (fastestTime == elapsedTimeInNs2) fastestMethod = "create2";
117+
else fastestMethod = "create3";
103118

104-
if (allocated1 < allocated2) {
105-
double timesLess = (double) allocated2 / allocated1;
106-
System.out.println("> create allocates " + String.format("%.2fx", timesLess) + " less memory than create2");
107-
} else {
108-
double timesLess = (double) allocated1 / allocated2;
109-
System.out.println("> create2 allocates " + String.format("%.2fx", timesLess) + " less memory than create");
110-
}
119+
System.out.println("> Time comparison (fastest: " + fastestMethod + "):");
120+
System.out.println(" - create: " + String.format("%.2fx", (double) elapsedTimeInNs1 / fastestTime));
121+
System.out.println(" - create2: " + String.format("%.2fx", (double) elapsedTimeInNs2 / fastestTime));
122+
System.out.println(" - create3: " + String.format("%.2fx", (double) elapsedTimeInNs3 / fastestTime));
123+
124+
// Performance comparison - Memory
125+
long leastMemory = Math.min(allocated1, Math.min(allocated2, allocated3));
126+
String mostEfficientMethod = "";
127+
if (leastMemory == allocated1) mostEfficientMethod = "create";
128+
else if (leastMemory == allocated2) mostEfficientMethod = "create2";
129+
else mostEfficientMethod = "create3";
130+
131+
System.out.println("> Memory comparison (least: " + mostEfficientMethod + "):");
132+
System.out.println(" - create: " + String.format("%.2fx", (double) allocated1 / leastMemory));
133+
System.out.println(" - create2: " + String.format("%.2fx", (double) allocated2 / leastMemory));
134+
System.out.println(" - create3: " + String.format("%.2fx", (double) allocated3 / leastMemory));
111135

112136
System.out.println("=========================================================");
113137
}

0 commit comments

Comments
 (0)