|
1
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
2
|
|
|
3
|
import java.sql.*;
|
|
4
|
import java.util.*;
|
|
5
|
import java.util.concurrent.CompletableFuture;
|
|
6
|
public class Test {
|
|
7
|
|
|
8
|
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
|
|
9
|
|
|
10
|
static {
|
|
11
|
try {
|
|
12
|
dataSource.setDriverClass("org.postgresql.Driver");
|
|
13
|
dataSource.setJdbcUrl("jdbc:postgresql://localhost:5433/fwd");
|
|
14
|
dataSource.setUser("fwd_admin");
|
|
15
|
dataSource.setPassword("admin");
|
|
16
|
dataSource.setMinPoolSize(10);
|
|
17
|
dataSource.setMaxPoolSize(100);
|
|
18
|
} catch (Exception e) {
|
|
19
|
throw new RuntimeException("Error setting up datasource", e);
|
|
20
|
}
|
|
21
|
}
|
|
22
|
|
|
23
|
public static void executeParallelQueries() {
|
|
24
|
CompletableFuture<List<Map<String, Object>>> future1 = CompletableFuture.supplyAsync(() ->
|
|
25
|
runStreamingQuery("SELECT *, pg_sleep(0.001) FROM employees e WHERE e.id = 7001;")
|
|
26
|
);
|
|
27
|
|
|
28
|
CompletableFuture<List<Map<String, Object>>> future4 = CompletableFuture.supplyAsync(() ->
|
|
29
|
runStreamingQuery("SELECT *, pg_sleep(0.001) FROM employees e WHERE e.id = 7001;")
|
|
30
|
);
|
|
31
|
|
|
32
|
CompletableFuture<List<Map<String, Object>>> future2 = CompletableFuture.supplyAsync(() ->
|
|
33
|
runStreamingQuery("SELECT *, pg_sleep(0.001) FROM departments d WHERE d.id = 343")
|
|
34
|
);
|
|
35
|
CompletableFuture<List<Map<String, Object>>> future3 = CompletableFuture.supplyAsync(() ->
|
|
36
|
runStreamingQuery("SELECT *, pg_sleep(0.001) FROM departments d WHERE d.id = 343")
|
|
37
|
);
|
|
38
|
|
|
39
|
|
|
40
|
CompletableFuture.allOf(future1, future2, future3, future4).join();
|
|
41
|
|
|
42
|
try {
|
|
43
|
List<Map<String, Object>> employees = future1.get();
|
|
44
|
List<Map<String, Object>> departments = future2.get();
|
|
45
|
List<Map<String, Object>> departments2 = future3.get();
|
|
46
|
List<Map<String, Object>> employees2 = future4.get();
|
|
47
|
|
|
48
|
|
|
49
|
} catch (Exception e) {
|
|
50
|
e.printStackTrace();
|
|
51
|
}
|
|
52
|
}
|
|
53
|
|
|
54
|
private static List<Map<String, Object>> runStreamingQuery(String query) {
|
|
55
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
56
|
|
|
57
|
try (Connection conn = dataSource.getConnection()) {
|
|
58
|
conn.setAutoCommit(false);
|
|
59
|
|
|
60
|
try (Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
|
|
61
|
stmt.setFetchSize(50);
|
|
62
|
|
|
63
|
try (ResultSet rs = stmt.executeQuery(query)) {
|
|
64
|
ResultSetMetaData meta = rs.getMetaData();
|
|
65
|
int columnCount = meta.getColumnCount();
|
|
66
|
|
|
67
|
while (rs.next()) {
|
|
68
|
Map<String, Object> row = new HashMap<>();
|
|
69
|
for (int i = 1; i <= columnCount; i++) {
|
|
70
|
row.put(meta.getColumnLabel(i), rs.getObject(i));
|
|
71
|
}
|
|
72
|
results.add(row);
|
|
73
|
}
|
|
74
|
}
|
|
75
|
}
|
|
76
|
} catch (SQLException e) {
|
|
77
|
System.err.println("Error executing query: " + query);
|
|
78
|
e.printStackTrace();
|
|
79
|
}
|
|
80
|
|
|
81
|
return results;
|
|
82
|
}
|
|
83
|
|
|
84
|
public static void main(String[] args) {
|
|
85
|
double async = System.currentTimeMillis();
|
|
86
|
for (int i = 0; i < 500; i++) {
|
|
87
|
executeParallelQueries();
|
|
88
|
}
|
|
89
|
async = System.currentTimeMillis() - async;
|
|
90
|
System.out.println(async);
|
|
91
|
|
|
92
|
double sync = System.currentTimeMillis();
|
|
93
|
for (int i = 0; i < 500; i++) {
|
|
94
|
executeQueriesSequentially();
|
|
95
|
}
|
|
96
|
|
|
97
|
sync = System.currentTimeMillis() - sync;
|
|
98
|
System.out.println(sync);
|
|
99
|
|
|
100
|
System.out.println((sync - async) / sync * 100);
|
|
101
|
|
|
102
|
}
|
|
103
|
|
|
104
|
|
|
105
|
public static void executeQueriesSequentially() {
|
|
106
|
List<Map<String, Object>> employees = runQueryNoStreaming("SELECT *, pg_sleep(0.001) FROM employees e WHERE e.id = 7001;");
|
|
107
|
|
|
108
|
List<Map<String, Object>> employees2 = runQueryNoStreaming("SELECT *, pg_sleep(0.001) FROM employees e WHERE e.id = 7001;");
|
|
109
|
|
|
110
|
List<Map<String, Object>> departments = runQueryNoStreaming("SELECT *, pg_sleep(0.001) FROM departments d WHERE d.id = 343");
|
|
111
|
List<Map<String, Object>> departments2 = runQueryNoStreaming("SELECT *, pg_sleep(0.001) FROM departments d WHERE d.id = 343");
|
|
112
|
|
|
113
|
}
|
|
114
|
|
|
115
|
private static List<Map<String, Object>> runQueryNoStreaming(String query) {
|
|
116
|
List<Map<String, Object>> results = new ArrayList<>();
|
|
117
|
|
|
118
|
try (Connection conn = dataSource.getConnection()) {
|
|
119
|
conn.setAutoCommit(true);
|
|
120
|
|
|
121
|
try (Statement stmt = conn.createStatement()) {
|
|
122
|
|
|
123
|
try (ResultSet rs = stmt.executeQuery(query)) {
|
|
124
|
ResultSetMetaData meta = rs.getMetaData();
|
|
125
|
int columnCount = meta.getColumnCount();
|
|
126
|
|
|
127
|
while (rs.next()) {
|
|
128
|
Map<String, Object> row = new HashMap<>();
|
|
129
|
for (int i = 1; i <= columnCount; i++) {
|
|
130
|
row.put(meta.getColumnLabel(i), rs.getObject(i));
|
|
131
|
}
|
|
132
|
results.add(row);
|
|
133
|
}
|
|
134
|
}
|
|
135
|
}
|
|
136
|
} catch (SQLException e) {
|
|
137
|
System.err.println("Error executing query: " + query);
|
|
138
|
e.printStackTrace();
|
|
139
|
}
|
|
140
|
|
|
141
|
return results;
|
|
142
|
}
|
|
143
|
}
|