00:00:11.120
hello um my name is yakob and I'm very
00:00:15.759
uh stressed of
00:00:17.880
course okay give him one more
00:00:23.359
Applause oh no no
00:00:25.720
no uh but it's not about a talk that I'm
00:00:28.240
stressed it's just I lately was made
00:00:30.359
redundant in a company as a software
00:00:33.040
engineer and I had to kind of downgrade
00:00:36.360
my standard of living so this is my new
00:00:40.360
new flat mate and we live just next to
00:00:44.160
to the petrol station where I actually
00:00:45.800
got a good job as a counter um salesman
00:00:50.440
so over there I have a manager who found
00:00:53.160
out that I have a career behind me and
00:00:55.559
he sometimes just nags me on the you
00:00:58.519
know on the chat things like I had a
00:01:01.079
dream and I'm like you
00:01:03.320
know he sees
00:01:06.119
things and and he's really
00:01:10.280
symbolic so then when I hear something
00:01:12.479
like that I immediately think think on
00:01:14.439
the behind the counter okay is that pump
00:01:16.560
working in a separate thread or in the
00:01:18.439
fiber or something but he keeps on
00:01:20.320
pushing like I want to know which pumps
00:01:23.000
we should buy and how
00:01:24.759
many and how much fuel we need to order
00:01:27.640
depending on traffic I already have n
00:01:30.360
ideas but they all seem equally awesome
00:01:33.560
could you compare them for me somehow
00:01:35.880
I'll definitely need some nice
00:01:38.560
animations and data and when we hear
00:01:41.360
about animations we are all happy as
00:01:43.280
backend developers especially in Ruby
00:01:45.520
because our framework just supportive a
00:01:48.040
lot about about animations know so but I
00:01:51.640
could take it easy we had a whole day
00:01:53.079
for so that was good it was 3:00 a.m.
00:01:57.159
and uh and yeah I thought okay why not
00:02:00.320
doing it what what what better thing I
00:02:02.039
have to do so sometime later I show him
00:02:04.079
this hey D I have a simulation for you
00:02:07.200
and I was very happy about showing him
00:02:09.759
that that it is visualized no and look
00:02:13.599
at
00:02:14.519
that I'm see showing him and he's like
00:02:17.040
what is this some biology joke or where
00:02:19.560
are the cars so I'm telling him look
00:02:22.400
this is a petrol station around and in
00:02:25.879
that big bubble there are cars waiting
00:02:28.080
in a queue which are the ones
00:02:30.519
who that are outside the orange bubble
00:02:32.720
which is about the cars being pumped or
00:02:34.840
the green bubble about the cars that
00:02:37.239
already are fuy fully
00:02:39.800
fueled so I further described to him
00:02:42.480
that you look there are cars served
00:02:44.959
because of lack of cars not served
00:02:47.040
because of lack of fuel in the black box
00:02:49.640
and the partial ones in the
00:02:53.159
brown but he was not convinced uh
00:02:56.840
definitely this is not how his dream was
00:03:00.800
uh
00:03:01.720
so but then I wanted to show him that
00:03:04.159
look it moves so we can see that
00:03:05.879
something is happening on the petrol
00:03:07.319
station no these things are actually uh
00:03:12.360
alive you just need to understand my
00:03:15.040
concept but he didn't want to understand
00:03:17.000
my concept but I show him look you can
00:03:19.239
do two simulations at the same time and
00:03:21.720
compare your uh parameters for the
00:03:25.360
petrol station and see which one do you
00:03:28.480
like more uh is this one working better
00:03:31.000
as a station or the other one is working
00:03:32.799
better as a
00:03:34.640
station and then I say you can even
00:03:36.920
compare all your nine or eight ideas and
00:03:39.680
and and see them in parallel and then
00:03:43.080
have your ideas which one is the best
00:03:45.760
for
00:03:47.560
you and from from my perspective on this
00:03:51.239
like the best where the simulation
00:03:53.200
number two and simulation number five
00:03:55.200
because they had the most cars fully
00:03:58.239
fueled in the green circles so I show
00:04:01.439
him this but
00:04:05.560
um he was not very much interested so
00:04:10.040
but let's let's compare these two ideas
00:04:12.159
like in the first one we have three
00:04:14.079
pumps of three seconds per liter so
00:04:16.160
there are faster pumps and in the other
00:04:19.000
simulation we have uh six pumps at the
00:04:21.680
rate of SE six
00:04:23.600
seconds so sometime later I show him
00:04:26.720
this look there are actually cars coming
00:04:30.000
to a station there's a top station and
00:04:32.600
the bottom station they all have price
00:04:34.280
tag on their fuel and you can see some
00:04:37.840
data um like you know the revenue and
00:04:41.720
all that so maybe now you're happy okay
00:04:46.199
because you see the cars that you didn't
00:04:47.759
see in the previous one but he says wait
00:04:50.080
wait I saw the spreadsheets simulate for
00:04:54.680
oh what is this I don't think so he said
00:04:58.360
the revenue is better for simulation 8
00:05:00.560
and seven I want 8 and seven not 5 and
00:05:03.840
two and the fueling time is better in
00:05:07.000
these and the the fueling speed is
00:05:09.240
better the waiting time is less in the
00:05:11.160
queue so I gave him finally the
00:05:13.440
simulation seven and
00:05:15.039
eight so he can again see the cars
00:05:17.720
coming they're being pumped some are
00:05:20.199
waiting in the queue the revenue uh
00:05:23.880
comes up as the cars leave the
00:05:26.880
station and yeah true to him the better
00:05:30.600
the best simulation was number eight
00:05:32.199
with the best
00:05:34.280
Revenue so he says this is it this is it
00:05:37.400
we have a green light you can go to
00:05:39.800
Saro so I said all right so how does it
00:05:43.319
work let's see some of that
00:05:46.720
um like the concept is that that we have
00:05:50.600
some scenarios written in a yo we we put
00:05:53.319
it through a through a config Runner and
00:05:55.479
for each simulation we we inject both
00:05:58.360
logger timer and a glob Rapport uh and
00:06:01.759
then this works for a simulation Runner
00:06:04.560
and we come to an end in a single
00:06:08.160
simulation we have a station that has
00:06:11.160
many pumps these pumps are consuming the
00:06:13.759
queue to which a car spaer is pushing uh
00:06:18.919
the cars or the car spaer pushes cars to
00:06:21.800
the to the asy road as we saw it because
00:06:24.199
then the the queueing is different
00:06:26.280
depending on the on the board actually
00:06:28.880
so the Que also pushes some updates to
00:06:30.919
the report we have a separate threet for
00:06:32.880
report saver and this way we get the
00:06:35.400
data in adjacent for example that can be
00:06:37.599
read again from some server to display
00:06:39.960
these D free JS uh
00:06:44.240
graphs and the aski arst like this that
00:06:46.919
there's a board that animates and
00:06:48.560
refreshes the screen and inside of a
00:06:51.479
road object we have all those Ries which
00:06:54.199
are the cars that have their positions
00:06:57.120
and at the end we get a pretty nice
00:06:59.800
effect that you can stare at as a screen
00:07:02.120
saver if you want and and be happy so so
00:07:05.840
some components let's first look at the
00:07:07.639
logger the most basic and needed
00:07:09.440
component and this is actually how this
00:07:11.039
mini project of hobby started for me so
00:07:14.879
you run and you see that in some moment
00:07:19.000
in time that is uh done B by the timer
00:07:23.840
there are some simulation parameters at
00:07:26.280
the beginning and the things that
00:07:28.000
happens the the the events that happen
00:07:30.440
no and the results at the end I was my
00:07:33.280
first thought with the simulator was how
00:07:36.440
should I position the pumps what
00:07:39.120
parameters the pump should have so so
00:07:41.160
the cars wait the least amount of
00:07:43.879
seconds and from that idea the whole the
00:07:46.199
rest kind of came up the hobby thing um
00:07:50.120
so now let's look at the
00:07:53.520
timer um so I'll show you some counter
00:07:56.479
race timer
00:08:06.400
you hear
00:08:13.440
something we don't do that all right so
00:08:16.599
this is
00:08:18.120
uh some previous ideas on an Arduino
00:08:21.440
things that I was doing a washing
00:08:23.120
machine with my uncle actually and and
00:08:26.560
but you you can see some similarities
00:08:28.520
from the from my kind of previous mental
00:08:31.199
imprints about simulators that you want
00:08:33.240
to do something that you see in graphics
00:08:36.080
but it's happening on the LED for
00:08:37.839
example and it and they seem like they
00:08:41.200
need to work in different threets but
00:08:44.120
actually with Arduino code you just have
00:08:46.240
one big loop and you need a lot of
00:08:49.440
arrays in which you put like when the
00:08:53.480
when particular output should be low or
00:08:56.480
up no or High um and and as you see
00:09:00.519
there the delay is 1,000 and that means
00:09:03.600
that we just cycle uh you know every
00:09:05.920
second we we make a
00:09:07.800
change so these are two people that that
00:09:10.839
inspired me to do stuff uh that's uh you
00:09:14.040
can see that the picture is old because
00:09:15.600
of the how laptops were air fin um so
00:09:20.320
they are my kind of uh guides on the on
00:09:22.880
the programming Journey that I had 20
00:09:24.600
years the middle is my dad here's my
00:09:26.480
uncle and that's my friend from work for
00:09:30.200
so now let's go and see how timer can be
00:09:33.360
done inside of
00:09:35.959
Ruby
00:09:38.279
um we this is a this is a basic class of
00:09:41.519
timers so you can see there's a Fred and
00:09:43.279
in that Fred we U wait particular time
00:09:47.720
and then we tick and something I would
00:09:50.160
say about J Ruby MRI and and Gil over
00:09:53.519
here um I tried to do that in J Ruby but
00:09:57.000
with J Ruby if you run that code
00:10:00.160
the Fred can get a separate parallel um
00:10:05.320
you know CPU even and just run that Fred
00:10:09.360
very fast and tick super fast so that
00:10:12.519
the other Frets they cannot uh uh keep
00:10:16.480
up with it and with the MRI if you have
00:10:19.360
if you have gild they all have to keep
00:10:21.399
up some way so so that's how it better
00:10:23.640
works with
00:10:25.079
MRI
00:10:27.120
um so we basically update Uh current
00:10:31.440
tick as the step that we have the the
00:10:34.120
the normal step would be just
00:10:36.480
one we wait the duration and the
00:10:39.959
duration is dependent on the speed of
00:10:43.720
the of the simulation that we can config
00:10:47.839
in our yaml at the
00:10:50.320
beginning there's also a helpful
00:10:52.440
function if you do simulations that are
00:10:55.160
called from stuff like car sper uh
00:10:57.959
station or pump is that you wait for a
00:11:00.519
particular tick of the timer so that you
00:11:03.680
can make some
00:11:04.880
action and this basically Waits until
00:11:09.200
the the tick has come and then and does
00:11:13.880
things okay so let's look further in the
00:11:17.079
structure how this simulation yaml can
00:11:19.839
look how we can think about configuring
00:11:21.920
our scenarios for simulations how does
00:11:24.800
the
00:11:25.519
simulation in the very basic way look
00:11:28.279
like and and uh in the simulation
00:11:32.760
Runner so that's the yo as you can
00:11:35.920
see um you define speed you define car
00:11:39.959
numbers uh cars number that that will
00:11:42.320
come to the station the fuel station
00:11:44.760
reserve the station closing
00:11:47.760
tick like how many pumps you have what's
00:11:50.519
the what's the speed range of this pump
00:11:52.480
how much the fuel cost and what's its
00:11:54.839
price so that you can uh calculate the
00:11:57.160
initial cost and uh Revenue at the end
00:12:01.519
and then you define the scenarios so for
00:12:03.959
my manager I defined the scenarios that
00:12:06.399
had two simulations in it and they were
00:12:10.399
uh just overwriting some default
00:12:12.839
settings uh in order to met meet his
00:12:17.600
expectations then you run such yo file
00:12:22.320
not maybe directly here but just an
00:12:25.120
approximate uh you have a runner first
00:12:27.880
two simulations from the simulation from
00:12:29.880
the scenario box you can display in the
00:12:31.760
ASI art if you want and the rest of them
00:12:34.639
you just run directly uh in a simulation
00:12:38.360
and I used async here just to also show
00:12:40.880
that I used interchangeably Frets and
00:12:43.160
async I use async for more top level
00:12:47.040
things like uh dispatching simulations
00:12:50.959
or making Refreshments in the ski board
00:12:54.320
but I use Frets for
00:12:56.519
CPU heavy lifting like pumping cars and
00:13:00.120
so on and Frets are good for
00:13:03.279
that um this is how the how the
00:13:07.079
simulation kind of main uh method works
00:13:11.240
that you just calculate how long it's
00:13:13.680
going to take you start the timer you
00:13:15.720
put some intro outro and you join all
00:13:18.040
the Frets which are like station Fred Q
00:13:21.279
Fred car spound or Road and Report
00:13:25.320
saver and you can see here that it's not
00:13:28.000
that every car has its own own threet
00:13:29.959
that's not needed I know that sometimes
00:13:32.320
I I like to think about uh such
00:13:35.240
simulations that
00:13:36.760
every car is like a living entity and
00:13:39.760
they deserve their own threat but they
00:13:42.199
don't you just need a car spanner that
00:13:44.720
pushes to que and the Q consumer and
00:13:47.199
that's
00:13:48.120
it um so let's take a look at the car
00:13:50.680
sper
00:13:53.360
then I did something like this random
00:13:56.199
interval
00:13:57.480
enumerator that spawn
00:13:59.600
them and pushes them either to the road
00:14:03.320
if you do askart or just directly to the
00:14:05.399
queue if you don't care about the
00:14:06.680
visuals and you just want the logger to
00:14:08.519
to give you um
00:14:10.480
idea and this random interal enumerator
00:14:14.800
like you define the car's number that
00:14:16.600
you want and you just check if the
00:14:18.880
station is done if it's not closed and
00:14:21.120
so on and this car's delay interval
00:14:25.399
range is also defined in the config file
00:14:29.160
so you P pause for as long as you define
00:14:32.720
it there and then you yield a new
00:14:35.079
builded car and you push it to the
00:14:37.560
queue so if you define this delay
00:14:40.360
interval range between two and eight
00:14:43.320
units of time uh it's going to push in
00:14:46.680
that kind of
00:14:49.800
ratio um I I've run this simulator like
00:14:52.920
you can run much more than 10 simulators
00:14:55.360
with a lot of cars and it doesn't have
00:14:58.079
much problem with the CPU load because
00:15:01.320
uh if you increase the number of cars it
00:15:03.880
does not increase number of threads
00:15:06.440
thanks to such a lazy
00:15:09.120
enumerator and as I said better not
00:15:11.440
pre-build all cars in memory because
00:15:13.399
that's not going to the program is not
00:15:15.000
going to run even if you put that amount
00:15:18.399
uh and better not make fret for each
00:15:20.320
other and this is a very sweet slide and
00:15:23.160
uh I think we can put something more
00:15:25.279
over here and uh yes so uh as I said
00:15:30.519
about the asy art before so how do we
00:15:33.160
even think about asy art sometimes no uh
00:15:37.079
let's look at the board this is how you
00:15:39.880
define a board in Ruby as you see it's
00:15:42.720
just a constant
00:15:45.160
string and uh what you saw before was
00:15:49.000
moving and this is not moving you can
00:15:50.519
see placeholders like the X's for the
00:15:53.639
top petrol station and Y for the
00:15:57.279
bottom and on the right hand side you
00:16:00.240
see oh sorry this is the the station
00:16:03.279
part but here on the this one you you
00:16:06.959
see all the placeholders for actual data
00:16:09.600
that you want to display to the user so
00:16:12.519
he can see uh or she can see what's
00:16:18.240
happening and then we can look in the
00:16:20.880
queue how does the queueing of the cars
00:16:23.440
work in some basic way but here I'll ask
00:16:26.839
you question whoever Direct ly and
00:16:29.440
intentionally used condition
00:16:32.959
variable
00:16:35.040
wow
00:16:37.639
like no
00:16:40.519
hand that's that's that's what I thought
00:16:44.759
actually so with condition variable you
00:16:47.639
can do something like easy producer and
00:16:50.440
consumer example in
00:16:53.199
Ruby um look at
00:16:56.639
that I will not much theory about the
00:17:00.560
idea of mutex here and so on so if
00:17:03.440
somebody is completely not into mutexes
00:17:05.640
then uh in the resources I show where is
00:17:07.959
the best start uh reading about about
00:17:10.880
them but in this example you can see
00:17:14.000
that we are protecting in the mutex
00:17:16.679
something that is
00:17:19.120
um putting stuff in the queue and then
00:17:24.079
after that we are signaling by the
00:17:27.199
condition variable to another Frets
00:17:29.880
these are producers we are producing
00:17:31.679
cars here and they have random time into
00:17:34.720
coming to the queue to the
00:17:37.200
station and now we have consumers and
00:17:40.679
these are the pumps and as you see
00:17:43.679
there's a lock the same
00:17:46.640
lock but it waits until the queue is not
00:17:51.720
empty and if it is empty it will put
00:17:55.000
that frad into sleep and therefore it
00:17:58.520
will unlock the mutex and if if the
00:18:01.720
producer is going to Signal then we
00:18:06.000
can go further with that thread and it
00:18:09.799
will eventually uh take stuff from the
00:18:12.880
queue and do the rest which is the Sleep
00:18:15.400
fueling time or some other uh
00:18:18.360
calculation and the result would look
00:18:20.480
like this that there are cars coming the
00:18:22.799
pump is taking them and where when all
00:18:26.120
pumps are busy the car free is first in
00:18:28.960
the queue then the fourth is coming
00:18:30.760
fifth is coming and then the car one is
00:18:34.440
pumped so the pump one is free and it
00:18:37.080
takes carfree so uh condition variable
00:18:41.159
also has another method which is called
00:18:42.960
broadcasting so it doesn't just signal
00:18:45.880
to the first uh registered threet but it
00:18:49.320
broadcasts the idea to all of
00:18:52.520
them all right so we're approaching a
00:18:54.960
summary of that
00:18:57.080
talk um some less
00:18:59.280
learned uh this is definitely a book to
00:19:02.480
read um this is how this whole simulator
00:19:05.919
is built basically I just uh you play
00:19:08.960
around with the idea that that for me
00:19:11.960
started because I like the smell of
00:19:15.559
petrol and you just pursue that dream
00:19:19.440
right
00:19:22.240
uh a a few funny things I would say it's
00:19:25.320
very hard to find out how many petrol
00:19:28.280
stations there are in Saro for
00:19:31.440
example um these kind
00:19:35.039
of data resources are usually paid and
00:19:38.559
then they're very hard to find so one
00:19:40.880
thing you can do is is go through Google
00:19:43.400
Maps and check it out or you can take
00:19:46.200
vacation in Saro and you drive with
00:19:48.400
somebody who at least will tell you that
00:19:50.559
there are 200,000 cars in s you know a
00:19:54.440
taxi driver um but he couldn't answer me
00:19:57.799
how many petrol station there are so but
00:20:01.799
some approximates are around
00:20:04.640
200 um the lessons learn if you do stuff
00:20:08.640
like this do dependency injection from
00:20:10.760
the beginning I started with making a
00:20:12.559
timer and the logger the only instance
00:20:15.400
singlle ton for whole project why not
00:20:17.960
right at least there is one thing that
00:20:20.120
should write to the log and one timer
00:20:22.840
for to rule them all but no um so always
00:20:28.280
think forward because then you'll need
00:20:30.520
to rewrite uh so dependency injection
00:20:33.559
good then system time flies that means
00:20:38.120
it canot give you that much control if I
00:20:40.080
would base my timer on some um you know
00:20:44.840
Epoch to till the nanc it doesn't give
00:20:48.039
me that much control as if I do my own
00:20:52.360
counter and sometimes Gil is when keeps
00:20:55.000
you going but then you need to be aware
00:20:56.520
that your code works well because of of
00:20:58.760
it so you need to think differently if
00:21:00.799
you want to make it really
00:21:03.280
parallel uh forget about tests when
00:21:05.559
you're making a prototype because they
00:21:07.000
will be um they will expire very fast so
00:21:10.840
just play along hobby projects makes
00:21:13.520
your production code better you need to
00:21:15.320
make your uh tiny little funny decisions
00:21:18.720
there and then naturally you're going to
00:21:20.440
bring them to to
00:21:23.240
job um do things with Ruby that you
00:21:25.919
wouldn't do at all at your job
00:21:29.120
so and this is a programmer smile when
00:21:30.960
cutting simulators I actually found
00:21:32.720
myself being so immersed in that that I
00:21:34.559
thought oh how my face going to
00:21:36.159
look like so I make a picture and I say
00:21:38.360
okay this is a good sign that I enjoy
00:21:41.000
what I
00:21:41.799
do uh and as my friend always said U
00:21:44.840
that my forehead is like a airport so I
00:21:47.320
made this for
00:21:51.080
him and follow up ideas maybe some
00:21:53.880
framework or gam for such simulations
00:21:56.240
something that if somebody comes up with
00:21:58.080
with they can reuse what I did um rap
00:22:01.760
simulators and Rea and rectors because
00:22:04.720
uh these simulators they can they can
00:22:06.600
actually run in parallel why not
00:22:10.039
um it's just inside they need that uh
00:22:14.320
kind of Gil
00:22:16.400
factor I would like to play with the
00:22:18.600
idea in other languages like go and
00:22:20.960
elixir because they're
00:22:23.360
more ibe made for it just like we saw
00:22:26.480
yesterday with this live view that look
00:22:29.159
nice uh make the internals event driven
00:22:32.039
they are not event driven right now it's
00:22:34.400
more like the one talks to this one and
00:22:37.159
the other to this one uh make a game
00:22:39.960
game from it with different boards and
00:22:42.600
levels wasm it up I be interesting to
00:22:46.400
just see it working in the browser with
00:22:48.600
that wasm uh compiler that we saw or
00:22:52.679
just leave it as it is I
00:22:55.640
forget and so these are the resources if
00:22:58.520
you're going to find this presentation
00:22:59.799
somewhere uh I say again the working
00:23:02.880
with ruby.com
00:23:04.720
wwt uh it's an old article about working
00:23:08.200
with Ruby Freds but it's still very good
00:23:11.720
intro well well fought
00:23:14.200
through um and music in the videos there
00:23:18.120
was no music as far but but maybe you
00:23:20.559
will hear some
00:23:22.039
soon I also want to thank to the P rug
00:23:25.360
Ruby woro Meetup for letting me present
00:23:28.520
learing that around April where I just
00:23:31.440
had a logger back then and I uh kind of
00:23:34.440
send it here and they liked it also so
00:23:37.720
uh I made this visualized I'm very glad
00:23:40.000
for the visuality to to do that in
00:23:42.840
Poland um so I want to thank you and at
00:23:46.320
the end show you something and please
00:23:49.360
see that the top and bottom simulations
00:23:52.240
are working with different timers this
00:23:54.200
is cool look at that
00:24:03.520
you see you can do stuff simultaneously
00:24:05.720
with different settings
00:24:09.840
completely this is s simulation speed 20
00:24:13.440
and this is simulation speed
00:24:16.480
1,000 and you can put as many cars as
00:24:18.960
you want and you're going to get pump
00:24:20.640
and pump and pump yeah and you just
00:24:23.480
check the revenue and the revenue grows
00:24:25.919
up so it's just amazing
00:24:31.200
all right I hope you enjoyed that up
00:24:34.640
thank you again