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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! This crate provides the tools to benchmark code for
//! further analyzation using different tools.
//!
//! # Writers System
//!
//! This crate uses writers system to manage collected data.
//! This means that all the macros that collect data during
//! program running push this data in form of [BenchData] enums
//! to a shared storage. At the end of the program all the data is given
//! to the instances of [Writer] that are given at the initialization.
//!
//! # Examples
//!
//! Examples of using gbench basic functionality
//! ```rust
//! use gbench::{instantiate, scope, ChromeTracing};
//! fn main() {
//!     // Istantiation of the global variables
//!     // It is needed at the top of every program that uses gbench
//!     // The folder that is specified is the folder where the data
//!     // will be saved
//!     instantiate!(ChromeTracing("target/bench"));
//!     {
//!         // This macro creates a variable that starts benchmarking
//!         // on creation and saves the result on drop.
//!         // The variable name is the first argument, the scope name
//!         // is the second.
//!         scope!(sc | "Scope");
//!         for _ in 0..1_000_000 {
//!             let _a = 1 + 1;
//!         }
//!     }
//! }
//! ```
//!
//! Example of a [log!] macro use
//! ```rust
//! use gbench::{instantiate, log, scope, ChromeTracing};
//!
//! fn main() {
//!     instantiate!(ChromeTracing("target/bench"));
//!     {
//!         scope!(sc | "Scope");
//!         for _ in 0..1_000 {
//!             let a = 1 + 1;
//!             // You can log to the file with a timestamp
//!             // using log! macro
//!             log!("A = {}", a);
//!         }
//!     }
//! }
//! ```
//!
//! Example of a [count!] macro and [CsvWriter] writer use
//! ```rust
//! use gbench::{count, instantiate, scope, ChromeTracing, CsvWriter};
//!
//! fn main() {
//!     // Additionally CsvWriter will save all the counter data in
//!     // a csv table
//!     instantiate!(ChromeTracing("target/bench"), CsvWriter("target/bench"));
//!     {
//!         scope!(sc | "Scope");
//!         for i in 0..1000 {
//!             let val = i * i;
//!             // This statement writes val to field "val" of counter "Actual value"
//!             // and writes i to field "i" of counter "I"
//!             count! {
//!                 "Actual value" => {
//!                     "val" => val
//!                 },
//!                 "I" => {
//!                     "i" => i
//!                 }
//!             }
//!         }
//!     }
//! }
//! ```
//!
//! Full example
//! ```rust
//! use gbench::{instantiate, scope, ChromeTracing};
//! use std::thread;
//! fn calculate(num: f32, n: u32) -> f32 {
//!     (0..n)
//!         .fold((num, 0.0), |(x, v), _| (x + v * 0.01, v - x * 0.001))
//!         .0
//! }
//!
//! fn main() {
//!     instantiate!(ChromeTracing("target/bench"));
//!
//!     scope!(program_scope | "Program scope");
//!     // Doing the work that needs benchmarking
//!     for _ in 0..5 {
//!         scope!(main | "Main scope");
//!         // Spawning a thread to do work
//!         let thread = thread::spawn(move || {
//!             // This benchmarks the scope that it is in
//!             scope!(child | "Child");
//!             calculate(1.0, 1_500_000)
//!         });
//!         // You can organize your subtasks in scopes to
//!         // benchmark them
//!         scope!(imp | "An important task");
//!         {
//!             scope!(i1 | "Important subtask");
//!             calculate(1.0, 300_000);
//!         }
//!         {
//!             scope!(i2 | "Less important subtask");
//!             calculate(1.0, 500_000);
//!         }
//!         // If the block of code that you need to benchmark
//!         // has ended you can drop the guard if the scope
//!         // has not ended
//!         drop(imp);
//!         // Marking the start of another task
//!         scope!(join | "Joining thread");
//!         thread.join().unwrap();
//!         // This line of code is unnecessary but I like
//!         // to keep it
//!         drop(join);
//!     }
//! }
//! ```
//!
//! [log!]: macro.log.html
//! [count!]: macro.count.html
//! [CsvWriter]: struct.CsvWriter.html
//! [BenchData]: enum.BenchData.html
//! [Writer]: trait.Writer.html

mod bench;
mod global;
mod id;
mod writer;

pub use bench::Instantiator;
pub use bench::TimeScope;

pub use global::BenchData;
pub use writer::ChromeTracing;
pub use writer::CsvWriter;
pub use writer::Writer;

#[doc(hidden)]
pub use bench::_log;

#[doc(hidden)]
pub use bench::_count;

