storage_benchmarks/
testing.rs

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
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::filesystem::MountedFilesystemInstance;
use crate::{CacheClearableFilesystem, Filesystem};
use async_trait::async_trait;
use futures::lock::Mutex;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tempfile::TempDir;

/// Filesystem implementation that records `clear_cache` calls for validating warm and cold
/// benchmarks.
#[derive(Clone)]
pub struct TestFilesystem {
    benchmark_dir: PathBuf,
    inner: Arc<Mutex<TestFilesystemInner>>,
}

struct TestFilesystemInner {
    fs: Option<Box<MountedFilesystemInstance>>,
    clear_cache_count: u64,
}

impl TestFilesystem {
    pub fn new() -> Self {
        let benchmark_dir = TempDir::new_in("/tmp").unwrap().into_path();
        Self {
            inner: Arc::new(Mutex::new(TestFilesystemInner {
                fs: Some(Box::new(MountedFilesystemInstance::new(&benchmark_dir))),
                clear_cache_count: 0,
            })),
            benchmark_dir,
        }
    }

    pub async fn clear_cache_count(&self) -> u64 {
        self.inner.lock().await.clear_cache_count
    }
}

#[async_trait]
impl Filesystem for TestFilesystem {
    async fn shutdown(self) {
        let mut inner = self.inner.lock().await;
        inner.fs.take().unwrap().shutdown().await;
    }

    fn benchmark_dir(&self) -> &Path {
        &self.benchmark_dir
    }
}

#[async_trait]
impl CacheClearableFilesystem for TestFilesystem {
    async fn clear_cache(&mut self) {
        let mut inner = self.inner.lock().await;
        inner.clear_cache_count += 1;
    }
}

impl Drop for TestFilesystem {
    fn drop(&mut self) {
        assert!(
            !self.benchmark_dir.try_exists().unwrap(),
            "The benchmark directory still exists. Shutdown may not have been called."
        );
    }
}