Skip to content

Commit 57fe269

Browse files
authored
WIP: working on (bad) performance (#888)
tolldemo --------- Signed-off-by: Mark Nelson <mark.x.nelson@oracle.com>
1 parent 85c5f09 commit 57fe269

File tree

7 files changed

+89
-12
lines changed

7 files changed

+89
-12
lines changed

tolldemo/queue-reader/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# mark's dodgy bodgy sql slower downer
2+
3+
```sql
4+
create or replace function remove_state(plate in varchar2)
5+
return varchar2
6+
is
7+
begin
8+
dbms_lock.sleep(dbms_random.value(0.004,0.005));
9+
return regexp_replace(plate, '[A-Z]+-', '', 1, 1);
10+
end;
11+
```

tolldemo/queue-reader/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@
146146
<groupId>org.springframework.boot</groupId>
147147
<artifactId>spring-boot-maven-plugin</artifactId>
148148
</plugin>
149+
<!-- for migration to Spring Boot 3 -->
150+
<plugin>
151+
<groupId>org.openrewrite.maven</groupId>
152+
<artifactId>rewrite-maven-plugin</artifactId>
153+
<version>5.29.0</version>
154+
<configuration>
155+
<activeRecipes>
156+
<recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2</recipe>
157+
</activeRecipes>
158+
</configuration>
159+
<dependencies>
160+
<dependency>
161+
<groupId>org.openrewrite.recipe</groupId>
162+
<artifactId>rewrite-spring</artifactId>
163+
<version>5.8.0</version>
164+
</dependency>
165+
</dependencies>
166+
</plugin>
149167
</plugins>
150168
</build>
151169

tolldemo/queue-reader/src/main/java/com/example/queuereader/QueueReaderApplication.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33

44
package com.example.queuereader;
55

6+
import javax.jms.ConnectionFactory;
7+
68
import org.springframework.boot.SpringApplication;
79
import org.springframework.boot.autoconfigure.SpringBootApplication;
810
import org.springframework.cloud.openfeign.EnableFeignClients;
11+
import org.springframework.context.ApplicationContext;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.DependsOn;
914
import org.springframework.jms.annotation.EnableJms;
15+
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
1016

1117
@EnableJms
1218
@SpringBootApplication
@@ -17,4 +23,15 @@ public static void main(String[] args) {
1723
SpringApplication.run(QueueReaderApplication.class, args);
1824
}
1925

26+
@Bean
27+
@DependsOn("aqJmsConnectionFactory")
28+
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ApplicationContext context) {
29+
DefaultJmsListenerContainerFactory factory =
30+
new DefaultJmsListenerContainerFactory();
31+
ConnectionFactory connectionFactory = (ConnectionFactory) context.getBean("aqJmsConnectionFactory");
32+
factory.setConnectionFactory(connectionFactory);
33+
factory.setConcurrency("1-1");
34+
return factory;
35+
}
36+
2037
}

tolldemo/queue-reader/src/main/java/com/example/queuereader/controller/TollReaderReceiver.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
import com.fasterxml.jackson.databind.node.ObjectNode;
1414

1515
import io.micrometer.core.annotation.Timed;
16+
import io.micrometer.core.instrument.Counter;
1617
import io.micrometer.core.instrument.Tags;
1718
import io.micrometer.core.instrument.Timer;
1819
import io.micrometer.prometheus.PrometheusMeterRegistry;
1920
import lombok.extern.slf4j.Slf4j;
2021

2122
import java.util.List;
2223

24+
import org.apache.tomcat.util.modeler.Registry;
2325
import org.springframework.jms.annotation.JmsListener;
2426
import org.springframework.stereotype.Component;
2527

