0

I have used K6 in websocket performance test. While the content from server is compressed, and I got "�0E�e�!�56���j0&��v!�{�:�9�^�" printed in console. I used nodejs code to handle the same message from server. I got the right text.

So my question is how to decode compressed text in K6?

my K6 script is

   socket.on('message', function (data) {
      console.log(typeof (data));
      console.log(data.length)
      if (data.length < 200) {
        // I got "�0E�e�!�56���j0&��v!�{�:�9�^�" in console
        console.log(data);
      }
      // I tried to decode it got "incorrect header check"
      let text = pako.inflate(data, { to: 'string' }); 
    }

If I used the following JS script, I got the correct text to inflate it to plain text.

ws.on('message', function (data) {

        console.log('-------- begin -------');
        // I got <Buffer 1f 8b 08 00 00 00 00 00 00 00 2d 8b 4b 0a 80 20 14 45 f7 72 c7 21 f9 1b e4 6e 34 1f 14 12 49 5e 47 d1 de 33 68 7a 3e 37 f6 8c 80 c4 b5 b7 4c 4c 68 3d in console
        console.log(data);
        console.log('-------- end -------');

        let text = pako.inflate(data, {
            to: 'string'
        });
        // msg is " msg: {"id":"btcusdt","subbed":"market.btcusdt.depth.step0","ts":1525243319443,"status":"ok"} "
        console.log('msg: ' + text);
    })
wazhao
  • 106
  • 1
  • 4

1 Answers1

0

As I mentioned in the GitHub issue, it seems like pako's browserify version may have some issues when you specify { to: 'string' } in the options. Instead you should be able to do something like this:

let binText = pako.inflate(data);
let strText = String.fromCharCode.apply(null, binText);
console.log(strText);
na--
  • 1,016
  • 7
  • 9
  • when invoke pako.inflate, will get "incorrect header check". Seems like not the issue of pako, because it worked in javascript code. K6 used go lang to handle websocket message, and it returned different content with JS – wazhao May 02 '18 at 07:57
  • When I tried [this code](https://github.com/loadimpact/k6/issues/594#issuecomment-383413191), it worked. Did you try just `pako.inflate(data)`, without the `{ to: 'string' }` options? Are you using the browserify version of pako, since the node.js one almost certainly won't work (it probably depends on some nodejs-specific functionality)? – na-- May 02 '18 at 08:13
  • Yes, I used browserify version of pako. And it worked when I pako deflate a string and inflate the result to string. But when I tried to pako deflate the message of socket, it returned "incorrect header check". – wazhao May 02 '18 at 08:20
  • I have tried without {to: 'string'}, same error message – wazhao May 02 '18 at 08:21
  • Ok, so this seems like a bug in either the goja runtime or in the k6 corejs/bower libraries included in k6. I'll reopen the old github issue and I'll edit the new issue so it's for natively supporting websocket compression in k6 itself. – na-- May 02 '18 at 08:42
  • That will be great! Thanks a lot @na. Eager to use K6 in my project. – wazhao May 02 '18 at 08:46