· 7 years ago · Sep 10, 2018, 09:33 AM
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html>
2<%@ page import="java.sql.*" %>
3<%!
4/*
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19// Code de tests divers (performances, etc.)
20// A utiliser dans un serveur JEE quelconque, Java 7+
21// Dépendances : servlet-api, sqlite
22
23private String session(HttpSession session, int added) {
24 if(session.getAttribute("counter") == null)
25 session.setAttribute("counter", 0);
26 int v = (Integer) (session.getAttribute("counter"));
27 v += added;
28 session.setAttribute("counter", v);
29 return "Current value = <span id='counter'>"+v+"</span>";
30}
31
32private String connection(HttpServletRequest request, HttpSession session, int added) {
33 if(session.getAttribute("id") != null)
34 return "Already connected";
35 if(request.getParameter("pwd") != null) {
36 if(request.getParameter("pwd").equals("1234")) {
37 session.setAttribute("id", 1);
38 return "Connected";
39 } else {
40 return "Failed";
41 }
42 }
43 return "?";
44}
45
46private String slowTimer(int ms) {
47 try {
48 Thread.sleep(ms);
49 return "ok";
50 } catch(InterruptedException ex) {
51 return "Interrupted";
52 }
53}
54
55private String slowLoops(int iterations) {
56 double n = 1;
57 for(int i = 0 ; i<iterations ; i++)
58 for(int j = 0 ; j<iterations ; j++)
59 for(int k = 0 ; k<iterations ; k++)
60 for(int l = 0 ; l<iterations ; l++)
61 n = Math.cos(n);
62 return "ok ("+n+")";
63}
64
65private String slowMem(int kb) {
66 byte [][] all = new byte[kb][];
67 for(int i = 0; i<kb; i++) {
68 all[i] = new byte[1024];
69 java.util.Arrays.fill(all[i], (byte)0x01);
70 }
71 return "ok";
72}
73
74private int fiborec(int n) {
75 if(n<2)
76 return n;
77 else
78 return fiborec(n-1)+fiborec(n-2);
79}
80
81private String slowRec(int baseNumber) {
82 int res = fiborec(baseNumber);
83 return "ok ("+res+")";
84}
85
86private String jdbc(int iterations) {
87 try {
88 Class.forName("org.sqlite.JDBC");
89 Connection conn = DriverManager.getConnection("jdbc:sqlite:supertest.sqlite");
90 conn.createStatement().execute(
91 "create table if not exists numbers(num integer)"
92 );
93 PreparedStatement ps = conn.prepareStatement("insert into numbers values(?)");
94 for(int i = 0 ; i<iterations ; i++) {
95 ps.setInt(1, i);
96 ps.execute();
97 }
98 conn.close();
99 return "ok";
100 } catch(Exception ex) {
101 return "Exception BD : "+ex;
102 }
103}
104
105private static Object o1 = new Object(), o2 = new Object();
106private String deadlock(int ms) {
107 String useless = "";
108 try {
109 synchronized(o1) {
110 Thread.sleep(ms);
111 synchronized(o2) {
112 useless = "Please dont remove me at compilation";
113 }
114 }
115 Thread.sleep(ms);
116 synchronized(o2) {
117 Thread.sleep(ms);
118 synchronized(o1) {
119 useless = "Please dont remove me at compilation";
120 }
121 }
122 return "ok";
123 } catch(InterruptedException ex) {
124 return "Interrupted";
125 }
126}
127
128%>
129<%
130String message = "";
131if(request.getParameter("action")!=null) {
132 message += "Action "+request.getParameter("action")+" : ";
133 int v = 1;
134 if(request.getParameter("v")!=null) {
135 v = Integer.parseInt(request.getParameter("v"));
136 }
137 switch(request.getParameter("action")) {
138 case "session":
139 message += session(session, v);
140 break;
141 case "connection":
142 message += connection(request, session, v);
143 break;
144 case "slowtimer":
145 message += slowTimer(v);
146 break;
147 case "slowloops":
148 message += slowLoops(v);
149 break;
150 case "slowmem":
151 message += slowMem(v);
152 break;
153 case "slowrec":
154 message += slowRec(v);
155 break;
156 case "jdbc":
157 message += jdbc(v);
158 break;
159 case "deadlock":
160 message += deadlock(v);
161 break;
162 }
163}
164request.setAttribute("message", message);
165%>
166<html>
167<head>
168 <meta charset="utf-8" />
169</head>
170<body>
171 <h1>JSP de tests divers</h1>
172 <p>${message}</p>
173 <p><a href="?">Index, RAS</a></p>
174 <p><a href="?action=session&v=1">Compteur (grâce à la session)</a></p>
175 <p><form action="?action=connection" method="post">Par password : <input name="pwd"><input type="submit"></form></p>
176 <p><a href="?action=slowtimer&v=1000">Pause de 1s</a></p>
177 <p><a href="?action=slowloops&v=100">Tas de boucles (complexité O(4))</a></p>
178 <p><a href="?action=slowmem&v=1000000">Mémoire (->1 Go)</a></p>
179 <p><a href="?action=slowrec&v=44">Recursivité</a></p>
180 <p><a href="?action=jdbc&v=100">JDBC SQL (SQLite)</a></p>
181 <p><a href="?action=deadlock&v=1000">Interblocage potentiel (2 objets statiques)</a></p>
182
183<%-- 2016, M. Blanchard --%>
184</body>
185</html>