git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: b9ba5b15445f78c8ddf04e0c79f6e5d9caead624

Files: b9ba5b15445f78c8ddf04e0c79f6e5d9caead624 / benchmark / index.js

2384 bytesRaw
1const Message = require('../message.js')
2const Hypervisor = require('../')
3
4const level = require('level-browserify')
5const RadixTree = require('dfinity-radix-tree')
6const db = level('./testdb')
7const {ModuleRef} = require('../systemObjects')
8
9class BaseContainer {
10 constructor (actor) {
11 this.actor = actor
12 }
13 onStartup () {}
14 static onCreation (code, id) {
15 const exp = {}
16 Object.getOwnPropertyNames(this.prototype).filter(name => name !== 'constructor').forEach(name => {
17 exp[name] = {}
18 })
19 return new ModuleRef(exp, id)
20 }
21 onMessage (message) {
22 return this[message.funcRef.identifier[1]](...message.funcArguments)
23 }
24 static get typeId () {
25 return 9
26 }
27}
28
29async function main (numOfActors, depth) {
30 // const messageOrder = {}
31 let numOfMsg = 0
32 class BenchmarkContainer extends BaseContainer {
33 main () {
34 const refs = [...arguments]
35 const ref = refs.pop()
36 // const last = messageOrder[this.actor.id.toString('hex')]
37 // const message = this.actor.currentMessage
38 // messageOrder[this.actor.id.toString('hex')] = message._fromTicks
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 const tree = new RadixTree({
50 db: db
51 })
52
53 const hypervisor = new Hypervisor(tree)
54 hypervisor.registerContainer(BenchmarkContainer)
55
56 const refernces = []
57 let _numOfActors = numOfActors
58 while (_numOfActors--) {
59 const {
60 module
61 } = hypervisor.createActor(BenchmarkContainer.typeId)
62 refernces.push(module.getFuncRef('main'))
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
81 let start = new Date()
82 hypervisor.send(msgs)
83 await hypervisor.scheduler.on('idle', () => {
84 const end = new Date() - start
85 console.info('Execution time: %dms', end)
86 console.log('messages processed', numOfActors * depth + numOfActors)
87 console.log('messages processed', numOfMsg)
88 })
89}
90
91main(1000, 10)
92

Built with git-ssb-web