Files: df84d7871808036fce1c56eaa79c0565b9b5ddb6 / benchmark / index.js
2313 bytesRaw
1 | const AbstractContainer = require('primea-abstract-container') |
2 | const Message = require('primea-message') |
3 | const Hypervisor = require('../') |
4 | |
5 | const level = require('level-browserify') |
6 | const RadixTree = require('dfinity-radix-tree') |
7 | const db = level('./testdb') |
8 | |
9 | let numOfMsg = 0 |
10 | const messageOrder = {} |
11 | |
12 | async function main (numOfActors, depth) { |
13 | class BenchmarkContainer extends AbstractContainer { |
14 | onCreation () {} |
15 | async onMessage (message) { |
16 | // console.log('run queue', this.actor.inbox._queue) |
17 | // console.log('from', message._fromId.toString('hex'), 'to: ', this.actor.id.toString('hex'), this.actor.ticks, message._fromTicks) |
18 | const last = messageOrder[this.actor.id.toString('hex')] |
19 | if (last && last > message._fromTicks) { |
20 | console.log(last, message._fromTicks) |
21 | // console.log(this.actor.hypervisor.scheduler.instances) |
22 | } else { |
23 | } |
24 | messageOrder[this.actor.id.toString('hex')] = message._fromTicks |
25 | numOfMsg++ |
26 | this.actor.incrementTicks(1) |
27 | const cap = message.caps.pop() |
28 | if (cap) { |
29 | return this.actor.send(cap, message) |
30 | } |
31 | } |
32 | static get typeId () { |
33 | return 9 |
34 | } |
35 | } |
36 | |
37 | const tree = new RadixTree({ |
38 | db: db |
39 | }) |
40 | |
41 | const hypervisor = new Hypervisor(tree) |
42 | hypervisor.registerContainer(BenchmarkContainer) |
43 | |
44 | const caps = [] |
45 | let _numOfActors = numOfActors |
46 | while (_numOfActors--) { |
47 | const cap = await hypervisor.createActor(BenchmarkContainer.typeId, new Message()) |
48 | caps.push(cap) |
49 | } |
50 | _numOfActors = numOfActors |
51 | let msgs = [] |
52 | while (_numOfActors--) { |
53 | const message = new Message() |
54 | msgs.push(message) |
55 | let _depth = depth |
56 | while (_depth--) { |
57 | const r = Math.floor(Math.random() * numOfActors) |
58 | const cap = caps[r] |
59 | message.caps.push(cap) |
60 | } |
61 | } |
62 | |
63 | let start = new Date() |
64 | // hypervisor.scheduler.lock(Buffer.from([0xff])) |
65 | msgs.forEach((msg, index) => { |
66 | hypervisor.send(caps[index], msg) |
67 | }) |
68 | console.log('done sending') |
69 | await hypervisor.scheduler.wait(Infinity) |
70 | // console.log(JSON.stringify(hypervisor.tree.root, null ,2)) |
71 | const end = new Date() - start |
72 | console.info('Execution time: %dms', end) |
73 | console.log('messages processed', numOfActors * depth + numOfActors) |
74 | console.log('messages processed', numOfMsg) |
75 | } |
76 | |
77 | main(55, 10) |
78 |
Built with git-ssb-web