-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.rs
More file actions
371 lines (315 loc) · 12.1 KB
/
Copy pathmain.rs
File metadata and controls
371 lines (315 loc) · 12.1 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use function_runner::{BytesContainer, BytesContainerType, Codec};
use wasmtime::Module;
use std::{
fs::File,
io::{stdin, BufRead, BufReader, Read},
path::PathBuf,
};
use anyhow::{anyhow, Result};
use clap::Parser;
use function_runner::{
bluejay_schema_analyzer::BluejaySchemaAnalyzer,
engine::{run, FunctionRunParams, ProfileOpts, ValidatedModule},
};
use is_terminal::IsTerminal;
const PROFILE_DEFAULT_INTERVAL: u32 = 500_000; // every 5us
const DEFAULT_SCALE_FACTOR: f64 = 1.0;
/// Simple Function runner which takes JSON as a convenience.
#[derive(Parser, Debug)]
#[clap(version)]
#[command(arg_required_else_help = true)]
struct Opts {
/// Path to wasm/wat Function
#[clap(short, long, default_value = "function.wasm")]
function: PathBuf,
/// Path to json file containing Function input; if omitted, stdin is used
#[clap(short, long)]
input: Option<PathBuf>,
/// Name of the export to invoke.
#[clap(short, long, default_value = "_start")]
export: String,
/// Log the run result as a JSON object
#[clap(short, long)]
json: bool,
/// Enable profiling. This will make your Function run slower.
/// The resulting profile can be used in speedscope (https://www.speedscope.app/)
/// Specifying --profile-* argument will also enable profiling.
#[clap(short, long)]
profile: bool,
/// Where to save the profile information. Defaults to ./{wasm-filename}.perf.
#[clap(long)]
profile_out: Option<PathBuf>,
/// How many samples per second. Defaults to 500_000 (every 2us).
#[clap(long)]
profile_frequency: Option<u32>,
/// Path to graphql file containing Function schema; if omitted, defaults will be used to calculate limits.
#[clap(short = 's', long)]
schema_path: Option<PathBuf>,
/// Path to graphql file containing Function input query; if omitted, defaults will be used to calculate limits.
#[clap(short = 'q', long)]
query_path: Option<PathBuf>,
/// Enable batch mode - read multiple JSON inputs (one per line) from stdin/file
#[clap(short, long)]
batch: bool,
/// In batch mode, continue processing on individual input errors (default: false)
#[clap(long)]
batch_continue_on_error: bool,
}
impl Opts {
pub fn profile_opts(&self) -> Option<ProfileOpts> {
if !self.profile && self.profile_out.is_none() && self.profile_frequency.is_none() {
return None;
}
let interval = self.profile_frequency.unwrap_or(PROFILE_DEFAULT_INTERVAL);
let out = self
.profile_out
.clone()
.unwrap_or_else(|| self.default_profile_out());
Some(ProfileOpts { interval, out })
}
fn default_profile_out(&self) -> PathBuf {
let mut path = PathBuf::new();
path.set_file_name(
self.function
.file_name()
.unwrap_or(std::ffi::OsStr::new("function")),
);
path.set_extension("perf");
path
}
pub fn read_schema_to_string(&self) -> Option<Result<String>> {
self.schema_path.as_ref().map(read_file_to_string)
}
pub fn read_query_to_string(&self) -> Option<Result<String>> {
self.query_path.as_ref().map(read_file_to_string)
}
}
fn read_file_to_string(file_path: &PathBuf) -> Result<String> {
let mut file = File::open(file_path)
.map_err(|e| anyhow!("Couldn't open file {}: {}", file_path.to_string_lossy(), e))?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.map_err(|e| anyhow!("Couldn't read file {}: {}", file_path.to_string_lossy(), e))?;
Ok(contents)
}
fn main() -> Result<()> {
let opts: Opts = Opts::parse();
// Create engine and module once (expensive operations - amortize across all inputs)
let engine = function_runner::engine::new_engine()?;
let module = Module::from_file(&engine, &opts.function)
.map_err(|e| anyhow!("Couldn't load the Function {:?}: {}", &opts.function, e))?;
// Infer codec from the module based on imported modules
let codec = if function_runner::engine::uses_msgpack_provider(&module) {
Codec::Messagepack
} else {
Codec::Json
};
// Validate and compile the module (and its standard provider) once, then
// reuse it across every run so batch mode doesn't recompile per input.
let validated_module = ValidatedModule::new(module, &engine)?;
if opts.batch {
run_batch_mode(&opts, &engine, &validated_module, codec)
} else {
run_single_mode(&opts, &engine, &validated_module, codec)
}
}
fn run_single_mode(
opts: &Opts,
engine: &wasmtime::Engine,
validated_module: &ValidatedModule,
codec: Codec,
) -> Result<()> {
let mut input: Box<dyn Read + Sync + Send + 'static> = if let Some(ref input) = opts.input {
Box::new(BufReader::new(File::open(input).map_err(|e| {
anyhow!("Couldn't load input {:?}: {}", input, e)
})?))
} else if !std::io::stdin().is_terminal() {
Box::new(BufReader::new(stdin()))
} else {
return Err(anyhow!(
"You must provide input via the --input flag or piped via stdin."
));
};
let mut buffer = Vec::new();
input.read_to_end(&mut buffer)?;
let schema_string = opts.read_schema_to_string().transpose()?;
let query_string = opts.read_query_to_string().transpose()?;
let input = BytesContainer::new(BytesContainerType::Input, codec, buffer)?;
let scale_factor = if let (Some(schema_string), Some(query_string), Some(json_value)) =
(schema_string, query_string, input.json_value.clone())
{
BluejaySchemaAnalyzer::analyze_schema_definition(
&schema_string,
opts.schema_path.as_ref().and_then(|p| p.to_str()),
&query_string,
opts.query_path.as_ref().and_then(|p| p.to_str()),
&json_value,
)?
} else {
DEFAULT_SCALE_FACTOR
};
let profile_opts = opts.profile_opts();
let function_run_result = run(FunctionRunParams {
function_path: opts.function.clone(),
input,
export: opts.export.as_ref(),
profile_opts: profile_opts.as_ref(),
scale_factor,
module: validated_module.clone(),
engine: engine.clone(),
})?;
if opts.json {
println!("{}", function_run_result.to_json());
} else {
println!("{function_run_result}");
}
if let Some(profile) = function_run_result.profile.as_ref() {
std::fs::write(profile_opts.unwrap().out, profile)?;
}
if function_run_result.success {
Ok(())
} else {
anyhow::bail!("The Function execution failed. Review the logs for more information.")
}
}
fn run_batch_mode(
opts: &Opts,
engine: &wasmtime::Engine,
validated_module: &ValidatedModule,
codec: Codec,
) -> Result<()> {
let input_reader: Box<dyn BufRead> = if let Some(ref input) = opts.input {
Box::new(BufReader::new(File::open(input).map_err(|e| {
anyhow!("Couldn't load input {:?}: {}", input, e)
})?))
} else if !std::io::stdin().is_terminal() {
Box::new(BufReader::new(stdin()))
} else {
return Err(anyhow!(
"You must provide input via the --input flag or piped via stdin."
));
};
// Load schema/query once; scale factor is computed per input.
let schema_string = opts.read_schema_to_string().transpose()?;
let query_string = opts.read_query_to_string().transpose()?;
// Disable profiling in batch mode for performance
let profile_opts = None;
let mut line_num = 0;
let mut processed_count = 0;
let mut success_count = 0;
let mut failed_count = 0;
for line_result in input_reader.lines() {
line_num += 1;
let line = match line_result {
Ok(l) => l,
Err(e) => {
failed_count += 1;
if opts.batch_continue_on_error {
eprintln!("Error reading line {}: {}", line_num, e);
println!(
r#"{{"success":false,"error":"Error reading input: {}"}}"#,
e
);
continue;
} else {
return Err(e.into());
}
}
};
// Skip empty lines
if line.trim().is_empty() {
continue;
}
processed_count += 1;
// Parse input
let input = match BytesContainer::new(BytesContainerType::Input, codec, line.into_bytes()) {
Ok(i) => i,
Err(e) => {
failed_count += 1;
if opts.batch_continue_on_error {
eprintln!("Error parsing line {}: {}", line_num, e);
println!(r#"{{"success":false,"error":"Invalid JSON input: {}"}}"#, e);
continue;
} else {
return Err(e);
}
}
};
// Calculate scale factor for this input
let scale_factor =
if let (Some(ref schema_string), Some(ref query_string), Some(ref json_value)) =
(&schema_string, &query_string, &input.json_value)
{
match BluejaySchemaAnalyzer::analyze_schema_definition(
schema_string,
opts.schema_path.as_ref().and_then(|p| p.to_str()),
query_string,
opts.query_path.as_ref().and_then(|p| p.to_str()),
json_value,
) {
Ok(sf) => sf,
Err(e) => {
failed_count += 1;
if opts.batch_continue_on_error {
eprintln!("Error analyzing schema for line {}: {}", line_num, e);
println!(
r#"{{"success":false,"error":"Schema analysis failed: {}"}}"#,
e
);
continue;
} else {
return Err(e);
}
}
}
} else {
DEFAULT_SCALE_FACTOR
};
// Run function (reusing engine/module!)
let result = run(FunctionRunParams {
function_path: opts.function.clone(),
input,
export: opts.export.as_ref(),
profile_opts,
scale_factor,
module: validated_module.clone(),
engine: engine.clone(),
});
// Output result immediately (streaming JSONL - compact format for line-by-line parsing)
match result {
Ok(function_result) => {
let function_succeeded = function_result.success;
if function_succeeded {
success_count += 1;
} else {
failed_count += 1;
}
// Use compact JSON (not pretty-printed) for JSONL format
let compact_json = serde_json::to_string(&function_result)
.unwrap_or_else(|error| error.to_string());
println!("{}", compact_json);
if !function_succeeded && !opts.batch_continue_on_error {
anyhow::bail!(
"Function execution failed on line {}. Review the logs for more information.",
line_num
);
}
}
Err(e) => {
failed_count += 1;
if opts.batch_continue_on_error {
eprintln!("Error executing line {}: {}", line_num, e);
println!(r#"{{"success":false,"error":"Execution failed: {}"}}"#, e);
} else {
return Err(e);
}
}
}
}
// Log summary to stderr (so it doesn't interfere with JSONL output on stdout)
eprintln!(
"Batch complete: {} inputs processed, {} successful, {} failed",
processed_count, success_count, failed_count
);
Ok(())
}