Files: e7082de1f1be0792df1a00c4efb43344119a6e11 / benchmark / index.js
2182 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) { |
14 | const exp = {} |
15 | Object.getOwnPropertyNames(this.prototype).filter(name => name !== 'constructor').forEach(name => { |
16 | exp[name] = {} |
17 | }) |
18 | return { |
19 | exports: exp, |
20 | state: [] |
21 | } |
22 | } |
23 | onMessage (message) { |
24 | return this[message.funcRef.identifier[1]](...message.funcArguments) |
25 | } |
26 | static get typeId () { |
27 | return 9 |
28 | } |
29 | } |
30 | |
31 | async function main (numOfActors, depth) { |
32 | const tree = new RadixTree({ |
33 | db |
34 | }) |
35 | |
36 | let numOfMsg = 0 |
37 | |
38 | class BenchmarkContainer extends BaseContainer { |
39 | main () { |
40 | const refs = [...arguments] |
41 | const ref = refs.pop() |
42 | numOfMsg++ |
43 | this.actor.incrementTicks(10) |
44 | if (ref) { |
45 | this.actor.send(new Message({ |
46 | funcRef: ref, |
47 | funcArguments: refs |
48 | })) |
49 | } |
50 | } |
51 | } |
52 | |
53 | const hypervisor = new Hypervisor({tree, meter: false}) |
54 | hypervisor.registerModule(BenchmarkContainer) |
55 | |
56 | const refernces = [] |
57 | let _numOfActors = numOfActors |
58 | while (_numOfActors--) { |
59 | const actor = hypervisor.newActor(BenchmarkContainer) |
60 | const funcRef = actor.getFuncRef('main') |
61 | funcRef.gas = 1000 |
62 | refernces.push(funcRef) |
63 | } |
64 | _numOfActors = numOfActors |
65 | let msgs = [] |
66 | while (_numOfActors--) { |
67 | let _depth = depth |
68 | const funcArguments = [] |
69 | while (_depth--) { |
70 | const r = Math.floor(Math.random() * numOfActors) |
71 | const ref = refernces[r] |
72 | funcArguments.push(ref) |
73 | } |
74 | const message = new Message({ |
75 | funcArguments, |
76 | funcRef: refernces[_numOfActors] |
77 | }) |
78 | msgs.push(message) |
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