@@ -33,6 +35,7 @@ public class TollReaderReceiver {
3335
private AIVisionService aiVisionService;
3436
private CustomerDataService customerDataService;
3537
private Timer timer;
38+
private Counter counter;
3639

3740
public TollReaderReceiver(
3841
JournalService journalService,
@@ -44,10 +47,12 @@ public TollReaderReceiver(
4447
this.aiVisionService = aiVisionService;
4548
this.customerDataService = customerDataService;
4649
timer = registry.timer("process.toll.read", Tags.empty());
50+
counter = registry.counter("toll.messages.count", Tags.empty());
4751
}
4852

4953
@JmsListener(destination = "TollGate")
5054
public void receiveTollData(String tollData) {
55+
counter.increment();
5156
Timer.Sample sample = Timer.start();
5257
log.info("Received message {}", tollData);
5358
try {
@@ -58,9 +63,10 @@ public void receiveTollData(String tollData) {
5863
log.info("Check that the tag, licensePlate and accountNumber match up");
5964
String tagId = tollDataJson.get("tagId").asText();
6065
String accountId = tollDataJson.get("accountNumber").asText();
61-
String licensePlate = tollDataJson.get("licensePlate").asText().split("-")[1];
66+
String licensePlate = tollDataJson.get("licensePlate").asText();
67+
String vehicleType = tollDataJson.get("vehicleType").asText();
6268

63-
List<AccountDetails> accountDetails = customerDataService.getAccountDetails(licensePlate);
69+
List<AccountDetails> accountDetails = customerDataService.getAccountDetails(licensePlate, vehicleType);
6470
boolean found = false;
6571
for (AccountDetails a : accountDetails) {
6672
if (a.getAccountNumber().equalsIgnoreCase(accountId) && a.getTagId().equalsIgnoreCase(tagId)) {
@@ -85,7 +91,7 @@ public void receiveTollData(String tollData) {
8591
} catch (JsonProcessingException e) {
8692
e.printStackTrace();
8793
} finally {
88-
timer.record(() -> sample.stop(timer) / 1_000_000);
94+
timer.record(() -> sample.stop(timer) / 1_000);
8995
}
9096
}
9197
}

tolldemo/queue-reader/src/main/java/com/example/queuereader/service/CustomerDataService.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,55 @@
99

1010
import com.example.queuereader.model.AccountDetails;
1111

12+
import io.micrometer.core.instrument.Tags;
13+
import io.micrometer.core.instrument.Timer;
14+
import io.micrometer.prometheus.PrometheusMeterRegistry;
1215
import lombok.extern.slf4j.Slf4j;
1316

1417
@Service
1518
@Slf4j
1619
public class CustomerDataService {
1720

21+
private Timer timer;
22+
1823
private final JdbcTemplate jdbcTemplate;
1924

20-
public CustomerDataService(JdbcTemplate jdbcTemplate) {
25+
public CustomerDataService(JdbcTemplate jdbcTemplate, PrometheusMeterRegistry registry) {
2126
this.jdbcTemplate = jdbcTemplate;
27+
timer = registry.timer("get.account.details", Tags.empty());
2228
}
2329

24-
30+
// slower query
2531
final String accountDetailsQuery =
26-
"select c.customer_id, c.account_number, c.first_name, c.last_name, c.address, c.city, c.zipcode, "
32+
"select /*+ ORDERED_PREDICATES */ c.customer_id, c.account_number, c.first_name, c.last_name, c.address, c.city, c.zipcode, "
2733
+ "v.vehicle_id, v.tag_id, v.state, v.license_plate, v.vehicle_type "
2834
+ "from customer c, vehicle v "
2935
+ "where c.customer_id like '%'||v.customer_id||'%' "
3036
+ "and c.customer_id like ("
31-
+ " select '%'||customer_id||'%' "
37+
+ " select /*+ ORDERED_PREDICATES */ '%'||customer_id||'%' "
38+
+ " from vehicle "
39+
+ " where vehicle_type = 'TTTTTT' "
40+
+ " and license_plate like '%'|| remove_state('XXXXXX') || '%'"
41+
+ ")";
42+
43+
// faster query
44+
final String fasterAccountDetailsQuery =
45+
"select c.customer_id, c.account_number, c.first_name, c.last_name, c.address, c.city, c.zipcode, "
46+
+ "v.vehicle_id, v.tag_id, v.state, v.license_plate, v.vehicle_type "
47+
+ "from customer c, vehicle v "
48+
+ "where c.customer_id = v.customer_id "
49+
+ "and c.customer_id = ("
50+
+ " select customer_id "
3251
+ " from vehicle "
33-
+ " where license_plate like '%XXXXXX%'"
52+
+ " where license_plate = regexp_replace('XXXXXX', '[A-Z]+-', 1, 1)"
3453
+ ")";
3554

3655

37-
public List<AccountDetails> getAccountDetails(String licensePlate) {
56+
public List<AccountDetails> getAccountDetails(String licensePlate, String vehicleType) {
3857
List<AccountDetails> result = new ArrayList<AccountDetails>();
3958
long startTime = System.currentTimeMillis();
40-
jdbcTemplate.query(accountDetailsQuery.replace("XXXXXX", licensePlate),
59+
Timer.Sample sample = Timer.start();
60+
jdbcTemplate.query(accountDetailsQuery.replace("XXXXXX", licensePlate).replace("TTTTTT", vehicleType),
4161
(rs, rowNum) -> new AccountDetails(
4262
rs.getString("customer_id"),
4363
rs.getString("account_number"),
@@ -53,6 +73,7 @@ public List<AccountDetails> getAccountDetails(String licensePlate) {
5373
rs.getString("vehicle_type")
5474
)).forEach(accountDetails -> result.add(accountDetails));
5575
long endTime = System.currentTimeMillis();
76+
timer.record(() -> sample.stop(timer) / 1_000);
5677
log.info("The query took " + (endTime - startTime) + "ms");
5778

5879
log.info("returning " + result.size() + " account detail rows");

tolldemo/queue-reader/src/main/java/com/example/queuereader/service/JournalService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void journal(JsonNode tollData) {
3131
Timer.Sample sample = Timer.start();
3232
log.info("Journal data: {}", tollData);
3333
journalClient.journal(tollData);
34-
timer.record(() -> sample.stop(timer) / 1_000_000);
34+
timer.record(() -> sample.stop(timer) / 1_000);
3535

3636
}
3737
}

tolldemo/queue-reader/src/main/resources/application.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ management:
3131
include: "*"
3232
metrics:
3333
tags:
34-
application: ${spring.application.name}
34+
application: ${spring.application.name}
35+
36+
# server:
37+
# tomcat:
38+
# max-threads: 10

0 commit comments

Comments
 (0)