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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*! Loates is an simple and easy load testing framework for Rust,
inspired by [grafana k6](https://k6.io) and powered by
[`tracing`](https://docs.rs/tracing/latest/tracing/) crate.
Although this framework covers common usecases like HTTP load testing,
it is usecase agnostic. Metrics for a test is generated via use of
tracing span and events inside of a user call, more about this is documented
in section [generating custom metrics](#emitting-metrics). These spans and
events are collected via a tracing subscriber.
# Concepts
Load testing is a process where an application is subjected to a specific
amount of simulated load to evaluate its performance and reliability.
By simulating multiple users accessing the application simultaneously, developers can identify potential bottlenecks, optimize performance, and
ensure the system can handle expected traffic.
* *Virtual Users* - Implementaion of User trait defines how to simulate this interaction with your application in ways similar to a real users. For more detail explaination look at [`User`](user::User).
Users of this framework are suppose to implement [`User`](user::User) trait for each variant of user along with their user builder function.
* *User Builder* - A user builder function defines how to create a User instance. A User can borrow shared data from a [RuntimeDataStore](data::RuntimeDataStore).
* *Datastores* - Each execution creates a [RuntimeDataStore](data::RuntimeDataStore) before it begins execution, which is used to hold shared data that will be used by User.
* *Executors* - Executors control how loates will allocate Users and perform iterations. Look at Executor docs for more details.
* *Scenario* - Scenario is grouping of one or more executors. Scenarios are ran sequentially in order that they are defined.
# Example
More examples are available in the [github repo](https://github.com/trueleo/loates/examples)
```no_run
use std::time::Duration;
use loates::client::reqwest::Client;
use loates::prelude::*;
struct MyUser<Iter> {
client: Client,
post_content: Iter,
}
impl<'a, Iter> User for MyUser<Iter>
where
Iter: Iterator<Item = &'a String> + Send,
{
async fn call(&mut self) -> UserResult {
// In each iteration get the next string
let body = self.post_content.next().unwrap().to_string();
let res = self
.client
.post("https://httpbin.org/anything")
.body(body)
.send()
.await?;
tokio::time::sleep(Duration::from_millis(500)).await;
Ok(())
}
}
async fn datastore(store: &mut RuntimeDataStore) {
let data = vec!["a".to_string(), "b".to_string(), "c".to_string()];
store.insert(data);
store.insert(Client::new());
}
async fn user_builder(runtime: &RuntimeDataStore) -> impl User + '_ {
let client: &Client = runtime.get().unwrap();
let content: &Vec<String> = runtime.get().unwrap();
MyUser {
client: client.clone(),
post_content: content.iter().cycle(),
}
}
#[tokio::main]
async fn main() {
let exec = Execution::builder()
.with_user_builder(user_builder)
.with_data(datastore)
.with_executor(Executor::Once);
let scenario1 = Scenario::new("scene1", exec);
let scenarios = vec![scenario1];
Runner::new(scenarios).enable_web(true).run().await.unwrap();
}
```
# Features
- `tui` Enables tui mode, allowing for user to look at live feed of execution in terminal.
- `web` Enables web mode which contains a simple axum server along with a inbuilt UI for looking at updates.
- `serde` - Enable serialization with serde.
- `reqwest` - Wrapper client type for reqwest.
# Architecture
```text
┌───────────────────────────────────────────────┐
│ Runner │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ scene1 │ │ scene2 │ │ scene3 │ .... │
logical │ └──────────┘ └──────────┘ └──────────┘ │
┌─────────────────────────┐ │ ▼ │
│Scenarios │ │ ┌───────────────────┬───────────┐ ┌───────────┤
│ - Scenario │ │ │ Executor │ datastore │ │ │
│ - Executors ├──────►│ │ └─────▲─────┤ │ │
│ - UserBuilder │ run │ │ ┌───────┬────────┬┘ │ │ │
│ - DataStoreModifiers │ │ ├───────┴──┬────┴─────┬──┴──────┤ │ ..... │
└─────────────────────────┘ │ │ │ │ │ │ │
│ │ user │ user │ user │ │ │
│ │ │ │ │ │ │
│ └──────────┴──────────┴─────────┘ └───────────┤
└───────────────────────────────────────────────┘
traces,│
events │ ┌────────────┐ ┌─────────┐ ┌────┬───────┐
└──►│ TRACING ├────────►│ APP │ │ UI ├───────┤
│ SUBSCRIBER │ message │ STATE ├──────► │ │METRICS│
└────────────┘ └─────────┘ │ │ │
└────┴───────┘
```
# Tracing
Loates relies heavily on usage of tracing and tracing-subscriber to emit and
collect flow of execution. Even the metrics that show up when a test
runs is generated through crafted spans and events.
## Emitting metrics
To emit a custom metric from within a user task, use [`event`](https://docs.rs/tracing/0.1.40/tracing/index.html#events-1) macro.
* The event name must be followed by a `.` dot and a metric type.
* `target` for this event must to set to the constant [`USER_TASK`]
* `value` field contains the value that you want to record.
any other fields in the event is captures as the attributes for this metric which also includes all parent span's attributes.
```no_run
event!(name: "failure.counter", target: USER_TASK, Level::INFO, value = 1u64);
```
There are three type of event signals that you can emit from within a user's task.
* `counter` - Sums all values emitted during a run and shows a counter. only accepts `u64`
* `gauge` - Shows timeseries value over fixed sample range as graph. Permitted types are `u64`, `i64`, `f64` or Durations as nanos (`u128`)
* `histogram` - Captures *p50*, *p90*, *p95*, *p99* values from sampled values. Permitted types are `f64` or Duration as nanos (`u128`)
Any span(s) inside of a user task is converted to a histogram metric which would track duration of its execution as its value.
*/
/// User Interfaces and state management for creating live view of execution.
/// Augmented wrapper types for popular crates.
/// Data layer for shared data
/// User error type
/// Create test scenarios
/// Running test scenarios
/// Trace execution of a test
/// Traits for creating virtual users
pub type UserResult = ;
/// Common imports for defining a test
use unbounded_channel as channel;
use UnboundedReceiver as Receiver;
use UnboundedSender as Sender;
pub use reqwest;
/// A const variable that can be used to define custom traces in your test. The tracing layer provided by this crate will ignore any events in user call that are not of this target.
pub const USER_TASK: &str = "user_event";
const CRATE_NAME: &str = env!;
const SPAN_TASK: &str = "task";
const SPAN_EXEC: &str = "execution";
const SPAN_SCENARIO: &str = "scenario";