Allow predictable sessions PAZ-1030
[pazpar2-moved-to-github.git] / test / stress-test
1 #!/usr/bin/perl -w
2
3 # $Id: stress-test,v 1.2 2007-01-02 14:11:02 sondberg Exp $
4 # -------------------------------------------------
5 # Simulate a search/retrieve session
6
7 use LWP::UserAgent;
8 use XML::LibXML;
9 use strict;
10
11 my $url = 'http://test.indexdata.dk:8001/~sondberg/pazpar2/www/search.pz2';
12 my $parser = new XML::LibXML;
13 my $ua = new LWP::UserAgent;
14 my $session_id = get_session($ua);
15 my $debug = 0;
16 my @terms = qw(water mineral computer java texas energy xml pirsig clinton
17                management environment dinosaur houston washington);
18
19
20 while (1) {
21     my $term = get_random_term(@terms);
22     
23     print STDERR "Search for: '", $term, "'\n";
24     
25     if (start_search($session_id, $ua, $term)) {
26         print STDERR "Success...\n";
27
28         foreach my $p (0..4) {
29             foreach (1..10) {
30                 sleep(1);
31                 
32                 if (fetch_records($session_id, $ua, $p * 20)) {
33                     print STDERR "Fetched...\n";
34                 }
35             }
36         }
37     }
38 }
39
40
41 sub get_random_term {
42     my (@terms) = @_;
43
44     return $terms[int rand($#terms)];
45 }
46
47
48 sub check_status {
49     my ($root) = @_;
50     my ($status_node) = $root->getElementsByTagName('status');
51     my $status = $status_node->textContent;
52
53     return ($status eq 'OK');
54 }
55
56
57 sub fetch_records {
58     my ($sid, $ua, $offset) = @_;
59     my $uri = $url . '?session=' . $sid . '&command=show&start=' . $offset;
60     my $response = $ua->get($uri);
61
62     if ($response->is_success) {
63         my $root = get_dom($response);
64
65         if (check_status($root)) {
66             if ($debug) {
67                 my (@hits) = $root->getElementsByTagName('hit');
68
69                 foreach my $h (@hits) {
70                     my ($title) = $h->getElementsByTagName('title');
71                     print STDERR "Title: '", $title->textContent, "'\n";
72                 }
73             }
74
75             return 1;
76         } else {
77             die($0 . ': Unable to fetch records, wrong status');
78         }
79     } else {
80         die($0 . ': Unable to fetch records');
81     }
82 }
83     
84
85
86 sub start_search {
87     my ($sid, $ua, $term) = @_;
88     my $uri = $url . '?session=' . $sid . '&command=search&query=' . $term;
89     my $response = $ua->get($uri);
90
91     if ($response->is_success) {
92         my $root = get_dom($response);
93
94         if (check_status($root)) {
95             return 1;
96         } else {
97             die($0 . ': Unable to search');
98         }
99     } else {
100         die($0 . ': Unable to perform search');
101     }
102 }
103
104
105 sub get_dom {
106     my ($resp) = @_;
107     my $doc = $parser->parse_string($resp->content);
108
109     return $doc->documentElement();
110 }
111
112
113 sub get_session {
114     my ($ua) = @_;
115     my $response = $ua->get($url . '?command=init');
116
117     if ($response->is_success) {
118         my $root = get_dom($response);
119
120         if (check_status($root)) {
121             my ($ses_node) = $root->getElementsByTagName('session');
122             return $ses_node->textContent;
123         } else {
124             die($0 . ':Unable to get session, wrong status'); 
125         }
126     } else {
127         die($0 . ': Unable to get session id');
128     }
129 }
130