| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use criterion::{BatchSize, Criterion, Throughput, black_box, criterion_group, criterion_main}; |
| 7 | use tool_parser::parsers::MinimaxM2Parser as ExternalMinimaxM2Parser; |
| 8 | use vllm_parser::tool::test_utils::{split_by_chars, test_tools}; |
| 9 | use vllm_parser::tool::{MinimaxM2ToolParser, Tool, ToolParser}; |
| 10 | |
| 11 | mod utils; |
| 12 | use utils::{feed_external_parser, feed_parser, openai_tools}; |
| 13 | |
| 14 | const CHUNK_CHARS: usize = 7; |
| 15 | const LONG_NORMAL_TEXT_REPEATS: usize = 2048; |
| 16 | |
| 17 | fn mixed_fixture() -> String { |
| 18 | concat!( |
| 19 | "I will check two cities before answering.\n", |
| 20 | "<minimax:tool_call>", |
| 21 | "<invoke name=\"get_weather\">", |
| 22 | "<parameter name=\"city\">Hangzhou</parameter>", |
| 23 | "<parameter name=\"date\">2026-04-30</parameter>", |
| 24 | "<parameter name=\"unit\">celsius</parameter>", |
| 25 | "<parameter name=\"days\">3</parameter>", |
| 26 | "</invoke>", |
| 27 | "<invoke name=\"get_weather\">", |
| 28 | "<parameter name=\"city\">San Francisco</parameter>", |
| 29 | "<parameter name=\"date\">2026-04-30</parameter>", |
| 30 | "<parameter name=\"unit\">fahrenheit</parameter>", |
| 31 | "<parameter name=\"days\">2</parameter>", |
| 32 | "</invoke>", |
| 33 | "</minimax:tool_call>", |
| 34 | ) |
| 35 | .to_string() |
| 36 | } |
| 37 | |
| 38 | fn long_normal_text_fixture() -> String { |
| 39 | let line = "This is ordinary assistant text with no MiniMax M2 tool markers at all.\n"; |
| 40 | line.repeat(LONG_NORMAL_TEXT_REPEATS) |
| 41 | } |
| 42 | |
| 43 | fn native_parser(tools: &[Tool]) -> Box<dyn ToolParser> { |
| 44 | MinimaxM2ToolParser::create(tools).expect("MiniMax M2 parser should initialize") |
| 45 | } |
| 46 | |
| 47 | fn run_stream_group( |
| 48 | c: &mut Criterion, |
| 49 | name: &str, |
| 50 | tools: &[Tool], |
| 51 | text: &str, |
| 52 | chunk_chars: usize, |
| 53 | expected_normal_text: &str, |
| 54 | expected_native_calls_len: usize, |
| 55 | ) { |
| 56 | let chunks = split_by_chars(text, chunk_chars); |
| 57 | let openai_tools = openai_tools(tools); |
| 58 | |
| 59 | let mut group = c.benchmark_group(name); |
| 60 | group.sample_size(50); |
| 61 | group.warm_up_time(Duration::from_millis(300)); |
| 62 | group.measurement_time(Duration::from_secs(2)); |
| 63 | group.throughput(Throughput::Bytes(text.len() as u64)); |
| 64 | |
| 65 | group.bench_function("native_reuse_parser", |b| { |
| 66 | let mut parser = native_parser(tools); |
| 67 | b.iter(|| { |
| 68 | let result = feed_parser(&mut *parser, black_box(&chunks)); |
| 69 | debug_assert_eq!(result.0, expected_normal_text); |
| 70 | debug_assert_eq!(result.1, expected_native_calls_len); |
| 71 | black_box(result); |
| 72 | }) |
| 73 | }); |
| 74 | |
| 75 | group.bench_function("native_create_parser", |b| { |
| 76 | b.iter_batched( |
| 77 | || native_parser(tools), |
| 78 | |mut parser| { |
| 79 | let result = feed_parser(&mut *parser, black_box(&chunks)); |
| 80 | debug_assert_eq!(result.0, expected_normal_text); |
| 81 | debug_assert_eq!(result.1, expected_native_calls_len); |
| 82 | black_box(result); |
| 83 | }, |
| 84 | BatchSize::SmallInput, |
| 85 | ) |
| 86 | }); |
| 87 | |
| 88 | group.bench_function("external_reuse_parser", |b| { |
| 89 | let mut parser = ExternalMinimaxM2Parser::new(); |
| 90 | b.iter(|| { |
| 91 | let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks)); |
| 92 | debug_assert_eq!(result.0, expected_normal_text); |
| 93 | black_box(result); |
| 94 | }) |
| 95 | }); |
| 96 | |
| 97 | group.bench_function("external_create_parser", |b| { |
| 98 | b.iter_batched( |
| 99 | ExternalMinimaxM2Parser::new, |
| 100 | |mut parser| { |
| 101 | let result = feed_external_parser(&mut parser, &openai_tools, black_box(&chunks)); |
| 102 | debug_assert_eq!(result.0, expected_normal_text); |
| 103 | black_box(result); |
| 104 | }, |
| 105 | BatchSize::SmallInput, |
| 106 | ) |
| 107 | }); |
| 108 | |
| 109 | group.finish(); |
| 110 | } |
| 111 | |
| 112 | fn bench_minimax_m2(c: &mut Criterion) { |
| 113 | let tools = test_tools(); |
| 114 | let mixed_text = mixed_fixture(); |
| 115 | let long_normal_text = long_normal_text_fixture(); |
| 116 | |
| 117 | run_stream_group( |
| 118 | c, |
| 119 | "minimax_m2/mixed_text_tool_call", |
| 120 | &tools, |
| 121 | &mixed_text, |
| 122 | CHUNK_CHARS, |
| 123 | "I will check two cities before answering.\n", |
| 124 | 2, |
| 125 | ); |
| 126 | |
| 127 | run_stream_group( |
| 128 | c, |
| 129 | "minimax_m2/long_normal_text", |
| 130 | &tools, |
| 131 | &long_normal_text, |
| 132 | CHUNK_CHARS, |
| 133 | &long_normal_text, |
| 134 | 0, |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | criterion_group!(benches, bench_minimax_m2); |
| 139 | criterion_main!(benches); |