/// Benchmarks a scope of code
///
/// # Implementation
///
/// The macro expands into a [TimeScope] declaration
///
/// ```rust
/// scope!(main)
/// // expands into this
/// let main = TimeScope::new(format!("main"));
/// ```
///
/// ```rust
/// scope!(main | "A {}", 0)
/// // expands into this
/// let main = TimeScope::new(format!("A {}", 0));
/// ```
///
/// [TimeScope]: struct.TimeScope.html
///
/// # Examples
///
/// ```rust
/// // You can organize your subtasks in scopes to
/// // benchmark them
/// scope!(imp | "An important task");
///
/// {
///     scope!(i1 | "Important subtask");
///     do_work();
/// }
///
/// {
///     scope!(i2 | "Less important subtask");
///     do_other_work();
/// }
///
/// // If the block of code that you need to benchmark
/// // has ended you can drop the guard if the scope
/// // has not ended
/// drop(imp);
///
/// // rest of the scope...
/// ```
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! scope {
    ($name:ident) => {
        scope!($name | stringify!(name));
    };

    ($name:ident | $($arg:tt)*) => {
        let $name = {
            use gbench::TimeScope;
            TimeScope::new(format!($($arg)*))
        };
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! scope {
    ($name:ident) => {};

    ($name:ident|$($arg:tt)*) => {};
}

/// Instantiates the global variables for benchmark logging
///
/// This macro should be used at the top of any program using this crate.
///
/// # Implementation
///
/// This macro expands into a declaration of [Instantiator] which instantiates
/// global variables on creation and deinstantiates them on drop.
///
/// ```rust
/// instantiate!(ChromeTracing("target/bench"));
/// // expands into this
/// let __gbench_instantiator__ = Instantiator::new(vec![Box::new(ChromeTracing("target/bench"))]);
/// ```
///
/// If you need to deinstantiate global variables before variable goes out
/// of scope you can specify the variable name and then call [end] on it
/// when you need the deinstantiation.
///
/// ```rust
/// instantiate!(ginst | ChromeTracing("target/bench"));
/// // expands into this
/// let ginst = Instantiator::new(vec![Box::new(ChromeTracing("target/bench"))]);
/// ```
///
/// [Instantiator]: struct.Instantiator.html
/// [end]: struct.Instantiator.html#method.end
///
/// # Examples
/// ```rust
/// use gbench::{instantiate, scope, ChromeTracing};
///
/// fn main() {
///     instantiate!(ChromeTracing("target/bench"));
///     {
///         scope!(sc | "Scope");
///
///         for _ in 0..1_000_000 {
///             let _a = 1 + 1;
///         }
///     }
/// }
/// ```
/// or using [end]
/// ```rust
/// use gbench::{instantiate, scope, ChromeTracing};
///
/// fn main() {
///     instantiate!(ginst | ChromeTracing("target/bench"));
///     {
///         scope!(sc | "Scope");
///
///         for _ in 0..1_000_000 {
///             let _a = 1 + 1;
///         }
///     }
///     ginst.end();
/// }
/// ```
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! instantiate {
    ($name: ident | $($writer: expr),*) => {
        let mut $name = {
            use gbench::Instantiator;

            let mut writers = std::vec::Vec::new();

            $(
                writers.push(std::boxed::Box::new($writer) as std::boxed::Box<dyn gbench::Writer + 'static>);
            )*

            Instantiator::new(writers)
        };
    };

    ($($writer: expr),*) => {
        instantiate!(__global_instantiator__ | $($writer),*);
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! instantiate {
    ($folder: expr) => {};
}

/// Logs data to a benchmarking file
///
/// ```rust
/// let a = 0;
/// log!("A: {}", a);
/// ```
/// will queue this [BenchData]
/// ```
/// Log {
///     log: "A: 0",
///     ts: /* event's timestamp */,
///     tid: /* event's thread of execution */,
/// }
/// ```
///
/// [BenchData]: enum.BenchData.html
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! log {
    ($($arg:tt)*) => {
        {
            use gbench::_log as log;
            let log_string = format!($($arg)*);
            log(log_string);
        }
    };
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! log {
    ($($arg:tt)*) => {};
}

/// Creates a counting event
///
/// ```rust
/// let i = 10;
/// count!(
///     "a" => {
///         "a" => i,
///         "b" => i / 2
///     },
///     "b" => {
///         "c" => i % 3
///     }
/// );
/// ```
///
/// Will queue these [BenchData]
///
/// ```
/// Count {
///     name: "a",
///     ts: /* event's timestamp */,
///     tid: /* event's thread of execution */,
///     data: [
///         (
///             "a",
///             10.0,
///         ),
///         (
///             "b",
///             5.0,
///         ),
///     ],
/// },
/// Count {
///     name: "b",
///     ts: /* event's timestamp */,
///     tid: /* event's thread of execution */,
///     data: [
///         (
///             "c",
///             1.0,
///         ),
///     ],
/// },
/// ```
///
/// [BenchData]: enum.BenchData.html
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! count {
    ($($name: expr => {$($argname: expr => $val:expr),*}),*) => {{
        use gbench::_count as count;

        $(
            let cname = std::string::String::from($name);

            let mut data = std::vec::Vec::new();

            $(
                data.push((std::string::String::from($argname), $val as f32));
            )*

            count(cname, data);
        )*
    }};
}

#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! count {
    ($name: ident => {$($argname: ident => $val: expr),*}) => {};
}