1 |
package dk.thoerup.traininfoservice; |
package dk.thoerup.traininfoservice.db; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
import java.sql.SQLException; |
import java.sql.SQLException; |
5 |
import java.util.Random; |
import java.util.Random; |
6 |
|
|
7 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
8 |
|
import javax.servlet.annotation.WebServlet; |
9 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
10 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
11 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
12 |
|
|
13 |
|
|
14 |
|
@WebServlet(urlPatterns={"/TestServlet"}) |
15 |
public class TestServlet extends HttpServlet { |
public class TestServlet extends HttpServlet { |
16 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
17 |
final int COUNT = 10; |
|
18 |
|
Random r = new Random(); |
19 |
|
|
20 |
//South-West corner= 54.0, 7.0 |
//South-West corner= 54.5, 8.0 |
21 |
//North-East corner= 58.0, 16.0 |
//North-East corner= 57.8, 12.7 |
22 |
//Latitude span=4.0 |
//Latitude span=3.3 |
23 |
//Longitude span=9.0 |
//Longitude span=4.7 |
24 |
void testFindNearest() throws SQLException { |
void testFindNearest(int count) throws SQLException { |
|
Random r = new Random(); |
|
25 |
|
|
26 |
StationDAO db = new StationDAO(); |
StationDAO db = new StationDAO(); |
27 |
|
|
28 |
|
|
29 |
for (int i=0; i<COUNT; i++) { |
for (int i=0; i<count; i++) { |
30 |
float lat = (r.nextFloat()*4.0F) + 54.0F; |
float lat = (r.nextFloat()*3.3F) + 54.5F; |
31 |
float lng = (r.nextFloat()*9.0F) + 7.0F; |
float lng = (r.nextFloat()*4.7F) + 8.0F; |
32 |
db.getByLocation(lat, lng); |
db.getByLocation(lat, lng); |
33 |
} |
} |
34 |
|
|
36 |
|
|
37 |
} |
} |
38 |
|
|
39 |
void testFindName() throws SQLException { |
void testFindName(int count) throws SQLException { |
|
Random r = new Random(); |
|
40 |
|
|
41 |
StationDAO db = new StationDAO(); |
StationDAO db = new StationDAO(); |
42 |
|
|
43 |
for (int i=0; i<COUNT; i++) { |
for (int i=0; i<count; i++) { |
44 |
char c1 = ((char)r.nextInt(26)); |
char c1 = ((char)r.nextInt(26)); |
45 |
c1 += 'a'; |
c1 += 'a'; |
46 |
char c2 = ((char)r.nextInt(26)) ; |
char c2 = ((char)r.nextInt(26)) ; |
51 |
|
|
52 |
db.getByName(search); |
db.getByName(search); |
53 |
} |
} |
54 |
|
} |
55 |
|
|
56 |
|
void testFindFavorites(int count) throws SQLException { |
57 |
|
|
58 |
|
StationDAO db = new StationDAO(); |
59 |
|
|
60 |
|
for (int i=0; i<count; i++) { |
61 |
|
final int MAX = 8; |
62 |
|
|
63 |
|
StringBuilder sb = new StringBuilder(); |
64 |
|
sb.append('('); |
65 |
|
for (int j=0; j<MAX;j++) { |
66 |
|
if (j>0) |
67 |
|
sb.append(','); |
68 |
|
sb.append(r.nextInt(400)); |
69 |
|
} |
70 |
|
sb.append(')'); |
71 |
|
|
72 |
|
|
73 |
|
db.getByList(sb.toString()); |
74 |
|
} |
75 |
} |
} |
76 |
|
|
77 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
78 |
|
|
79 |
|
|
80 |
|
String test = request.getParameter("test"); |
81 |
|
String strCount = request.getParameter("count"); |
82 |
|
int count = Integer.parseInt(strCount); |
83 |
|
|
84 |
|
|
85 |
try { |
try { |
86 |
long start = System.currentTimeMillis(); |
long start = System.currentTimeMillis(); |
87 |
testFindName(); |
|
88 |
|
if ( test.equals("name") ) { |
89 |
|
testFindName(count); |
90 |
|
} else if ( test.equals("nearest") ) { |
91 |
|
testFindNearest(count); |
92 |
|
} else if ( test.equals("favorites") ) { |
93 |
|
testFindFavorites(count); |
94 |
|
} else { |
95 |
|
throw new ServletException("No parameter test"); |
96 |
|
} |
97 |
|
|
98 |
long stop = System.currentTimeMillis(); |
long stop = System.currentTimeMillis(); |
99 |
|
|
100 |
long elapsed = stop-start; |
long elapsed = stop-start; |
101 |
|
|
102 |
float avg = ((float)elapsed) / COUNT; |
float avg = ((float)elapsed) / count; |
103 |
|
|
104 |
String out = "Count=" + COUNT + "\n" + |
String out = "Count=" + count + "\n" + |
105 |
"Elapsed=" + elapsed + "\n" + |
"Elapsed=" + elapsed + "\n" + |
106 |
"Avg.=" + avg; |
"Avg.=" + avg; |
107 |
|
|