]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/websocket-stdio/tester.pl
LP#1777180 Websocketd gateway and test scripts
[OpenSRF.git] / src / websocket-stdio / tester.pl
1 #!/usr/bin/perl
2 # --------------------------------------------------------------------------
3 # Copyright (C) 2018 King County Library Service
4 # Bill Erickson <berickxx@gmail.com>
5
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.
10
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 # --------------------------------------------------------------------------
16 #
17 # Synopsis:
18 #
19 # $ sudo cpan Net::Async::WebSocket::Client; 
20 # $ sudo cpan IO::Async::SSL;
21 # $ time perl tester.pl wss://localhost:443/osrf-websocket-translator
22 #
23 # --------------------------------------------------------------------------
24 use strict;
25 use warnings;
26 use IO::Async::Loop;
27 use Net::Async::WebSocket::Client;
28
29 # allow the script to run easily on test VMs.
30 use IO::Socket::SSL;
31 IO::Socket::SSL::set_ctx_defaults(SSL_verify_mode => 0);
32
33 my $client;
34 my $loop;
35 my $send_batches = 1000;
36 my $batches_sent = 0;
37 my $send_wanted = 5; # per batch
38 my $send_count = 0;
39 my $recv_count = 0;
40
41 sub send_one_msg {
42         my $thread = rand();
43     $send_count++;
44
45         my $osrf_msg = <<MSG;
46 {"service":"open-ils.auth","thread":"$thread","osrf_msg":[{"__c":"osrfMessage","__p":{"threadTrace":0,"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}}]}
47 MSG
48
49         $client->send_text_frame($osrf_msg);
50     print "batch=$batches_sent sent=$send_count received=$recv_count\n";
51 }
52
53 my $on_message = sub {
54     my ($self, $frame) = @_;
55     $recv_count++;
56     print "batch=$batches_sent sent=$send_count received=$recv_count\n";
57
58     if ($send_count == $send_wanted && $send_count == $recv_count) {
59         # once every request in the current batch has received
60         # a reply, kick off a new batch.
61         send_next_batch();
62     }
63 };
64
65 sub send_next_batch {
66
67     if ($batches_sent == $send_batches) {
68         $loop->stop;
69         return;
70     }
71
72     $batches_sent++;
73     $send_count = 0;
74     $recv_count = 0;
75     for (1..$send_wanted) {
76         send_one_msg();
77     }
78 }
79
80 my $url = $ARGV[0] or die "WS URL REQUIRED\n";
81
82 $client = Net::Async::WebSocket::Client->new(on_text_frame => $on_message);
83 $loop = IO::Async::Loop->new;
84 $loop->add($client);
85 $client->connect(url => $url, on_connected => sub {send_next_batch()});
86 $loop->run;
87