Adding stress testing script.
[pazpar2-moved-to-github.git] / test / stress-test
1 #!/usr/bin/perl -w
2
3 # $Id: stress-test,v 1.1 2007-01-02 13:30:52 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 = 1;
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 (1..10) {
29             sleep(1);
30
31             if (fetch_records($session_id, $ua)) {
32                 print STDERR "Fetched...\n";
33             }
34         }
35     }
36 }
37
38
39 sub get_random_term {
40     my (@terms) = @_;
41
42     return $terms[int rand($#terms)];
43 }
44
45
46 sub check_status {
47     my ($root) = @_;
48     my ($status_node) = $root->getElementsByTagName('status');
49     my $status = $status_node->textContent;
50
51     return ($status eq 'OK');
52 }
53
54
55 sub fetch_records {
56     my ($sid, $ua) = @_;
57     my $uri = $url . '?session=' . $sid . '&command=show';
58     my $response = $ua->get($uri);
59
60     if ($response->is_success) {
61         my $root = get_dom($response);
62
63         if (check_status($root)) {
64             if ($debug) {
65                 my (@hits) = $root->getElementsByTagName('hit');
66
67                 foreach my $h (@hits) {
68                     my ($title) = $h->getElementsByTagName('title');
69                     print STDERR "Title: '", $title->textContent, "'\n";
70                 }
71             }
72
73             return 1;
74         } else {
75             die($0 . ': Unable to fetch records, wrong status');
76         }
77     } else {
78         die($0 . ': Unable to fetch records');
79     }
80 }
81     
82
83
84 sub start_search {
85     my ($sid, $ua, $term) = @_;
86     my $uri = $url . '?session=' . $sid . '&command=search&query=' . $term;
87     my $response = $ua->get($uri);
88
89     if ($response->is_success) {
90         my $root = get_dom($response);
91
92         if (check_status($root)) {
93             return 1;
94         } else {
95             die($0 . ': Unable to search');
96         }
97     } else {
98         die($0 . ': Unable to perform search');
99     }
100 }
101
102
103 sub get_dom {
104     my ($resp) = @_;
105     my $doc = $parser->parse_string($resp->content);
106
107     return $doc->documentElement();
108 }
109
110
111 sub get_session {
112     my ($ua) = @_;
113     my $response = $ua->get($url . '?command=init');
114
115     if ($response->is_success) {
116         my $root = get_dom($response);
117
118         if (check_status($root)) {
119             my ($ses_node) = $root->getElementsByTagName('session');
120             return $ses_node->textContent;
121         } else {
122             die($0 . ':Unable to get session, wrong status'); 
123         }
124     } else {
125         die($0 . ': Unable to get session id');
126     }
127 }
128