Files: fefa2cbb27cccd87fb8e2592c242dccdc7fb61b4 / benchmark / index.js
2350 bytesRaw
1 | const {Message, ModuleRef} = require('primea-objects') |
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 | class BaseContainer { |
9 | constructor (actor) { |
10 | this.actor = actor |
11 | } |
12 | onStartup () {} |
13 | static onCreation (code, id) { |
14 | const exp = {} |
15 | Object.getOwnPropertyNames(this.prototype).filter(name => name !== 'constructor').forEach(name => { |
16 | exp[name] = {} |
17 | }) |
18 | return new ModuleRef(exp, id) |
19 | } |
20 | onMessage (message) { |
21 | return this[message.funcRef.identifier[1]](...message.funcArguments) |
22 | } |
23 | static get typeId () { |
24 | return 9 |
25 | } |
26 | } |
27 | |
28 | async function main (numOfActors, depth) { |
29 | // const messageOrder = {} |
30 | let numOfMsg = 0 |
31 | class BenchmarkContainer extends BaseContainer { |
32 | main () { |
33 | const refs = [...arguments] |
34 | const ref = refs.pop() |
35 | // const last = messageOrder[this.actor.id.toString('hex')] |
36 | // const message = this.actor.currentMessage |
37 | // messageOrder[this.actor.id.toString('hex')] = message._fromTicks |
38 | numOfMsg++ |
39 | this.actor.incrementTicks(10) |
40 | if (ref) { |
41 | this.actor.send(new Message({ |
42 | funcRef: ref, |
43 | funcArguments: refs |
44 | })) |
45 | } |
46 | } |
47 | } |
48 | const tree = new RadixTree({ |
49 | db: db |
50 | }) |
51 | |
52 | const hypervisor = new Hypervisor(tree) |
53 | hypervisor.registerContainer(BenchmarkContainer) |
54 | |
55 | const refernces = [] |
56 | let _numOfActors = numOfActors |
57 | while (_numOfActors--) { |
58 | const { |
59 | module |
60 | } = hypervisor.createActor(BenchmarkContainer.typeId) |
61 | refernces.push(module.getFuncRef('main')) |
62 | } |
63 | _numOfActors = numOfActors |
64 | let msgs = [] |
65 | while (_numOfActors--) { |
66 | let _depth = depth |
67 | const funcArguments = [] |
68 | while (_depth--) { |
69 | const r = Math.floor(Math.random() * numOfActors) |
70 | const ref = refernces[r] |
71 | funcArguments.push(ref) |
72 | } |
73 | const message = new Message({ |
74 | funcArguments, |
75 | funcRef: refernces[_numOfActors] |
76 | }) |
77 | msgs.push(message) |
78 | } |
79 | |
80 | let start = new Date() |
81 | hypervisor.send(msgs) |
82 | await hypervisor.scheduler.on('idle', () => { |
83 | const end = new Date() - start |
84 | console.info('Execution time: %dms', end) |
85 | console.log('messages processed', numOfActors * depth + numOfActors) |
86 | console.log('messages processed', numOfMsg) |
87 | }) |
88 | } |
89 | |
90 | main(1000, 10) |
91 |
Built with git-ssb-web