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