Files: c6d792221008796efd37db976b3be33485c3cb9a / benchmark / index.js
2168 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 tree = new RadixTree({ |
30 | db |
31 | }) |
32 | |
33 | let numOfMsg = 0 |
34 | |
35 | class BenchmarkContainer extends BaseContainer { |
36 | main () { |
37 | const refs = [...arguments] |
38 | const ref = refs.pop() |
39 | numOfMsg++ |
40 | this.actor.incrementTicks(10) |
41 | if (ref) { |
42 | this.actor.send(new Message({ |
43 | funcRef: ref, |
44 | funcArguments: refs |
45 | })) |
46 | } |
47 | } |
48 | } |
49 | |
50 | const hypervisor = new Hypervisor({tree}) |
51 | hypervisor.registerContainer(BenchmarkContainer) |
52 | |
53 | const refernces = [] |
54 | let _numOfActors = numOfActors |
55 | while (_numOfActors--) { |
56 | const {module} = hypervisor.createActor(BenchmarkContainer.typeId) |
57 | const funcRef = module.getFuncRef('main') |
58 | funcRef.gas = 1000 |
59 | refernces.push(funcRef) |
60 | } |
61 | _numOfActors = numOfActors |
62 | let msgs = [] |
63 | while (_numOfActors--) { |
64 | let _depth = depth |
65 | const funcArguments = [] |
66 | while (_depth--) { |
67 | const r = Math.floor(Math.random() * numOfActors) |
68 | const ref = refernces[r] |
69 | funcArguments.push(ref) |
70 | } |
71 | const message = new Message({ |
72 | funcArguments, |
73 | funcRef: refernces[_numOfActors] |
74 | }) |
75 | msgs.push(message) |
76 | } |
77 | let start = new Date() |
78 | hypervisor.send(msgs) |
79 | await hypervisor.scheduler.on('idle', () => { |
80 | const end = new Date() - start |
81 | console.info('Execution time: %dms', end) |
82 | console.log('messages processed', numOfActors * depth + numOfActors) |
83 | console.log('messages processed', numOfMsg) |
84 | }) |
85 | } |
86 | |
87 | main(1000, 10) |
88 |
Built with git-ssb-web