Skip to content

Commit

Permalink
chore: whitespace cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thevilledev committed Nov 6, 2024
1 parent 2eda193 commit 4e99633
Showing 1 changed file with 114 additions and 123 deletions.
237 changes: 114 additions & 123 deletions tests/metrics_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,131 +4,122 @@ use tokio::time::Duration;

use single_page_web_server_rs::metrics::Metrics;

#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
use std::thread;

#[test]
fn test_metrics_recording() {
let metrics = Arc::new(Metrics::new());

// Simulate multiple requests
for _ in 0..5 {
metrics.record_request("GET");
thread::sleep(Duration::from_millis(10)); // Simulate some work
metrics.record_response("GET", 200, std::time::Instant::now());
}

for _ in 0..3 {
metrics.record_request("POST");
thread::sleep(Duration::from_millis(10)); // Simulate some work
metrics.record_response("POST", 404, std::time::Instant::now());
}

// Add a small delay to allow metrics collection
thread::sleep(Duration::from_millis(100));

// Force a metrics collection
metrics.collect_metrics();

// Gather Prometheus metrics
let metric_families = metrics.get_metrics();

//panic!("{:?}", metric_families);

// Helper function to find metric by name
let find_metric = |name: &str| {
metric_families.iter()
.find(|m| m.get_name() == name)
.unwrap_or_else(|| panic!("Metric {name} not found"))
};

// Verify request counts
let requests_total = find_metric("http_requests_total");
let get_requests = requests_total.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "GET"))
.expect("GET requests not found");
let post_requests = requests_total.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "POST"))
.expect("POST requests not found");

assert_eq!(get_requests.get_counter().get_value() as i64, 5);
assert_eq!(post_requests.get_counter().get_value() as i64, 3);

// Verify in-flight requests (should be 0 after all requests completed)
let requests_in_flight = find_metric("http_requests_in_flight");
for metric in requests_in_flight.get_metric() {
assert_eq!(metric.get_gauge().get_value() as i64, 0);
}

// Verify duration histogram
let duration = find_metric("http_request_duration_seconds");
let get_200_duration = duration.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "GET") &&
m.get_label().iter().any(|l| l.get_value() == "200"))
.expect("GET 200 duration not found");
let post_404_duration = duration.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "POST") &&
m.get_label().iter().any(|l| l.get_value() == "404"))
.expect("POST 404 duration not found");

assert!(get_200_duration.get_histogram().get_sample_count() == 5);
assert!(post_404_duration.get_histogram().get_sample_count() == 3);

#[test]
fn test_metrics_recording() {
let metrics = Arc::new(Metrics::new());

// Simulate multiple requests
for _ in 0..5 {
metrics.record_request("GET");
thread::sleep(Duration::from_millis(10)); // Simulate some work
metrics.record_response("GET", 200, std::time::Instant::now());
}

#[test]
fn test_concurrent_requests() {
use std::thread;

let metrics = Arc::new(Metrics::new());
let mut handles = vec![];

// Spawn 10 threads making concurrent requests
for i in 0..10 {
let metrics = metrics.clone();
let handle = thread::spawn(move || {
let method = if i % 2 == 0 { "GET" } else { "POST" };
metrics.record_request(method);
thread::sleep(Duration::from_millis(5));
metrics.record_response(method, 200, std::time::Instant::now());
});
handles.push(handle);
}

// Wait for all threads to complete
for handle in handles {
handle.join().unwrap();
}

// Add delay and force collection after all threads complete
thread::sleep(Duration::from_millis(100));
metrics.collect_metrics();

// Verify total request count
let metric_families = metrics.get_metrics();
let requests_total = metric_families.iter()
.find(|m| m.get_name() == "http_requests_total")
.unwrap();

let total_requests: u64 = requests_total.get_metric().iter()
.map(|m| m.get_counter().get_value() as u64)
.sum();

assert_eq!(total_requests, 10);

// Verify no requests are in flight
let requests_in_flight = metric_families.iter()
.find(|m| m.get_name() == "http_requests_in_flight")
.unwrap();

let total_in_flight: i64 = requests_in_flight.get_metric().iter()
.map(|m| m.get_gauge().get_value() as i64)
.sum();

assert_eq!(total_in_flight, 0);
for _ in 0..3 {
metrics.record_request("POST");
thread::sleep(Duration::from_millis(10)); // Simulate some work
metrics.record_response("POST", 404, std::time::Instant::now());
}

// Add a small delay to allow metrics collection
thread::sleep(Duration::from_millis(100));

// Force a metrics collection
metrics.collect_metrics();

// Gather Prometheus metrics
let metric_families = metrics.get_metrics();

// Helper function to find metric by name
let find_metric = |name: &str| {
metric_families.iter()
.find(|m| m.get_name() == name)
.unwrap_or_else(|| panic!("Metric {name} not found"))
};

// Verify request counts
let requests_total = find_metric("http_requests_total");
let get_requests = requests_total.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "GET"))
.expect("GET requests not found");
let post_requests = requests_total.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "POST"))
.expect("POST requests not found");

