Skip to content

Commit 60f5cbe

Browse files
committed
feat: add env var VM_JOB_QUEUE_SHARED for tests
This adds a kind of turbo-mode for running unit tests. Normally the job-queue is a shared singleton instance. When running unit tests, a shared instance has these characteristics: 1. tests must wait for each other's jobs to complete. CPU usage tends to be low for much of the testing duration. 2. it is possible to run all tests in parallel and generate proofs when proofs do not exist in local cache or on proof-server. 3. total time for running all tests increases substantially compared to scenario where each test using its own job-queue instance. A non-shared instance has these characteristics: 1. tests run independently and use up all CPU cores. 2. it is not possible to run all tests in parallel and generate proofs as it would exhaust device's resources, especially RAM. This mode only works well when proofs are already cached. A workaround is to run with --test-threads 1 to generate proofs. 3. total time for running all tests decrease substantially (assuming proofs are cached) vs the shared-instance scenario. When running unit tests, shared mode is the default. The mode can now be selected at runtime via: ``` VM_JOB_QUEUE_SHARED=false cargo test <args> VM_JOB_QUEUE_SHARED=true cargo test <args> ```
1 parent ca608d4 commit 60f5cbe

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/triton_vm_job_queue/mod.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,60 @@ impl DerefMut for TritonVmJobQueue {
3434
}
3535

3636
impl TritonVmJobQueue {
37-
/// returns the triton vm job queue (singleton).
37+
/// returns the single, shared triton vm job queue (singleton).
3838
///
3939
/// callers should execute resource intensive triton-vm tasks in this
4040
/// queue to avoid running simultaneous tasks that could exceed hardware
4141
/// capabilities.
42+
#[cfg(not(test))]
4243
pub fn get_instance() -> Arc<Self> {
44+
Self::get_instance_internal()
45+
}
46+
47+
/// returns a triton vm job queue.
48+
///
49+
/// By default, the returned queue will be a shared singleton instance.
50+
///
51+
/// When running unit tests, a shared instance has these characteristics:
52+
///
53+
/// 1. tests must wait for each other's jobs to complete. CPU usage tends to
54+
/// be low for much of the testing duration.
55+
/// 2. it is possible to run all tests in parallel and generate proofs when
56+
/// proofs do not exist in local cache or on proof-server.
57+
/// 3. total time for running all tests increases substantially compared to
58+
/// scenario where each test using its own job-queue instance.
59+
///
60+
/// A non-shared instance has these characteristics:
61+
///
62+
/// 1. tests run independently and use up all CPU cores.
63+
/// 2. it is not possible to run all tests in parallel and generate proofs
64+
/// as it would exhaust device's resources, especially RAM. This mode
65+
/// only works well when proofs are already cached. A workaround is to
66+
/// run with --test-threads 1 to generate proofs.
67+
/// 3. total time for running all tests decrease substantially (assuming
68+
/// proofs are cached) vs the shared-instance scenario.
69+
///
70+
/// When running unit tests, shared mode is the default. The mode can be
71+
/// selected at runtime via:
72+
///
73+
/// ```text
74+
/// # disable shared queue (each test gets it's own queue)
75+
/// VM_JOB_QUEUE_SHARED=false cargo test <args>
76+
///
77+
/// # enable shared queue (default behavior)
78+
/// VM_JOB_QUEUE_SHARED=true cargo test <args>
79+
/// ```
80+
#[cfg(test)]
81+
pub fn get_instance() -> Arc<Self> {
82+
let shared = std::env::var("VM_JOB_QUEUE_SHARED").unwrap_or_else(|_| "true".to_string());
83+
84+
match shared.as_str() {
85+
"false" => Arc::new(Self(JobQueue::<TritonVmJobPriority>::start())),
86+
"true" | &_ => Self::get_instance_internal(),
87+
}
88+
}
89+
90+
fn get_instance_internal() -> Arc<Self> {
4391
use std::sync::OnceLock;
4492
static INSTANCE: OnceLock<Arc<TritonVmJobQueue>> = OnceLock::new();
4593
INSTANCE

0 commit comments

Comments
 (0)