thag_profiler 1.1.0

A lightweight, cross-platform Rust code profiling toolkit with zero overhead when disabled
Documentation
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/// # Test Suite for #[`enable_profiling`] Attribute Macro
///
/// This test suite verifies that the `#[enable_profiling]` attribute macro correctly enables and disables profiling with different options:
///
/// ```bash
/// THAG_PROFILER=both,,announce cargo test --features=full_profiling --test test_enable_profiling -- --nocapture
/// ```
///
/// ## Key Areas Tested
///
/// 1. **Default Behavior**: Verifies that with no options, profiling is enabled using appropriate defaults based on feature flags.
///
/// 2. **Explicit Settings**:
///    - `yes` option: Explicitly enable profiling
///    - `no` option: Explicitly disable profiling
///    - `time` option: Enable time-only profiling
///    - `memory` option: Enable memory-only profiling (with `full_profiling` feature)
///    - `both` option: Enable both time and memory profiling (with `full_profiling` feature)
///
/// 3. **Runtime Control**:
///    - Environment variable configuration through `THAG_PROFILER`
///    - Handling valid and invalid profile types
///    - Configuration of detailed memory settings
///
/// 4. **Sequential Testing**:
///    - All tests run in a single sequence to avoid global state conflicts
///    - Tests include proper reset between test cases
///    - Complete coverage of environment variable interactions
///
/// 5. **Feature Compatibility**:
///    - Tests that adapt based on available features (`time_profiling`, `full_profiling`)
///    - Appropriate feature-specific assertions
///
/// ## Test Design Notes
///
/// - Uses a single `#[test]` function to ensure sequential execution
/// - Clears environment state between test cases
/// - Explicit check for profile type limitations based on features
/// - Handles potential panics in edge cases like invalid environment values
/// - Validates proper profiling state cleanup after each function completes
///
#[cfg(feature = "time_profiling")]
use std::env;

#[cfg(feature = "time_profiling")]
use thag_profiler::{
    ProfileConfiguration, ProfileType, end, profile,
    profiling::{
        disable_profiling, is_profiling_enabled, is_profiling_state_enabled, set_profile_config,
    },
};

// ---------------------------------------------------------------------------
// Test functions with enable_profiling attribute
// ---------------------------------------------------------------------------

/// Test with default option (yes)
#[cfg(feature = "time_profiling")]
#[thag_profiler::enable_profiling]
fn default_enabled_function() {
    assert!(is_profiling_enabled(), "Profiling should be enabled");

    // Verify the global profile type
    #[cfg(feature = "full_profiling")]
    assert_eq!(
        thag_profiler::get_global_profile_type(),
        ProfileType::Both,
        "Default global profile type should be Both with full_profiling feature"
    );

    #[cfg(all(feature = "time_profiling", not(feature = "full_profiling")))]
    assert_eq!(
        thag_profiler::get_global_profile_type(),
        ProfileType::Time,
        "Default global profile type should be Time with time_profiling feature"
    );

    // Test section profiling works when enabled
    profile!(default_section, time);
    let _ = (0..100).sum::<i32>(); // Do some work
    end!(default_section);
}

/// Test with explicit "yes" option
#[cfg(feature = "time_profiling")]
#[thag_profiler::enable_profiling(yes)]
fn yes_enabled_function() {
    assert!(
        is_profiling_enabled(),
        "Profiling should be enabled with 'yes' option"
    );

    // Test section profiling works when enabled
    profile!(yes_section, time);
    let _ = (0..100).sum::<i32>(); // Do some work
    end!(yes_section);
}

/// Test with "no" option (disabled)
#[cfg(feature = "time_profiling")]
#[thag_profiler::enable_profiling(no)]
fn no_disabled_function() {
    // Profiling should be disabled when using 'no'
    assert!(
        !is_profiling_enabled(),
        "Profiling should be disabled with 'no' option"
    );

    // Section profiling shouldn't create active profiles when disabled
    profile!(no_section, time);
    let _ = (0..100).sum::<i32>(); // Do some work
    end!(no_section);
    // No assertions needed - if profiling is incorrectly enabled, this would still work but not fail the test
}

/// Test with "time" option
#[cfg(feature = "time_profiling")]
#[thag_profiler::enable_profiling(time)]
fn time_enabled_function() {
    assert!(
        is_profiling_enabled(),
        "Profiling should be enabled with 'time' option"
    );
    assert_eq!(
        thag_profiler::get_global_profile_type(),
        ProfileType::Time,
        "Global profile type should be Time"
    );

    // Test section profiling works when enabled for time
    profile!(time_section, time);
    let _ = (0..100).sum::<i32>(); // Do some work
    end!(time_section);
}

