From d41ba72b733ff87167deca3e91ab2d3138e763d3 Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Sat, 2 May 2026 19:31:11 +0000 Subject: [PATCH] refactor: rewrite stress_test.c to use dispatcher API with Action structs Co-authored-by: aider (deepseek/deepseek-coder) --- stress_test.c | 113 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 93 insertions(+), 20 deletions(-) diff --git a/stress_test.c b/stress_test.c index 8c7dbba..0d8bdc7 100644 --- a/stress_test.c +++ b/stress_test.c @@ -46,6 +46,58 @@ static void teardown_dispatcher(DispatchFn dispatch) { dispatcher_stop(); } +// Helper to build an Action +static Action make_action(ActionType type, int int_arg, jack_nframes_t nframes_arg) { + Action action; + memset(&action, 0, sizeof(action)); + action.type = type; + // Set the appropriate union member based on type + switch (type) { + case ACTION_TRIGGER_CLIP: + action.data.trigger_clip.clip_index = int_arg; + break; + case ACTION_TRIGGER_SCENE: + action.data.trigger_scene.scene_index = int_arg; + break; + case ACTION_RESET_CLIP: + action.data.reset_clip.clip_index = int_arg; + break; + case ACTION_SET_QUANTIZE_MODE: + action.data.set_quantize_mode.mode = (QuantizeMode)int_arg; + break; + case ACTION_SET_QUANTIZE_THRESHOLD: + action.data.set_quantize_threshold.threshold = nframes_arg; + break; + case ACTION_SET_CLOCK_SOURCE: + action.data.set_clock_source.source = (ClockSource)int_arg; + break; + case ACTION_SET_BPM: + action.data.set_bpm.bpm = (double)int_arg; + break; + case ACTION_SAVE_CLIP: + action.data.save_clip.clip_index = int_arg; + break; + case ACTION_LOAD_CLIP: + action.data.load_clip.clip_index = int_arg; + break; + case ACTION_MIDI_NOTE_ON: + action.data.midi_note_on.note = int_arg; + action.data.midi_note_on.time = nframes_arg; + break; + case ACTION_MIDI_SCENE_LAUNCH: + action.data.midi_scene_launch.scene_index = int_arg; + action.data.midi_scene_launch.time = nframes_arg; + break; + case ACTION_PROCESS_AUDIO: + action.data.process_audio.nframes = nframes_arg; + break; + default: + // For actions with no data (TRANSPORT_PLAY, etc.), nothing to set + break; + } + return action; +} + // Stress test 1: Rapid clip triggers via dispatch static void stress_rapid_triggers(void) { printf("Stress test 1: Rapid clip triggers...\n"); @@ -55,7 +107,8 @@ static void stress_rapid_triggers(void) { int num_ops = 10000; for (int i = 0; i < num_ops; i++) { int clip_idx = i % MAX_CLIPS; - dispatch(ACTION_TRIGGER_CLIP, clip_idx, 0); + Action action = make_action(ACTION_TRIGGER_CLIP, clip_idx, 0); + dispatch(action); } // Give dispatcher time to process @@ -83,10 +136,12 @@ static void stress_mixed_triggers(void) { for (int i = 0; i < num_ops; i++) { if (i % 3 == 0) { int scene_idx = (i / 3) % MAX_SCENES; - dispatch(ACTION_TRIGGER_SCENE, scene_idx, 0); + Action action = make_action(ACTION_TRIGGER_SCENE, scene_idx, 0); + dispatch(action); } else { int clip_idx = i % MAX_CLIPS; - dispatch(ACTION_TRIGGER_CLIP, clip_idx, 0); + Action action = make_action(ACTION_TRIGGER_CLIP, clip_idx, 0); + dispatch(action); } } @@ -105,7 +160,8 @@ static void stress_queue_overflow(void) { // Submit more than queue capacity (256) int num_ops = 500; for (int i = 0; i < num_ops; i++) { - dispatch(ACTION_TRIGGER_CLIP, i % MAX_CLIPS, 0); + Action action = make_action(ACTION_TRIGGER_CLIP, i % MAX_CLIPS, 0); + dispatch(action); } usleep(100000); @@ -123,19 +179,22 @@ static void stress_undo_redo(void) { int num_ops = 1000; for (int i = 0; i < num_ops; i++) { int clip_idx = i % MAX_CLIPS; - dispatch(ACTION_TRIGGER_CLIP, clip_idx, 0); + Action action = make_action(ACTION_TRIGGER_CLIP, clip_idx, 0); + dispatch(action); usleep(1000); // Undo every other operation if (i % 2 == 0) { - dispatch(ACTION_UNDO, 0, 0); + Action undo_action = make_action(ACTION_UNDO, 0, 0); + dispatch(undo_action); usleep(1000); } } // Redo some for (int i = 0; i < 100; i++) { - dispatch(ACTION_REDO, 0, 0); + Action redo_action = make_action(ACTION_REDO, 0, 0); + dispatch(redo_action); usleep(1000); } @@ -153,23 +212,27 @@ static void stress_transport(void) { int num_ops = 5000; for (int i = 0; i < num_ops; i++) { + Action action; + memset(&action, 0, sizeof(action)); switch (i % 5) { case 0: - dispatch(ACTION_TRANSPORT_PLAY, 0, 0); + action.type = ACTION_TRANSPORT_PLAY; break; case 1: - dispatch(ACTION_TRANSPORT_PAUSE, 0, 0); + action.type = ACTION_TRANSPORT_PAUSE; break; case 2: - dispatch(ACTION_TRANSPORT_STOP, 0, 0); + action.type = ACTION_TRANSPORT_STOP; break; case 3: - dispatch(ACTION_TRANSPORT_TOGGLE_PLAY, 0, 0); + action.type = ACTION_TRANSPORT_TOGGLE_PLAY; break; case 4: - dispatch(ACTION_SET_CLOCK_SOURCE, (int)CLOCK_SOURCE_MIDI, 0); + action.type = ACTION_SET_CLOCK_SOURCE; + action.data.set_clock_source.source = CLOCK_SOURCE_MIDI; break; } + dispatch(action); usleep(100); } @@ -188,8 +251,10 @@ static void stress_quantize(void) { int num_ops = 5000; for (int i = 0; i < num_ops; i++) { QuantizeMode mode = (QuantizeMode)(i % 3); - dispatch(ACTION_SET_QUANTIZE_MODE, (int)mode, 0); - dispatch(ACTION_SET_QUANTIZE_THRESHOLD, 0, (jack_nframes_t)(i * 100)); + Action mode_action = make_action(ACTION_SET_QUANTIZE_MODE, (int)mode, 0); + dispatch(mode_action); + Action thresh_action = make_action(ACTION_SET_QUANTIZE_THRESHOLD, 0, (jack_nframes_t)(i * 100)); + dispatch(thresh_action); usleep(100); } @@ -208,11 +273,13 @@ static void stress_reset_while_triggering(void) { int num_ops = 2000; for (int i = 0; i < num_ops; i++) { int clip_idx = i % MAX_CLIPS; - dispatch(ACTION_TRIGGER_CLIP, clip_idx, 0); + Action trigger_action = make_action(ACTION_TRIGGER_CLIP, clip_idx, 0); + dispatch(trigger_action); usleep(500); if (i % 10 == 0) { - dispatch(ACTION_RESET_CLIP, clip_idx, 0); + Action reset_action = make_action(ACTION_RESET_CLIP, clip_idx, 0); + dispatch(reset_action); usleep(500); } } @@ -234,7 +301,8 @@ static void* thread_worker(void *arg) { ThreadArg *targ = (ThreadArg *)arg; for (int i = 0; i < targ->num_ops; i++) { int clip_idx = (targ->thread_id * 1000 + i) % MAX_CLIPS; - targ->dispatch(ACTION_TRIGGER_CLIP, clip_idx, 0); + Action action = make_action(ACTION_TRIGGER_CLIP, clip_idx, 0); + targ->dispatch(action); } return NULL; } @@ -276,9 +344,14 @@ static void stress_create_destroy(void) { // Do some operations for (int j = 0; j < 10; j++) { - dispatch(ACTION_TRIGGER_CLIP, j % MAX_CLIPS, 0); - dispatch(ACTION_TRANSPORT_TOGGLE_PLAY, 0, 0); - dispatch(ACTION_SET_QUANTIZE_MODE, j % 3, 0); + Action trigger_action = make_action(ACTION_TRIGGER_CLIP, j % MAX_CLIPS, 0); + dispatch(trigger_action); + Action toggle_action; + memset(&toggle_action, 0, sizeof(toggle_action)); + toggle_action.type = ACTION_TRANSPORT_TOGGLE_PLAY; + dispatch(toggle_action); + Action mode_action = make_action(ACTION_SET_QUANTIZE_MODE, j % 3, 0); + dispatch(mode_action); } usleep(1000);