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 |
|
|
|
16 |
|
|
//South-West corner= 54.0, 7.0 |
17 |
|
|
//North-East corner= 58.0, 16.0 |
18 |
|
|
//Latitude span=4.0 |
19 |
|
|
//Longitude span=9.0 |
20 |
torben |
736 |
void testFindNearest(int count) throws SQLException { |
21 |
torben |
735 |
Random r = new Random(); |
22 |
|
|
|
23 |
|
|
StationDAO db = new StationDAO(); |
24 |
|
|
|
25 |
|
|
|
26 |
torben |
736 |
for (int i=0; i<count; i++) { |
27 |
torben |
735 |
float lat = (r.nextFloat()*4.0F) + 54.0F; |
28 |
|
|
float lng = (r.nextFloat()*9.0F) + 7.0F; |
29 |
|
|
db.getByLocation(lat, lng); |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
} |
35 |
|
|
|
36 |
torben |
736 |
void testFindName(int count) throws SQLException { |
37 |
torben |
735 |
Random r = new Random(); |
38 |
|
|
|
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 |
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
58 |
torben |
736 |
|
59 |
|
|
|
60 |
|
|
String test = request.getParameter("test"); |
61 |
|
|
String strCount = request.getParameter("count"); |
62 |
|
|
int count = Integer.parseInt(strCount); |
63 |
|
|
|
64 |
|
|
|
65 |
torben |
735 |
try { |
66 |
|
|
long start = System.currentTimeMillis(); |
67 |
torben |
736 |
|
68 |
|
|
if ( test.equals("name") ) { |
69 |
|
|
testFindName(count); |
70 |
|
|
} else if ( test.equals("nearest") ) { |
71 |
|
|
testFindNearest(count); |
72 |
|
|
} else { |
73 |
|
|
throw new ServletException("No parameter test"); |
74 |
|
|
} |
75 |
|
|
|
76 |
torben |
735 |
long stop = System.currentTimeMillis(); |
77 |
|
|
|
78 |
|
|
long elapsed = stop-start; |
79 |
|
|
|
80 |
torben |
736 |
float avg = ((float)elapsed) / count; |
81 |
torben |
735 |
|
82 |
torben |
736 |
String out = "Count=" + count + "\n" + |
83 |
torben |
735 |
"Elapsed=" + elapsed + "\n" + |
84 |
|
|
"Avg.=" + avg; |
85 |
|
|
|
86 |
|
|
response.getWriter().print( out ); |
87 |
|
|
|
88 |
|
|
|
89 |
|
|
} catch (Exception e) { |
90 |
|
|
throw new ServletException(e); |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
} |