/// Test with "memory" option
#[cfg(feature = "full_profiling")]
#[thag_profiler::enable_profiling(memory)]
fn memory_enabled_function() {
    assert!(
        is_profiling_enabled(),
        "Profiling should be enabled with 'memory' option"
    );
    assert_eq!(
        thag_profiler::get_global_profile_type(),
        ProfileType::Memory,
        "Global profile type should be Memory"
    );

    // Test section profiling works when enabled for memory
    profile!(memory_section, mem_summary);
    let data = vec![0u8; 1024]; // Allocate some memory
    let _ = data.len(); // Use data to avoid compiler optimizations
    end!(memory_section);
}

/// Test with "both" option (time and memory)
#[cfg(feature = "full_profiling")]
#[thag_profiler::enable_profiling(both)]
fn both_enabled_function() {
    assert!(
        is_profiling_enabled(),
        "Profiling should be enabled with 'both' option"
    );
    assert_eq!(
        thag_profiler::get_global_profile_type(),
        ProfileType::Both,
        "Global profile type should be Both"
    );

    // Test section profiling works for both time and memory
    profile!(both_section, both);
    let data = vec![0u8; 1024]; // Allocate some memory
    let result = (0..100).sum::<usize>(); // Do some work
    let _ = data.len() + result; // Use values to avoid compiler optimizations
    end!(both_section);
}

/// Test with "runtime" option (environment variable controlled)
#[cfg(feature = "time_profiling")]
#[thag_profiler::enable_profiling(runtime)]
fn runtime_controlled_function() {
    // This function will check the environment variable THAG_PROFILER
    // It should be enabled if the env var is set, disabled otherwise
    let env_var_exists = env::var("THAG_PROFILER").is_ok();

    assert_eq!(
        is_profiling_enabled(),
        env_var_exists,
        "Profiling enabled state should match THAG_PROFILER environment variable presence"
    );

    // If env var exists, test that profile settings match what's in the variable
    if env_var_exists {
        let env_value = env::var("THAG_PROFILER").unwrap();
        let parts: Vec<&str> = env_value.split(',').collect();

        if !parts.is_empty() {
            let profile_type = parts[0].trim();

            match profile_type {
                "time" => assert_eq!(
                    thag_profiler::get_global_profile_type(),
                    ProfileType::Time,
                    "Global profile type should be Time from env var"
                ),
                "memory" => {
                    #[cfg(feature = "full_profiling")]
                    assert_eq!(
                        thag_profiler::get_global_profile_type(),
                        ProfileType::Memory,
                        "Global profile type should be Memory from env var"
                    );

                    #[cfg(not(feature = "full_profiling"))]
                    assert_eq!(
                        thag_profiler::get_global_profile_type(),
                        ProfileType::Time,
                        "Global profile type should fall back to Time when Memory requested but full_profiling not enabled"
                    );
                }
                "both" => {
                    #[cfg(feature = "full_profiling")]
                    assert_eq!(
                        thag_profiler::get_global_profile_type(),
                        ProfileType::Both,
                        "Global profile type should be Both from env var"
                    );

                    #[cfg(not(feature = "full_profiling"))]
                    assert_eq!(
                        thag_profiler::get_global_profile_type(),
                        ProfileType::Time,
                        "Global profile type should fall back to Time when Both requested but full_profiling not enabled"
                    );
                }
                _ => {}
            }

            // Test detailed memory flag if specified in 4th position
            if parts.len() >= 4 && !parts[3].trim().is_empty() {
                let detailed_memory = parts[3].trim().parse::<bool>().unwrap_or(false);
                assert_eq!(
                    thag_profiler::is_detailed_memory(),
                    detailed_memory
                        && (profile_type == "memory" || profile_type == "both")
                        && cfg!(feature = "full_profiling"),
                    "Detailed memory setting should match env var"
                );
            }
        }
    }

    // Test section profiling if profiling is enabled
    if is_profiling_enabled() {
        profile!(runtime_section, time);
        let _ = (0..100).sum::<i32>(); // Do some work
        end!(runtime_section);
    }
}

// ---------------------------------------------------------------------------
// Main test function - follows pattern from test_profiled_behavior.rs
// ---------------------------------------------------------------------------

