Files: fc3efb6d1a8e5ef2e4cbd0017fa9649b221b8793 / benchmark / index.js
2711 bytesRaw
1 | const Message = require('../message.js') |
2 | const Hypervisor = require('../') |
3 | |
4 | const level = require('level-browserify') |
5 | const RadixTree = require('dfinity-radix-tree') |
6 | const db = level('./testdb') |
7 | |
8 | let numOfMsg = 0 |
9 | const messageOrder = {} |
10 | |
11 | class BenchmarkContainer { |
12 | static validate () {} |
13 | static compile () {} |
14 | static get typeId () { |
15 | return 9 |
16 | } |
17 | |
18 | static exports (m, id) { |
19 | return Object.keys(this.functions()).map(name => { |
20 | return { |
21 | name, |
22 | destId: id |
23 | } |
24 | }) |
25 | } |
26 | static instance (actor) { |
27 | return { |
28 | exports: this.functions(actor) |
29 | } |
30 | } |
31 | static functions (actor) { |
32 | return { |
33 | onMessage: function () { |
34 | const refs = [...arguments] |
35 | const ref = refs.pop() |
36 | // console.log('run queue', this.actor.inbox._queue) |
37 | // console.log('from', message._fromId.toString('hex'), 'to: ', this.actor.id.toString('hex'), this.actor.ticks, message._fromTicks) |
38 | // const last = messageOrder[actor.id.toString('hex')] |
39 | // const message = actor.currentMessage |
40 | // if (last && last > message._fromTicks) { |
41 | // console.log(last, message._fromTicks) |
42 | // // console.log(this.actor.hypervisor.scheduler.instances) |
43 | // } |
44 | // messageOrder[actor.id.toString('hex')] = message._fromTicks |
45 | numOfMsg++ |
46 | actor.incrementTicks(10) |
47 | if (ref) { |
48 | return actor.send(new Message({ |
49 | funcRef: ref, |
50 | funcArguments: refs |
51 | })) |
52 | } |
53 | } |
54 | } |
55 | } |
56 | } |
57 | |
58 | async function main (numOfActors, depth) { |
59 | const tree = new RadixTree({ |
60 | db: db |
61 | }) |
62 | |
63 | const hypervisor = new Hypervisor(tree) |
64 | hypervisor.registerContainer(BenchmarkContainer) |
65 | |
66 | const refernces = [] |
67 | let _numOfActors = numOfActors |
68 | while (_numOfActors--) { |
69 | const {exports} = await hypervisor.createActor(BenchmarkContainer.typeId) |
70 | refernces.push(exports[0]) |
71 | } |
72 | // console.log(refernces) |
73 | _numOfActors = numOfActors |
74 | let msgs = [] |
75 | while (_numOfActors--) { |
76 | let _depth = depth |
77 | const funcArguments = [] |
78 | while (_depth--) { |
79 | const r = Math.floor(Math.random() * numOfActors) |
80 | const ref = refernces[r] |
81 | funcArguments.push(ref) |
82 | } |
83 | const message = new Message({ |
84 | funcArguments, |
85 | funcRef: refernces[_numOfActors] |
86 | }) |
87 | msgs.push(message) |
88 | } |
89 | |
90 | let start = new Date() |
91 | await Promise.all(msgs.map((msg) => hypervisor.send(msg))) |
92 | console.log('done sending') |
93 | hypervisor.scheduler.on('idle', () => { |
94 | const end = new Date() - start |
95 | console.info('Execution time: %dms', end) |
96 | console.log('messages processed', numOfActors * depth + numOfActors) |
97 | console.log('messages processed', numOfMsg) |
98 | }) |
99 | } |
100 | |
101 | main(1000, 10) |
102 |
Built with git-ssb-web