2 # --------------------------------------------------------------------------
3 # Copyright (C) 2018 King County Library Service
4 # Bill Erickson <berickxx@gmail.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 # --------------------------------------------------------------------------
19 # $ sudo cpan Net::Async::WebSocket::Client;
20 # $ sudo cpan IO::Async::SSL;
21 # $ time perl test-stateful.pl wss://localhost:443/osrf-websocket-translator
23 # --------------------------------------------------------------------------
27 use Net::Async::WebSocket::Client;
28 use OpenSRF::Utils::JSON;
31 my $batch_size = 1000;
33 # allow the script to run easily on test VMs.
35 IO::Socket::SSL::set_ctx_defaults(SSL_verify_mode => 0);
37 package StatefulBatch;
49 return bless($self, $class);
54 $self->{in_connect} = 1;
56 my $thread = $self->{thread} = rand(); # reset on connect
58 {"service":"open-ils.auth","thread":"$thread","osrf_msg":[{"__c":"osrfMessage","__p":{"threadTrace":"0","locale":"en-US","tz":"America/New_York","type":"CONNECT"}}]}
61 $self->{client}->send_text_frame($msg);
66 $self->{in_connect} = 0;
68 my $thread = $self->{thread};
70 {"service":"open-ils.auth","thread":"$thread","osrf_msg":[{"__c":"osrfMessage","__p":{"threadTrace":1,"type":"REQUEST","payload":{"__c":"osrfMethod","__p":{"method":"opensrf.system.echo","params":["EC asldi asldif asldfia sdflias dflasdif alsdif asldfias dlfiasd flasidf alsdif alsdif asldfia sldfias dlfias dflaisd flasidf lasidf alsdif asldif asldif asldif asldif asldif asldfia sldfia sdlfias dlfias dfliasd flasidf lasidf alsdif asldif alsdif asldif asldif aslidf alsdif alsidf alsdif asldif asldif asldif asldif asldif asldif alsdif alsdif alsidf alsidf alsdif asldif asldif asldfi asldfi asldif asldif asldfi asldfias ldfaisdf lasidf alsdif asldif asldfi asdlfias dHO ME"]}},"locale":"en-US","tz":"America/New_York","api_level":1}}]}
73 $self->{client}->send_text_frame($msg);
79 my $thread = $self->{thread};
81 {"service":"open-ils.auth","thread":"$thread","osrf_msg":[{"__c":"osrfMessage","__p":{"threadTrace":"2","locale":"en-US","tz":"America/New_York","type":"DISCONNECT"}}]}
83 $self->{client}->send_text_frame($msg);
88 my ($self, $frame) = @_;
90 my $msg = OpenSRF::Utils::JSON->JSON2perl($frame);
91 my $type = $msg->{osrf_msg}->[0]->{type};
93 if ($self->{in_connect}) {
94 my $msg = OpenSRF::Utils::JSON->JSON2perl($frame);
95 if ($type ne 'STATUS') {
96 die "Received unexpected message type: $type : $frame\n";
102 if ($type ne 'RESULT') {
103 die "Received unexpected message type: $type : $frame\n";
106 # disconnect messages do not return replies
107 $self->send_disconnect;
109 print "[$$] completed ".$self->{sent_count} . " of $batch_size\n";
111 if ($self->{sent_count}++ >= $batch_size) {
122 my $url = $ARGV[0] or die "WS URL REQUIRED\n";
125 for (1..$fork_count) {
128 my $tester = StatefulBatch->new;
130 $tester->{client} = Net::Async::WebSocket::Client->new(
131 on_text_frame => sub {
132 my ($client, $frame) = @_;
133 $tester->on_message($frame);
137 $tester->{loop} = IO::Async::Loop->new;
138 $tester->{loop}->add($tester->{client});
139 $tester->{client}->connect(
140 url => $url, on_connected => sub {$tester->send_connect});
141 $tester->{loop}->run;
146 # exit after all children have exited
147 while (wait() > -1) {}