git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: a560c11794c6a4a7ea60a385f2aba1532a51d0af

Files: a560c11794c6a4a7ea60a385f2aba1532a51d0af / benchmark / index.js

2350 bytesRaw
1const {Message, ModuleRef} = require('primea-objects')
2const Hypervisor = require('../')
3
4const level = require('level-browserify')
5const RadixTree = require('dfinity-radix-tree')
6const db = level('./testdb')
7
8class 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
28async 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
90main(1000, 10)
91

Built with git-ssb-web