wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
const rpc = require('../');
 
const client = rpc(__dirname + '/../test-worker.js', 'My Server');
test('it should work', () => {
  for (let i = 0; i < 10; i++) {
    expect(client('My Message')).toBe('sent My Message to My Server');
  }
});
 
let nativeNCFails = false;
 
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
  test('profile ' + fn.name, () => {
    try {
      rpc.configuration.fastestFunction = fn;
      const start = Date.now();
      for (let i = 0; i < 100; i++) {
        expect(client('My Message')).toBe('sent My Message to My Server');
      }
      const end = Date.now();
      console.log(fn.name + ': ' + (end - start));
    } catch (ex) {
      if (fn.name === 'nativeNC') {
        console.log(fn.name + ' fails');
        nativeNCFails = true;
        return;
      }
      throw ex;
    }
  });
});
 
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
  test('test 30MB ' + fn.name, () => {
    let result;
    try {
      rpc.configuration.fastestFunction = fn;
      result = client('big');
    } catch (ex) {
      if (fn.name === 'nativeNC' && nativeNCFails) {
        console.log(fn.name + ' fails');
        return;
      }
      throw ex;
    }
    expect(result.length).toBe(30 * 1024 * 1024, 42);
    // for (let i = 0; i < 30 * 1024 * 1024, 42; i++) {
    //   expect(result[i]).toBe(42);
    // }
  });
});
 
rpc.FUNCTION_PRIORITY.forEach((fn, i) => {
  let longMessage = '';
  for (let i = 0; i < 100000; i++) {
    longMessage += 'My Long Message Content';
  }
  test('profile large ' + fn.name, () => {
    try {
      rpc.configuration.fastestFunction = fn;
      const start = Date.now();
      for (let i = 0; i < 10; i++) {
        expect(client(longMessage)).toBe(`sent ${longMessage} to My Server`);
      }
      const end = Date.now();
      console.log('large ' + fn.name + ': ' + (end - start));
    } catch (ex) {
      if (fn.name === 'nativeNC' && nativeNCFails) {
        console.log('large ' + fn.name + ' fails');
        return;
      }
      throw ex;
    }
  });
});