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