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