Skip to main content

pop_chains/bench/
binary.rs

1// SPDX-License-Identifier: GPL-3.0
2
3use pop_common::{
4	Error,
5	git::GitHub,
6	polkadot_sdk::sort_by_latest_stable_version,
7	sourcing::{
8		ArchiveFileSpec, Binary,
9		GitHub::*,
10		Source,
11		filters::prefix,
12		traits::{
13			Source as SourceT,
14			enums::{Source as _, *},
15		},
16	},
17	target,
18};
19use std::path::PathBuf;
20use strum_macros::EnumProperty;
21
22#[derive(Debug, EnumProperty, PartialEq)]
23pub(super) enum BenchmarkingCli {
24	#[strum(props(
25		Repository = "https://github.com/r0gue-io/polkadot",
26		Binary = "frame-omni-bencher",
27		TagPattern = "polkadot-{version}",
28		Fallback = "stable2512"
29	))]
30	OmniBencher,
31}
32
33impl SourceT for BenchmarkingCli {
34	type Error = Error;
35	/// Defines the source of the binary required for benchmarking.
36	fn source(&self) -> Result<Source, Error> {
37		// Source from GitHub release asset
38		let repo = GitHub::parse(self.repository())?;
39		let binary = self.binary();
40		Ok(Source::GitHub(ReleaseArchive {
41			owner: repo.org,
42			repository: repo.name,
43			tag: None,
44			tag_pattern: self.tag_pattern().map(|t| t.into()),
45			prerelease: false,
46			version_comparator: sort_by_latest_stable_version,
47			fallback: self.fallback().into(),
48			archive: format!("{binary}-{}.tar.gz", target()?),
49			contents: vec![ArchiveFileSpec::new(binary.into(), Some(binary.into()), true)],
50			latest: None,
51		}))
52	}
53}
54
55/// Generate the source of the `frame-omni-bencher` binary on the remote repository.
56///
57/// # Arguments
58/// * `cache` - The path to the directory where the binary should be cached.
59/// * `version` - An optional version string. If `None`, the latest available version is used.
60pub async fn omni_bencher_generator(
61	cache: PathBuf,
62	version: Option<&str>,
63) -> Result<Binary, Error> {
64	let cli = BenchmarkingCli::OmniBencher;
65	let name = cli.binary().to_string();
66	let source = cli
67		.source()?
68		.resolve(&name, version, cache.as_path(), |f| prefix(f, &name))
69		.await
70		.into();
71	let binary = Binary::Source { name, source, cache: cache.to_path_buf() };
72	Ok(binary)
73}
74
75#[cfg(test)]
76mod tests {
77	use super::*;
78	use tempfile::tempdir;
79
80	#[tokio::test]
81	async fn omni_bencher_generator_works() -> Result<(), Error> {
82		let temp_dir = tempdir()?;
83		let temp_dir_path = temp_dir.path().to_path_buf();
84		let version = "polkadot-stable2512";
85		let binary = omni_bencher_generator(temp_dir_path.clone(), Some(version)).await?;
86		assert!(matches!(binary, Binary::Source { name: _, source, cache }
87				if source == Source::GitHub(ReleaseArchive {
88					owner: "r0gue-io".to_string(),
89					repository: "polkadot".to_string(),
90					tag: Some(version.to_string()),
91					tag_pattern: Some("polkadot-{version}".into()),
92					prerelease: false,
93					version_comparator: sort_by_latest_stable_version,
94					fallback: "stable2512".to_string(),
95					archive: format!("frame-omni-bencher-{}.tar.gz", target()?),
96					contents: ["frame-omni-bencher"].map(|b| ArchiveFileSpec::new(b.into(), Some(b.into()), true)).to_vec(),
97					latest: binary.latest().map(|l| l.to_string()),
98				}).into() &&
99				cache == temp_dir_path.as_path()
100		));
101		Ok(())
102	}
103}