Skip to content

Commit 5e91ca5

Browse files
committed
Use env OUT_DIR as the temporary directory for benches and tests
1 parent 2562c82 commit 5e91ca5

File tree

11 files changed

+38
-99
lines changed

11 files changed

+38
-99
lines changed

spdlog/benches/common/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use std::{env, fs, path::PathBuf};
1+
use std::{
2+
env, fs,
3+
path::{Path, PathBuf},
4+
};
25

36
use once_cell::sync::Lazy;
47

58
pub static BENCH_LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
6-
let path = env::current_exe()
7-
.unwrap()
8-
.parent()
9-
.unwrap()
10-
.join("dev/bench_logs");
11-
fs::create_dir_all(&path).unwrap();
9+
let path = Path::new(env!("OUT_DIR")).join("bench_logs");
10+
if !path.exists() {
11+
fs::create_dir(&path).unwrap();
12+
}
1213
path
1314
});
1415

spdlog/benches/compare_with_cpp_spdlog.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ extern crate test;
44

55
mod common;
66

7-
use std::{env, fs, path::PathBuf, sync::Arc, thread, time::Instant};
7+
use std::{env, sync::Arc, thread, time::Instant};
88

99
use clap::Parser;
10-
use once_cell::sync::Lazy;
1110
use spdlog::{
1211
formatter::{pattern, PatternFormatter},
1312
info,
@@ -22,12 +21,6 @@ include!(concat!(
2221
));
2322
use test_utils::*;
2423

25-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
26-
let path = common::BENCH_LOGS_PATH.join("compare_with_cpp_spdlog");
27-
fs::create_dir_all(&path).unwrap();
28-
path
29-
});
30-
3124
const FILE_SIZE: u64 = 30 * 1024 * 1024;
3225
// C++ "spdlog" is `5` here because it does not include the current file,
3326
// "spdlog-rs" does.
@@ -44,7 +37,7 @@ fn bench_threaded_logging(threads: usize, iters: usize) {
4437
let logger = build_test_logger(|b| {
4538
b.sink(Arc::new(
4639
FileSink::builder()
47-
.path(LOGS_PATH.join("FileSink.log"))
40+
.path(common::BENCH_LOGS_PATH.join("FileSink.log"))
4841
.truncate(true)
4942
.build()
5043
.unwrap(),
@@ -56,7 +49,7 @@ fn bench_threaded_logging(threads: usize, iters: usize) {
5649
let logger = build_test_logger(|b| {
5750
b.sink(Arc::new(
5851
RotatingFileSink::builder()
59-
.base_path(LOGS_PATH.join("RotatingFileSink_FileSize.log"))
52+
.base_path(common::BENCH_LOGS_PATH.join("RotatingFileSink_FileSize.log"))
6053
.rotation_policy(RotationPolicy::FileSize(FILE_SIZE))
6154
.max_files(ROTATING_FILES)
6255
.build()
@@ -69,7 +62,7 @@ fn bench_threaded_logging(threads: usize, iters: usize) {
6962
let logger = build_test_logger(|b| {
7063
b.sink(Arc::new(
7164
RotatingFileSink::builder()
72-
.base_path(LOGS_PATH.join("RotatingFileSink_Daily.log"))
65+
.base_path(common::BENCH_LOGS_PATH.join("RotatingFileSink_Daily.log"))
7366
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 })
7467
.build()
7568
.unwrap(),

spdlog/benches/compare_with_cpp_spdlog_async.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ extern crate test;
44

55
mod common;
66

7-
use std::{cmp, env, fs, path::PathBuf, sync::Arc, thread, time::Instant};
7+
use std::{cmp, env, sync::Arc, thread, time::Instant};
88

99
use clap::Parser;
10-
use once_cell::sync::Lazy;
1110
use spdlog::{
1211
error::{Error, SendToChannelError},
1312
formatter::{pattern, PatternFormatter},
@@ -19,12 +18,6 @@ use test::black_box;
1918

2019
required_multi_thread_feature!();
2120

22-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
23-
let path = common::BENCH_LOGS_PATH.join("compare_with_cpp_spdlog_async");
24-
fs::create_dir_all(&path).unwrap();
25-
path
26-
});
27-
2821
fn bench(
2922
policy: OverflowPolicy,
3023
file_name: &str,
@@ -43,7 +36,7 @@ fn bench(
4336

4437
let file_sink = Arc::new(
4538
FileSink::builder()
46-
.path(LOGS_PATH.join(file_name))
39+
.path(common::BENCH_LOGS_PATH.join(file_name))
4740
.truncate(true)
4841
.build()
4942
.unwrap(),

spdlog/benches/fern.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,13 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf};
8-
97
use fern::Dispatch;
108
use log::info;
11-
use once_cell::sync::Lazy;
129
use test::Bencher;
1310

14-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
15-
let path = common::BENCH_LOGS_PATH.join("fern");
16-
fs::create_dir_all(&path).unwrap();
17-
path
18-
});
19-
2011
#[bench]
2112
fn bench_1_file(bencher: &mut Bencher) {
22-
let path = LOGS_PATH.join("file.log");
13+
let path = common::BENCH_LOGS_PATH.join("file.log");
2314

2415
Dispatch::new()
2516
.format(|out, message, record| {

spdlog/benches/flexi_logger.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf, sync::Mutex};
7+
use std::sync::Mutex;
88

99
use flexi_logger::{
1010
writers::FileLogWriter, Age, Cleanup, Criterion, DeferredNow, FileSpec, LogSpecification,
@@ -14,16 +14,10 @@ use log::{info, Record};
1414
use once_cell::sync::Lazy;
1515
use test::Bencher;
1616

17-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
18-
let path = common::BENCH_LOGS_PATH.join("flexi_logger");
19-
fs::create_dir_all(&path).unwrap();
20-
path
21-
});
22-
2317
static HANDLE: Lazy<Mutex<LoggerHandle>> = Lazy::new(|| {
2418
Mutex::new(
2519
Logger::with(LogSpecification::off())
26-
.log_to_file(FileSpec::try_from(LOGS_PATH.join("empty.log")).unwrap())
20+
.log_to_file(FileSpec::try_from(common::BENCH_LOGS_PATH.join("empty.log")).unwrap())
2721
.write_mode(WriteMode::BufferDontFlush)
2822
.format(formatter)
2923
.start()
@@ -47,7 +41,7 @@ pub fn formatter(
4741

4842
#[bench]
4943
fn bench_1_file(bencher: &mut Bencher) {
50-
let path = LOGS_PATH.join("file.log");
44+
let path = common::BENCH_LOGS_PATH.join("file.log");
5145

5246
let writer_builder = FileLogWriter::builder(FileSpec::try_from(path).unwrap())
5347
.write_mode(WriteMode::BufferDontFlush)
@@ -64,7 +58,7 @@ unavailable_bench!(bench_2_file_async);
6458

6559
#[bench]
6660
fn bench_3_rotating_file_size(bencher: &mut Bencher) {
67-
let path = LOGS_PATH.join("rotating_file_size.log");
61+
let path = common::BENCH_LOGS_PATH.join("rotating_file_size.log");
6862

6963
let writer_builder = FileLogWriter::builder(FileSpec::try_from(path).unwrap())
7064
.write_mode(WriteMode::BufferDontFlush)
@@ -84,7 +78,7 @@ fn bench_3_rotating_file_size(bencher: &mut Bencher) {
8478

8579
#[bench]
8680
fn bench_4_rotating_daily(bencher: &mut Bencher) {
87-
let path = LOGS_PATH.join("rotating_daily.log");
81+
let path = common::BENCH_LOGS_PATH.join("rotating_daily.log");
8882

8983
let writer_builder = FileLogWriter::builder(FileSpec::try_from(path).unwrap())
9084
.write_mode(WriteMode::BufferDontFlush)

spdlog/benches/log4rs.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf};
8-
97
use log::{info, LevelFilter};
108
use log4rs::{
119
append::{
@@ -24,12 +22,6 @@ use log4rs::{
2422
use once_cell::sync::Lazy;
2523
use test::Bencher;
2624

27-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
28-
let path = common::BENCH_LOGS_PATH.join("log4rs");
29-
fs::create_dir_all(&path).unwrap();
30-
path
31-
});
32-
3325
static HANDLE: Lazy<Handle> = Lazy::new(|| {
3426
log4rs::init_config(
3527
Config::builder()
@@ -41,7 +33,7 @@ static HANDLE: Lazy<Handle> = Lazy::new(|| {
4133

4234
#[bench]
4335
fn bench_1_file(bencher: &mut Bencher) {
44-
let path = LOGS_PATH.join("file.log");
36+
let path = common::BENCH_LOGS_PATH.join("file.log");
4537

4638
let appender = FileAppender::builder()
4739
.append(false)
@@ -62,8 +54,8 @@ unavailable_bench!(bench_2_file_async);
6254

6355
#[bench]
6456
fn bench_3_rotating_file_size(bencher: &mut Bencher) {
65-
let pattern_path = LOGS_PATH.join("rotating_file_size_{}.log");
66-
let path = LOGS_PATH.join("rotating_file_size.log");
57+
let pattern_path = common::BENCH_LOGS_PATH.join("rotating_file_size_{}.log");
58+
let path = common::BENCH_LOGS_PATH.join("rotating_file_size.log");
6759

6860
let policy = CompoundPolicy::new(
6961
Box::new(SizeTrigger::new(common::FILE_SIZE)),

spdlog/benches/slog.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf};
8-
9-
use once_cell::sync::Lazy;
107
use slog::{info, o, Fuse, Logger};
118
use sloggers::{file::FileLoggerBuilder, types::SourceLocation, Build};
129
use test::Bencher;
1310

14-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
15-
let path = common::BENCH_LOGS_PATH.join("slog");
16-
fs::create_dir_all(&path).unwrap();
17-
path
18-
});
19-
2011
unavailable_bench!(bench_1_file);
2112

2213
#[bench]
2314
fn bench_2_file_async(bencher: &mut Bencher) {
24-
let path = LOGS_PATH.join("file_async.log");
15+
let path = common::BENCH_LOGS_PATH.join("file_async.log");
2516

2617
let drain = Fuse(
2718
FileLoggerBuilder::new(path)
@@ -37,7 +28,7 @@ fn bench_2_file_async(bencher: &mut Bencher) {
3728

3829
#[bench]
3930
fn bench_3_rotating_file_size_async(bencher: &mut Bencher) {
40-
let path = LOGS_PATH.join("rotating_file_size_async.log");
31+
let path = common::BENCH_LOGS_PATH.join("rotating_file_size_async.log");
4132

4233
let drain = Fuse(
4334
FileLoggerBuilder::new(path)

spdlog/benches/spdlog_rs.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf, sync::Arc};
7+
use std::{path::PathBuf, sync::Arc};
88

9-
use once_cell::sync::Lazy;
109
use spdlog::{
1110
error::{Error, ErrorHandler, SendToChannelError},
1211
prelude::*,
@@ -23,12 +22,6 @@ use test_utils::*;
2322

2423
required_multi_thread_feature!();
2524

26-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
27-
let path = common::BENCH_LOGS_PATH.join("spdlog_rs");
28-
fs::create_dir_all(&path).unwrap();
29-
path
30-
});
31-
3225
enum Mode {
3326
Sync,
3427
Async,
@@ -40,7 +33,7 @@ impl Mode {
4033
Self::Sync => format!("{file_name}.log"),
4134
Self::Async => format!("{file_name}_async.log"),
4235
};
43-
LOGS_PATH.join(file_name)
36+
common::BENCH_LOGS_PATH.join(file_name)
4437
}
4538

4639
fn final_sink(&self, sink: Arc<dyn Sink>) -> Arc<dyn Sink> {

spdlog/benches/tracing.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@ extern crate test;
44

55
mod common;
66

7-
use std::{fs, path::PathBuf};
8-
9-
use once_cell::sync::Lazy;
107
use test::Bencher;
118
use tracing::info;
129
use tracing_subscriber::{filter::LevelFilter, fmt::MakeWriter};
1310

14-
static LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
15-
let path = common::BENCH_LOGS_PATH.join("tracing");
16-
fs::create_dir_all(&path).unwrap();
17-
path
18-
});
19-
2011
fn bench_any(
2112
bencher: &mut Bencher,
2213
writer: impl for<'writer> MakeWriter<'writer> + 'static + Send + Sync,
@@ -36,13 +27,14 @@ fn bench_any(
3627
fn bench_1_file(bencher: &mut Bencher) {
3728
bench_any(
3829
bencher,
39-
tracing_appender::rolling::never(&*LOGS_PATH, "file.log"),
30+
tracing_appender::rolling::never(&*common::BENCH_LOGS_PATH, "file.log"),
4031
);
4132
}
4233

4334
#[bench]
4435
fn bench_2_file_async(bencher: &mut Bencher) {
45-
let file_appender = tracing_appender::rolling::never(&*LOGS_PATH, "file_async.log");
36+
let file_appender =
37+
tracing_appender::rolling::never(&*common::BENCH_LOGS_PATH, "file_async.log");
4638
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
4739

4840
bench_any(bencher, non_blocking);
@@ -54,7 +46,7 @@ unavailable_bench!(bench_3_rotating_file_size);
5446
fn bench_4_rotating_daily(bencher: &mut Bencher) {
5547
bench_any(
5648
bencher,
57-
tracing_appender::rolling::daily(&*LOGS_PATH, "rotating_daily.log"),
49+
tracing_appender::rolling::daily(&*common::BENCH_LOGS_PATH, "rotating_daily.log"),
5850
);
5951
}
6052

spdlog/src/sink/rotating_file_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ mod tests {
871871

872872
static BASE_LOGS_PATH: Lazy<PathBuf> = Lazy::new(|| {
873873
let path = TEST_LOGS_PATH.join("rotating_file_sink");
874-
fs::create_dir_all(&path).unwrap();
874+
fs::create_dir(&path).unwrap();
875875
path
876876
});
877877

0 commit comments

Comments
 (0)