-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_single_actor_single_thread.rs
More file actions
101 lines (88 loc) · 2.57 KB
/
Copy pathbenchmark_single_actor_single_thread.rs
File metadata and controls
101 lines (88 loc) · 2.57 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::error::Error;
use std::process::exit;
use std::time::{Duration, Instant};
use tyra::prelude::*;
struct MessageA {}
impl ActorMessage for MessageA {}
struct Benchmark {
total_msgs: usize,
name: String,
count: usize,
start: Instant,
}
struct BenchmarkFactory {
total_msgs: usize,
name: String,
}
impl ActorFactory<Benchmark> for BenchmarkFactory {
fn new_actor(&mut self, context: ActorContext<Benchmark>) -> Result<Benchmark, Box<dyn Error>> {
return Ok(Benchmark::new(self.total_msgs, self.name.clone(), context));
}
}
impl Benchmark {
pub fn new(total_msgs: usize, name: String, _context: ActorContext<Self>) -> Self {
Self {
total_msgs,
name,
count: 0,
start: Instant::now(),
}
}
}
impl Actor for Benchmark {}
impl Handler<MessageA> for Benchmark {
fn handle(
&mut self,
_msg: MessageA,
context: &ActorContext<Self>,
) -> Result<ActorResult, Box<dyn Error>> {
if self.count == 0 {
println!("START PROCESSING!");
self.start = Instant::now();
}
self.count += 1;
let wip_print = self.total_msgs / 10;
if self.count % wip_print == 0 {
println!("{} Counter: {}", self.name, self.count)
}
if self.count % self.total_msgs == 0 {
let duration = self.start.elapsed();
println!(
"{} It took {:?} to process {} messages",
self.name, duration, self.total_msgs
);
}
if self.count == self.total_msgs {
context.system.stop(Duration::from_secs(60));
}
Ok(ActorResult::Ok)
}
}
fn main() {
let mut actor_config = TyraConfig::new().unwrap();
actor_config
.thread_pool
.config
.insert(String::from("default"), ThreadPoolConfig::new(1, 1, 1, 1.0));
let actor_system = ActorSystem::new(actor_config);
let message_count = 10000000;
let actor = actor_system
.builder()
.spawn(
"benchmark-single-actor",
BenchmarkFactory {
name: String::from("benchmark"),
total_msgs: message_count as usize,
},
)
.unwrap();
println!("Actors have been created");
let start = Instant::now();
for _i in 0..message_count {
let msg = MessageA {};
actor.send(msg).unwrap();
}
let duration = start.elapsed();
println!("It took {:?} to send {} messages", duration, message_count);
exit(actor_system.await_shutdown());
}