1/*  $Id$
2
3    Part of SWI-Prolog
4
5    Author:        Jan Wielemaker
6    E-mail:        jan@swi.psy.uva.nl
7    WWW:           http://www.swi-prolog.org
8    Copyright (C): 1985-2002, University of Amsterdam
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    as published by the Free Software Foundation; either version 2
13    of the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24    As a special exception, if you link this library with other files,
25    compiled with a Free Software compiler, to produce an executable, this
26    library does not by itself cause the resulting executable to be covered
27    by the GNU General Public License. This exception does not however
28    invalidate any other reasons why the executable file might be covered by
29    the GNU General Public License.
30*/
31
32:- use_module(library('http/http_client')).
33:- use_module(library(debug)).
34:- use_module(library('http/http_sgml_plugin')).
35
36
37%	stress(+Times, +Threads, +URLOrAlias)
38%
39%	Typical use: stress(1000, 3, 1): run the test 1000 times with
40%	3 client threads on the /xml test from demo_body.pl and verify
41%	the parsed result.
42
43stress(Times, Parallel, Alias) :-
44	answer(Alias, URL, _), !,
45	stress(Times, Parallel, URL).
46stress(Times, Parallel, URL) :-
47	(   pool(pool, _)
48	->  delete_pool(pool)
49	;   true
50	),
51	create_pool(pool, Parallel),
52	stress(Times, URL),
53	wait_done(Times),
54	delete_pool(pool).
55
56wait_done(0) :- !.
57wait_done(N) :-
58	thread_get_message(done, Result),
59	put(Result), flush,
60	N1 is N - 1,
61	wait_done(N1).
62
63stress(0, _) :- !.
64stress(N, URL) :-
65	thread_send_message(pool, stress_url(URL)),
66	NN is N - 1,
67	stress(NN, URL).
68
69stress_url(URL) :-
70	thread_self(Me),
71	atom_number(N, Me),
72	(   catch(http_get(URL, X, [connection(close)]), E, true)
73	->  (   var(E)
74	    ->	(   answer(_, URL, Correct)
75		->  (   X == Correct
76		    ->  thread_send_message(done, N)
77		    ;   thread_send_message(done, !)
78		    )
79		;   thread_send_message(done, ?)
80		)
81	    ;	print_message(error, E),
82		thread_send_message(done, 'E')
83	    )
84	;   thread_send_message(done, -)
85	).
86
87:- dynamic
88	pool/2.				% name, threads
89
90create_pool(Name, N) :-
91	message_queue_create(Name),
92	findall(Id, (between(1, N, _),
93		     thread_create(worker(Name), Id, [])), Threads),
94	assert(pool(Name, Threads)).
95
96
97delete_pool(Name) :-
98	pool(Name, Threads),
99	forall(member(_, Threads), thread_send_message(Name, thread_exit(ok))),
100	forall(member(Id, Threads), thread_join(Id, _)),
101	message_queue_destroy(Name),
102	retract(pool(Name, Threads)).
103
104
105
106
107
108worker(Queue) :-
109	repeat,
110	  thread_get_message(Queue, Goal),
111	  (   catch(Goal, E, true)
112	  ->  (   var(E)
113	      ->  true
114	      ;   print_message(error, E)
115	      )
116	  ;   print_message(error, goal_failed(Goal))
117	  ),
118	fail.
119
120
121		 /*******************************
122		 *	  CORRECT ANSWERS	*
123		 *******************************/
124
125
126answer(1, 'http://localhost:3000/xml',
127       [ element(message,
128	  [],
129	  [ '\n  ',
130	    element(head,
131		    [],
132		    [ '\n  ',
133		      element(from,
134			      [],
135			      [ 'Jan Wielemaker'
136			      ]),
137		      '\n  ',
138		      element(to,
139			      [],
140			      [ 'Prolog users'
141			      ]),
142		      '\n  ',
143		      element(subject,
144			      [],
145			      [ 'The SWI-Prolog web-server'
146			      ]),
147		      '\n  '
148		    ]),
149	    '\n  ',
150	    element(body,
151		    [],
152		    [ '\n',
153		      element(p,
154			      [],
155			      [ '\nThis is the first demo of the web-server serving an XML message\n'
156			      ]),
157		      '\n  '
158		    ]),
159	    '\n'
160	  ])
161       ]).
162