-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim_process.rb
83 lines (67 loc) · 2.03 KB
/
sim_process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'hlmodel'
require 'tcpn'
class ProcessToken < RBSim::HLModel::Process
include TCPN::TokenMethods
end
class CPU
attr_accessor :node, :performance
def initialize(node, performance)
@node, @performance = node, performance
end
end
class CPUToken < CPU
include TCPN::TokenMethods
end
class Handler
def call(*)
2345
end
end
def doproc(&block)
return block
end
model = TCPN.read 'lib/tcpn/model/application.rb'
process = ProcessToken.new(:node01)
process.on_event :data do |process, args|
handler = proc do |cpu|
4000 * args[:volume]/cpu.performance
end
process.register_event :cpu, block: handler
process.register_event :delay_for, time: 300
if args[:volume] > 10
process.register_event :data, volume: args[:volume]/10
else
constructor = proc do |process, args|
puts "===> FORKED <=="
process.register_event :delay_for, time: args
process.register_event :send_data, volume: args, type: :HTTP_REQUEST
end
process.register_event :new_process, program: :curl, constructor: constructor, constructor_args: 5000
end
process.register_event :send_data, volume: args[:volume], type: :response
end
#handler = Handler.new
handler = Proc.new { |cpu| 1000/cpu.performance } # java error if handler is set to Proc or lambda...
#handler = Proc.new { |cpu| 1000 } # java error if handler is set to Proc or lambda...
#handler = doproc do |cpu|
# 1000/cpu.performance
#end
process.register_event(:cpu, block: handler )
process.register_event(:cpu, block: handler )
process.register_event(:data, volume: 1000)
#process.register_event(:delay_for, time: 100)
model.add_marking_for 'process', process
cpu = CPUToken.new(:node01, 10)
model.add_marking_for 'CPU', cpu
sim = TCPN.sim model
sim.cb_for :transition, :after do |t, e|
puts "==> FIRING: #{e.transition} #{e.binding.map { |k, v| "#{k}: #{v}"}.join ',' }"
#puts model.marking
end
sim.cb_for :clock, :after do |t, e|
puts e.clock #if (tick += 1) % 100 == 0
end
sim.run
puts model.marking
process = sim.model.marking_for 'process'
p process