#[test]
#[cfg(feature = "time_profiling")]
#[allow(clippy::too_many_lines)]
fn test_enable_profiling_full_sequence() {
    use thag_profiler::profiling::{clear_profile_config_cache, set_global_profile_type};

    let max_profile_type = if cfg!(feature = "full_profiling") {
        ProfileType::Both
    } else {
        ProfileType::Time
    };

    // Ensure clean state to start
    unsafe { env::remove_var("THAG_PROFILER") };
    disable_profiling();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled at test start"
    );

    // -------------------------------------------------------------------
    // 1. Test default (yes) option
    // -------------------------------------------------------------------

    eprintln!("Testing default enabled function...");
    default_enabled_function();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );
    assert_eq!(thag_profiler::get_global_profile_type(), max_profile_type);

    // -------------------------------------------------------------------
    // 2. Test explicit yes option
    // -------------------------------------------------------------------

    eprintln!("Testing yes enabled function...");
    disable_profiling(); // Reset state before each test
    yes_enabled_function();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 3. Test no option
    // -------------------------------------------------------------------

    eprintln!("Testing no disabled function...");
    // Use an attribute-based macro approach to enable profiling first
    enable_time_profiling_for_test();
    no_disabled_function();

    // Helper function with attribute macro
    #[thag_profiler::enable_profiling(time)]
    fn enable_time_profiling_for_test() {}
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 4. Test time option
    // -------------------------------------------------------------------

    eprintln!("Testing time enabled function...");
    disable_profiling();
    set_global_profile_type(ProfileType::None);
    time_enabled_function();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 5. Test memory option (when available)
    // -------------------------------------------------------------------

    #[cfg(feature = "full_profiling")]
    {
        eprintln!("Testing memory enabled function...");
        disable_profiling();
        memory_enabled_function();
        assert!(
            !is_profiling_state_enabled(),
            "Profiling should be disabled after function"
        );
    }

    // -------------------------------------------------------------------
    // 6. Test both option (when available)
    // -------------------------------------------------------------------

    #[cfg(feature = "full_profiling")]
    {
        eprintln!("Testing both enabled function...");
        disable_profiling();
        both_enabled_function();
        assert!(
            !is_profiling_state_enabled(),
            "Profiling should be disabled after function"
        );
    }

    // -------------------------------------------------------------------
    // 7. Test runtime with no env var
    // -------------------------------------------------------------------

    eprintln!("Testing runtime with no env var...");
    unsafe { env::remove_var("THAG_PROFILER") };
    disable_profiling();
    clear_profile_config_cache();
    runtime_controlled_function();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 8. Test runtime with time profile
    // -------------------------------------------------------------------

    eprintln!("Testing runtime with time profile...");
    unsafe { env::set_var("THAG_PROFILER", "time,.,none,false") };
    disable_profiling();
    clear_profile_config_cache();
    runtime_controlled_function();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 9. Test runtime with invalid profile
    // -------------------------------------------------------------------

    eprintln!("Testing runtime with invalid profile...");
    unsafe { env::set_var("THAG_PROFILER", "invalid,.,none,false") };
    disable_profiling();
    clear_profile_config_cache();
    // This might fail if runtime_controlled_function doesn't handle invalid types well
    // Consider adding a try-catch here if necessary
    let _result = std::panic::catch_unwind(|| {
        runtime_controlled_function();
    });
    // We don't care if it panicked - just make sure we clean up
    set_profile_config(
        ProfileConfiguration::try_from(vec!["time", "", "announce"].as_slice()).unwrap(),
    );
    disable_profiling();
    assert!(
        !is_profiling_state_enabled(),
        "Profiling should be disabled after function"
    );

    // -------------------------------------------------------------------
    // 10. Test runtime option with detailed memory
    // -------------------------------------------------------------------

    #[cfg(feature = "full_profiling")]
    {
        unsafe { env::set_var("THAG_PROFILER", "both,.,none,true") };
        clear_profile_config_cache();
        runtime_controlled_function();
        assert!(!is_profiling_state_enabled());
        assert_eq!(thag_profiler::get_global_profile_type(), max_profile_type);
        assert!(thag_profiler::is_detailed_memory());
    }

    // -------------------------------------------------------------------
    // 11. Test runtime option without detailed memory
    // -------------------------------------------------------------------

    #[cfg(feature = "full_profiling")]
    {
        unsafe { env::set_var("THAG_PROFILER", "memory,.,quiet,false") };
        clear_profile_config_cache();

        // Run the function with runtime option
        runtime_controlled_function();

        // Verify profiling is disabled after function exits
        assert!(
            !is_profiling_state_enabled(),
            "Profiling should remain disabled when THAG_PROFILER is not set"
        );
        assert_eq!(
            thag_profiler::get_global_profile_type(),
            ProfileType::Memory,
            "Global profile type should be Memory when set in environment variable"
        );
        assert!(
            !thag_profiler::is_detailed_memory(),
            "Detailed memory should be disabled"
        );
    }

    // -------------------------------------------------------------------
    // 12. Test runtime option with detailed memory
    // -------------------------------------------------------------------

    #[cfg(feature = "full_profiling")]
    {
        // Set the environment variable for detailed memory profiling
        unsafe { env::set_var("THAG_PROFILER", "memory,.,announce,true") };
        clear_profile_config_cache();

        // Run the function with runtime option
        runtime_controlled_function();

        // Verify profiling is disabled after function exits
        assert!(
            !is_profiling_state_enabled(),
            "Profiling should remain disabled when THAG_PROFILER is not set"
        );
        assert_eq!(
            thag_profiler::get_global_profile_type(),
            ProfileType::Memory,
            "Global profile type should be Memory when set in environment variable"
        );
        assert!(
            thag_profiler::is_detailed_memory(),
            "Detailed memory should be enabled"
        );
    }

    // Clean up at the end
    unsafe { env::remove_var("THAG_PROFILER") };
    disable_profiling();

    println!("All enable_profiling tests completed successfully!");
}