assert_eq!(get_requests.get_counter().get_value() as i64, 5);
assert_eq!(post_requests.get_counter().get_value() as i64, 3);

// Verify in-flight requests (should be 0 after all requests completed)
let requests_in_flight = find_metric("http_requests_in_flight");
for metric in requests_in_flight.get_metric() {
assert_eq!(metric.get_gauge().get_value() as i64, 0);
}

// Verify duration histogram
let duration = find_metric("http_request_duration_seconds");
let get_200_duration = duration.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "GET") &&
m.get_label().iter().any(|l| l.get_value() == "200"))
.expect("GET 200 duration not found");
let post_404_duration = duration.get_metric().iter()
.find(|m| m.get_label().iter().any(|l| l.get_value() == "POST") &&
m.get_label().iter().any(|l| l.get_value() == "404"))
.expect("POST 404 duration not found");

assert!(get_200_duration.get_histogram().get_sample_count() == 5);
assert!(post_404_duration.get_histogram().get_sample_count() == 3);
}

#[test]
fn test_concurrent_requests() {

let metrics = Arc::new(Metrics::new());
let mut handles = vec![];

// Spawn 10 threads making concurrent requests
for i in 0..10 {
let metrics = metrics.clone();
let handle = thread::spawn(move || {
let method = if i % 2 == 0 { "GET" } else { "POST" };
metrics.record_request(method);
thread::sleep(Duration::from_millis(5));
metrics.record_response(method, 200, std::time::Instant::now());
});
handles.push(handle);
}

// Wait for all threads to complete
for handle in handles {
handle.join().unwrap();
}

// Add delay and force collection after all threads complete
thread::sleep(Duration::from_millis(100));
metrics.collect_metrics();

// Verify total request count
let metric_families = metrics.get_metrics();
let requests_total = metric_families.iter()
.find(|m| m.get_name() == "http_requests_total")
.unwrap();

let total_requests: u64 = requests_total.get_metric().iter()
.map(|m| m.get_counter().get_value() as u64)
.sum();

assert_eq!(total_requests, 10);

// Verify no requests are in flight
let requests_in_flight = metric_families.iter()
.find(|m| m.get_name() == "http_requests_in_flight")
.unwrap();

let total_in_flight: i64 = requests_in_flight.get_metric().iter()
.map(|m| m.get_gauge().get_value() as i64)
.sum();

assert_eq!(total_in_flight, 0);
}

#[test]
Expand All @@ -145,7 +136,7 @@ fn test_metrics_iterator() {

// Use get_metrics instead of metrics_iter
let metric_families = metrics.get_metrics();

// Find specific metric
let requests_total = metric_families.iter()
.find(|m| m.get_name() == "http_requests_total")
Expand Down

0 comments on commit 4e99633

Please sign in to comment.