Skip to content

Commit d8b05b4

Browse files
committed
Backported the profiler from sqliterg
1 parent 86eacf9 commit d8b05b4

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

profiler/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
environment/*db*
2+
*.class
3+
ws4sqlite*

profiler/Profile.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.io.InputStream;
2+
import java.net.HttpURLConnection;
3+
import java.net.URL;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.ExecutorService;
8+
import java.util.concurrent.Executors;
9+
import java.util.stream.Collectors;
10+
11+
public class Profile {
12+
private static final int NUM_THREADS = 8;
13+
14+
private static byte[] JSON_BYTES;
15+
private static int JSON_LEN;
16+
17+
static {
18+
try {
19+
var JSON = Files.readAllLines(Paths.get("./request.json")).stream()
20+
.collect(Collectors.joining(""));
21+
JSON_BYTES = JSON.getBytes("utf-8");
22+
JSON_LEN = JSON_BYTES.length;
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
System.exit(1);
26+
}
27+
}
28+
29+
private static int numRequests;
30+
private static String urlToCall;
31+
32+
private static ExecutorService threadPool = Executors.newFixedThreadPool(NUM_THREADS);
33+
34+
public static void main(String[] args) throws Exception {
35+
numRequests = Integer.parseInt(args[0]);
36+
urlToCall = args[1];
37+
38+
var cdl = new CountDownLatch(numRequests);
39+
40+
var start = System.currentTimeMillis();
41+
42+
for (int i = 0; i < numRequests; i++) {
43+
threadPool.execute(() -> {
44+
performHttpRequest();
45+
cdl.countDown();
46+
});
47+
}
48+
cdl.await();
49+
50+
System.out.println((System.currentTimeMillis() - start) / 1000.0);
51+
52+
threadPool.shutdown();
53+
}
54+
55+
private static void performHttpRequest() {
56+
try {
57+
var url = new URL(urlToCall);
58+
var connection = (HttpURLConnection) url.openConnection();
59+
connection.setRequestMethod("POST");
60+
connection.setRequestProperty("Content-Type", "application/json"); // Set Content-Type header
61+
connection.setDoOutput(true);
62+
63+
try (var os = connection.getOutputStream()) {
64+
os.write(JSON_BYTES, 0, JSON_LEN);
65+
}
66+
67+
var ret = connection.getResponseCode();
68+
if (ret != 200) {
69+
try (InputStream errorStream = connection.getErrorStream()) {
70+
byte[] buffer = new byte[1024];
71+
int bytesRead;
72+
while ((bytesRead = errorStream.read(buffer)) != -1) {
73+
System.err.write(buffer, 0, bytesRead);
74+
}
75+
}
76+
try (InputStream errorStream = connection.getInputStream()) {
77+
byte[] buffer = new byte[1024];
78+
int bytesRead;
79+
while ((bytesRead = errorStream.read(buffer)) != -1) {
80+
System.err.write(buffer, 0, bytesRead);
81+
}
82+
}
83+
}
84+
connection.disconnect();
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
auth:
2+
mode: INLINE
3+
byQuery: SELECT 1 FROM AUTH WHERE USER = :user AND PASS = :password
4+
corsOrigin: "*"
5+
initStatements:
6+
- CREATE TABLE IF NOT EXISTS TBL (ID INT, VAL TEXT)
7+
- CREATE TABLE IF NOT EXISTS AUTH (USER TEXT, PASS TEXT)
8+
- DELETE FROM AUTH
9+
- INSERT INTO AUTH VALUES ('myUser', 'ciao')

profiler/request.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"credentials": {
3+
"user": "myUser",
4+
"password": "ciao"
5+
},
6+
"transaction": [
7+
{
8+
"statement": "DELETE FROM TBL"
9+
},
10+
{
11+
"query": "SELECT * FROM TBL"
12+
},
13+
{
14+
"statement": "INSERT INTO TBL (ID, VAL) VALUES (:id, :val)",
15+
"values": {
16+
"id": 0,
17+
"val": "zero"
18+
}
19+
},
20+
{
21+
"statement": "INSERT INTO TBL (ID, VAL) VALUES (:id, :val)",
22+
"valuesBatch": [
23+
{
24+
"id": 1,
25+
"val": "uno"
26+
},
27+
{
28+
"id": 2,
29+
"val": "due"
30+
}
31+
]
32+
},
33+
{
34+
"noFail": true,
35+
"statement": "INSERT INTO TBL (ID, VAL) VALUES (:id, :val, 1)",
36+
"valuesBatch": [
37+
{
38+
"id": 1,
39+
"val": "uno"
40+
},
41+
{
42+
"id": 2,
43+
"val": "due"
44+
}
45+
]
46+
},
47+
{
48+
"statement": "INSERT INTO TBL (ID, VAL) VALUES (:id, :val)",
49+
"valuesBatch": [
50+
{
51+
"id": 3,
52+
"val": "tre"
53+
}
54+
]
55+
},
56+
{
57+
"query": "SELECT * FROM TBL WHERE ID=:id",
58+
"values": {
59+
"id": 1
60+
}
61+
},
62+
{
63+
"statement": "DELETE FROM TBL"
64+
}
65+
]
66+
}

profiler/stress_ws4sqlite.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
URL="http://localhost:12321/test_ws4sqlite"
4+
REQUESTS=100000
5+
6+
cd "$(dirname "$0")"
7+
8+
rm -f environment/*.db*
9+
rm -f ws4sqlite*
10+
11+
pkill -x ws4sqlite
12+
13+
cd ..
14+
make build-nostatic
15+
cp bin/ws4sqlite profiler/
16+
cd profiler
17+
18+
./ws4sqlite --db environment/test_ws4sqlite.db &
19+
20+
javac Profile.java
21+
22+
sleep 1
23+
24+
echo -n "Elapsed seconds: "
25+
java -cp ./ Profile $REQUESTS $URL $REQ
26+
27+
rm Profile.class
28+
29+
pkill -x ws4sqlite
30+
31+
rm -f ws4sqlite*
32+
rm -f environment/*.db*

0 commit comments

Comments
 (0)