summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/xpcshell/test_ext_native_messaging_perf.js
blob: 693f67ddeb6c0d68d31bd3337fff77d0c2bac9f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

XPCOMUtils.defineLazyModuleGetter(this, "MockRegistry",
                                  "resource://testing-common/MockRegistry.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                  "resource://gre/modules/osfile.jsm");

Cu.import("resource://gre/modules/Subprocess.jsm");

const MAX_ROUND_TRIP_TIME_MS = AppConstants.DEBUG || AppConstants.ASAN ? 36 : 18;
const MAX_RETRIES = 5;


const ECHO_BODY = String.raw`
  import struct
  import sys

  while True:
      rawlen = sys.stdin.read(4)
      if len(rawlen) == 0:
          sys.exit(0)

      msglen = struct.unpack('@I', rawlen)[0]
      msg = sys.stdin.read(msglen)

      sys.stdout.write(struct.pack('@I', msglen))
      sys.stdout.write(msg)
`;

const SCRIPTS = [
  {
    name: "echo",
    description: "A native app that echoes back messages it receives",
    script: ECHO_BODY.replace(/^ {2}/gm, ""),
  },
];

add_task(function* setup() {
  yield setupHosts(SCRIPTS);
});

add_task(function* test_round_trip_perf() {
  let extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.onMessage.addListener(msg => {
        if (msg != "run-tests") {
          return;
        }

        let port = browser.runtime.connectNative("echo");

        function next() {
          port.postMessage({
            "Lorem": {
              "ipsum": {
                "dolor": [
                  "sit amet",
                  "consectetur adipiscing elit",
                  "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
                ],
                "Ut enim": [
                  "ad minim veniam",
                  "quis nostrud exercitation ullamco",
                  "laboris nisi ut aliquip ex ea commodo consequat.",
                ],
                "Duis": [
                  "aute irure dolor in reprehenderit in",
                  "voluptate velit esse cillum dolore eu",
                  "fugiat nulla pariatur.",
                ],
                "Excepteur": [
                  "sint occaecat cupidatat non proident",
                  "sunt in culpa qui officia deserunt",
                  "mollit anim id est laborum.",
                ],
              },
            },
          });
        }

        const COUNT = 1000;
        let now;
        function finish() {
          let roundTripTime = (Date.now() - now) / COUNT;

          port.disconnect();
          browser.test.sendMessage("result", roundTripTime);
        }

        let count = 0;
        port.onMessage.addListener(() => {
          if (count == 0) {
            // Skip the first round, since it includes the time it takes
            // the app to start up.
            now = Date.now();
          }

          if (count++ <= COUNT) {
            next();
          } else {
            finish();
          }
        });

        next();
      });
    },
    manifest: {
      applications: {gecko: {id: ID}},
      permissions: ["nativeMessaging"],
    },
  });

  yield extension.startup();

  let roundTripTime = Infinity;
  for (let i = 0; i < MAX_RETRIES && roundTripTime > MAX_ROUND_TRIP_TIME_MS; i++) {
    extension.sendMessage("run-tests");
    roundTripTime = yield extension.awaitMessage("result");
  }

  yield extension.unload();

  ok(roundTripTime <= MAX_ROUND_TRIP_TIME_MS,
     `Expected round trip time (${roundTripTime}ms) to be less than ${MAX_ROUND_TRIP_TIME_MS}ms`);
});