/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/db/TestServlet.java
ViewVC logotype

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/db/TestServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1255 - (hide annotations) (download)
Mon Apr 4 10:56:44 2011 UTC (13 years, 2 months ago) by torben
File size: 2521 byte(s)
Move database related classes to it's own package
1 torben 1255 package dk.thoerup.traininfoservice.db;
2 torben 735
3     import java.io.IOException;
4     import java.sql.SQLException;
5     import java.util.Random;
6    
7     import javax.servlet.ServletException;
8 torben 958 import javax.servlet.annotation.WebServlet;
9 torben 735 import javax.servlet.http.HttpServlet;
10     import javax.servlet.http.HttpServletRequest;
11     import javax.servlet.http.HttpServletResponse;
12    
13 torben 1255
14 torben 958 @WebServlet(urlPatterns={"/TestServlet"})
15 torben 735 public class TestServlet extends HttpServlet {
16     private static final long serialVersionUID = 1L;
17 torben 851
18     Random r = new Random();
19 torben 735
20 torben 852 //South-West corner= 54.5, 8.0
21 torben 853 //North-East corner= 57.8, 12.7
22     //Latitude span=3.3
23     //Longitude span=4.7
24 torben 851 void testFindNearest(int count) throws SQLException {
25 torben 735
26     StationDAO db = new StationDAO();
27    
28    
29 torben 736 for (int i=0; i<count; i++) {
30 torben 853 float lat = (r.nextFloat()*3.3F) + 54.5F;
31     float lng = (r.nextFloat()*4.7F) + 8.0F;
32 torben 735 db.getByLocation(lat, lng);
33     }
34    
35    
36    
37     }
38    
39 torben 736 void testFindName(int count) throws SQLException {
40 torben 735
41     StationDAO db = new StationDAO();
42    
43 torben 736 for (int i=0; i<count; i++) {
44 torben 735 char c1 = ((char)r.nextInt(26));
45     c1 += 'a';
46     char c2 = ((char)r.nextInt(26)) ;
47     c2 += 'a';
48    
49     String search = "" + c1 + c2;
50     System.out.println(search);
51    
52     db.getByName(search);
53     }
54 torben 740 }
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 torben 735
72    
73 torben 740 db.getByList(sb.toString());
74     }
75 torben 735 }
76    
77     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
78 torben 736
79    
80     String test = request.getParameter("test");
81     String strCount = request.getParameter("count");
82     int count = Integer.parseInt(strCount);
83    
84    
85 torben 735 try {
86     long start = System.currentTimeMillis();
87 torben 736
88     if ( test.equals("name") ) {
89     testFindName(count);
90     } else if ( test.equals("nearest") ) {
91     testFindNearest(count);
92 torben 740 } else if ( test.equals("favorites") ) {
93     testFindFavorites(count);
94 torben 736 } else {
95     throw new ServletException("No parameter test");
96     }
97    
98 torben 735 long stop = System.currentTimeMillis();
99    
100     long elapsed = stop-start;
101    
102 torben 736 float avg = ((float)elapsed) / count;
103 torben 735
104 torben 736 String out = "Count=" + count + "\n" +
105 torben 735 "Elapsed=" + elapsed + "\n" +
106     "Avg.=" + avg;
107    
108     response.getWriter().print( out );
109    
110    
111     } catch (Exception e) {
112     throw new ServletException(e);
113     }
114     }
115    
116     }

  ViewVC Help
Powered by ViewVC 1.1.20