Skip to content

Commit

Permalink
Added optional loglevel parameter to the config-gen like the other we…
Browse files Browse the repository at this point in the history
…bassemblies
  • Loading branch information
tkmcmaster committed Aug 14, 2023
1 parent df03d2f commit 74cab8e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ jobs:
curl -sSL https://github.com/rustwasm/wasm-pack/releases/download/v0.11.0/wasm-pack-v0.11.0-x86_64-unknown-linux-musl.tar.gz \
| tar -xz --strip-components=1 -C ~/bin --no-anchored wasm-pack
if [ "${{ matrix.wasm-dirctory }}" = "config-gen"]; then
echo "Running wasm-pack test"
wasm-pack test --node
fi
wasm-pack build --release -t nodejs --scope fs
working-directory: ${{env.wasm-directory}}
shell: bash
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ tempfile = "3"
panic = "abort"
lto = "fat"

[profile.release.package.config-gen]
opt-level = 's'

[profile.release.package.config-wasm]
opt-level = 's'

Expand Down
2 changes: 2 additions & 0 deletions guide/results-viewer-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions lib/config-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ edition = "2021"
publish = false
license.workspace = true

[package.metadata.wasm-pack.profile.release]
wasm-opt = false
[package.metadata.wasm-pack.profile.dev]
wasm-opt = false
[package.metadata.wasm-pack.profile.profiling]
wasm-opt = false

[lib]
crate_type = ["cdylib", "rlib"]

Expand All @@ -24,9 +17,8 @@ serde_json.workspace = true
serde_yaml.workspace = true
wasm-bindgen.workspace = true
getrandom.workspace = true
log.workspace = true
wasm-logger.workspace = true

[dev-dependencies]
wasm-bindgen-test = "0.3.37"

[profile.release]
debug = true
52 changes: 48 additions & 4 deletions lib/config-gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
use config::templating::False;
use wasm_bindgen::prelude::*;
use log::LevelFilter;
use std::str::FromStr;
use wasm_bindgen::{prelude::wasm_bindgen, throw_str, JsError, UnwrapThrowExt};

pub type LoadTest = config::LoadTest<False, False>;

// Only valid because we are using this in a WebAssembly context without threads.
// We can only initialize the logger once or it will spit out error logs every time we init the constructor
static mut LOGGING_INITIALIZED: bool = false;

fn default_log_level() -> LevelFilter {
LevelFilter::Error
}

#[no_mangle]
fn set_logging_initialized() {
unsafe { LOGGING_INITIALIZED = true };
}

#[no_mangle]
fn get_logging_initialized() -> bool {
unsafe { LOGGING_INITIALIZED }
}

fn init_logging(log_level: Option<String>) {
if !get_logging_initialized() {
// Use a LevelFilter instead of Level so we can set it to "off"
let mut level_filter = default_log_level();
if let Some(level_string) = log_level {
level_filter = match LevelFilter::from_str(&level_string) {
Ok(val) => val,
Err(err) => throw_str(&err.to_string()),
}
}
let level = level_filter.to_level();
// May be off
if level.is_some() {
wasm_logger::init(wasm_logger::Config::new(level.unwrap_throw()));
}
// If it's off, we still don't want to set again, once it's on, it's on
set_logging_initialized();
}
}

#[wasm_bindgen]
pub fn load_test_yaml_from_js(data: &str) -> Result<String, JsError> {
pub fn load_test_yaml_from_js(data: &str, log_level: Option<String>) -> Result<String, JsError> {
init_logging(log_level);
// had some issues getting direct conversion from JsValue, so stringified JSON is used
console_error_panic_hook::set_once();
let lt: LoadTest = serde_json::from_str(data)?;
let yaml = serde_yaml::to_string(&lt)?;
log::debug!("load_test_yaml_from_js data: {}", data);
let load_test: LoadTest = serde_json::from_str(data)?;
log::debug!("load_test_yaml_from_js load_test: {:?}", load_test);
let yaml = serde_yaml::to_string(&load_test)?;
log::debug!("load_test_yaml_from_js yaml: {}", yaml);
Ok(yaml)
}
6 changes: 2 additions & 4 deletions lib/config-gen/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use serde_json::json;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn complex() {
let json = json!({
Expand All @@ -31,7 +29,7 @@ fn complex() {
});
let json_str = serde_json::to_string(&json).unwrap();
let lt: LoadTest = serde_json::from_str(&json_str).unwrap();
let yaml1 = load_test_yaml_from_js(&json_str)
let yaml1 = load_test_yaml_from_js(&json_str, None)
.map_err(JsValue::from)
.unwrap();
let lt2: LoadTest = serde_yaml::from_str(&yaml1).unwrap();
Expand Down Expand Up @@ -70,7 +68,7 @@ loggers:
fn other_test() {
let json_str = "{\"vars\":{\"rampTime\":\"${e:RAMP_TIME}\",\"loadTime\":\"${e:LOAD_TIME}\",\"peakLoad\":\"${e:PEAK_LOAD}\",\"sessionId\":\"${e:SESSIONID}\"},\"config\":{\"client\":{\"headers\":{\"User-Agent\":\"Pewpew Performance Load Test\"}},\"general\":{\"bucket_size\":\"1m\",\"log_provider_stats\":true}},\"load_pattern\":[{\"linear\":{\"from\":\"10%\",\"to\":\"100%\",\"over\":\"15m\"}},{\"linear\":{\"from\":\"100%\",\"to\":\"100%\",\"over\":\"15m\"}}],\"loggers\":{\"httpErrors\":{\"query\":{\"select\":{\"timestamp\":\"epoch(\\\"ms\\\")\",\"rtt\":\"stats.rtt\",\"request\":\"request[\\\"start-line\\\"]\",\"requestHeaders\":\"request.headers\",\"requestBody\":\"request.body\",\"response\":\"response[\\\"start-line\\\"]\",\"status\":\"response.status\",\"responseHeaders\":\"response.headers\"},\"where\":\"response.status >= 400\"},\"to\":\"stderr\",\"limit\":200},\"testEnd\":{\"query\":{\"select\":{\"timestamp\":\"epoch(\\\"ms\\\")\",\"status\":\"response.status\",\"request\":\"request[\\\"start-line\\\"]\",\"response\":\"response[\\\"start-line\\\"]\"},\"where\":\"response.status >= 500\"},\"to\":\"stderr\",\"limit\":50,\"kill\":true}}}";
let _lt: LoadTest = serde_json::from_str(json_str).unwrap();
let _yaml = load_test_yaml_from_js(json_str)
let _yaml = load_test_yaml_from_js(json_str, None)
.map_err(JsValue::from)
.unwrap();
}
7 changes: 4 additions & 3 deletions pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CWD=$(pwd)

cd "$CWD/lib/config-wasm"
# cargo install wasm-pack
wasm-pack test --node
wasm-pack build --release -t nodejs --scope fs
# ~/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt pkg/config_wasm_bg.wasm -o pkg/config_wasm_bg.wasm -Oz

Expand All @@ -34,9 +35,9 @@ cd "$CWD/lib/config-gen"
wasm-pack build --release -t nodejs --scope fs
# ~/.cache/.wasm-pack/wasm-opt-4d7a65327e9363b7/wasm-opt pkg/config_wasm_bg.wasm -o pkg/config_wasm_bg.wasm -Oz

# cd tests/
# npm ci
# npm test
cd tests/
npm ci
npm test

cd "$CWD/lib/hdr-histogram-wasm"
wasm-pack build --release -t nodejs --scope fs
Expand Down

0 comments on commit 74cab8e

Please sign in to comment.