|
| 1 | +#include <catch2/catch_test_macros.hpp> |
| 2 | +#include <chrono> |
| 3 | +#include <cstdlib> |
| 4 | +#include <iostream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include "openai/OpenAIClient.h" |
| 8 | +#include "openai/OpenAITypes.h" |
| 9 | + |
| 10 | +using namespace std::chrono; |
| 11 | + |
| 12 | +static bool isReasoningModel(OpenAI::Model model) { |
| 13 | + return model == OpenAI::Model::GPT_5 || model == OpenAI::Model::GPT_5_Mini || |
| 14 | + model == OpenAI::Model::GPT_5_Nano || model == OpenAI::Model::O3 || |
| 15 | + model == OpenAI::Model::O3_Mini || model == OpenAI::Model::O1 || |
| 16 | + model == OpenAI::Model::O1_Mini || model == OpenAI::Model::O1_Preview || |
| 17 | + model == OpenAI::Model::O1_Pro || model == OpenAI::Model::O4_Mini; |
| 18 | +} |
| 19 | + |
| 20 | +TEST_CASE("OpenAI model benchmarks (structured outputs)", "[openai][integration][benchmark]") { |
| 21 | + const char* runBenchEnv = std::getenv("LLMCPP_RUN_BENCHMARKS"); |
| 22 | + if (!runBenchEnv || std::string(runBenchEnv) != "1") { |
| 23 | + SUCCEED("Benchmarks skipped. Set LLMCPP_RUN_BENCHMARKS=1 to enable."); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const char* apiKey = std::getenv("OPENAI_API_KEY"); |
| 28 | + REQUIRE(apiKey != nullptr); |
| 29 | + |
| 30 | + OpenAI::OpenAIClient client(apiKey); |
| 31 | + |
| 32 | + // Minimal structured output schema |
| 33 | + json schema = {{"type", "object"}, |
| 34 | + {"properties", {{"answer", {{"type", "string"}}}}}, |
| 35 | + {"required", json::array({"answer"})}, |
| 36 | + {"additionalProperties", false}}; |
| 37 | + |
| 38 | + // Simple input |
| 39 | + auto input = OpenAI::ResponsesInput::fromText("Reply with the word OK."); |
| 40 | + |
| 41 | + // Iterate through response-capable models |
| 42 | + for (const auto& modelName : OpenAI::RESPONSES_MODELS) { |
| 43 | + DYNAMIC_SECTION("Benchmark model: " << modelName) { |
| 44 | + OpenAI::ResponsesRequest req; |
| 45 | + req.model = modelName; |
| 46 | + req.input = input; |
| 47 | + req.text = OpenAI::TextOutputConfig("bench_schema", schema, true); |
| 48 | + req.maxOutputTokens = 16; |
| 49 | + |
| 50 | + // Tweak reasoning parameters when appropriate |
| 51 | + auto modelEnum = OpenAI::modelFromString(modelName); |
| 52 | + if (isReasoningModel(modelEnum)) { |
| 53 | + req.reasoning = json{{"effort", "low"}}; |
| 54 | + } |
| 55 | + |
| 56 | + const auto start = steady_clock::now(); |
| 57 | + auto response = client.sendResponsesRequest(req); |
| 58 | + const auto end = steady_clock::now(); |
| 59 | + |
| 60 | + const auto elapsedMs = duration_cast<milliseconds>(end - start).count(); |
| 61 | + std::cout << "[BENCH] model=" << modelName << ", ms=" << elapsedMs |
| 62 | + << ", success=" << (response.isCompleted() && !response.hasError()) << std::endl; |
| 63 | + |
| 64 | + // Sanity: we should at least get a response object back; don't assert success to avoid |
| 65 | + // flakes |
| 66 | + REQUIRE(!response.id.empty()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#include <catch2/catch_test_macros.hpp> |
| 72 | +#include <chrono> |
| 73 | +#include <cstdlib> |
| 74 | +#include <iostream> |
| 75 | +#include <string> |
| 76 | + |
| 77 | +#include "openai/OpenAIClient.h" |
| 78 | +#include "openai/OpenAITypes.h" |
| 79 | + |
| 80 | +using namespace std::chrono; |
| 81 | + |
| 82 | +static bool isReasoningModel(OpenAI::Model model) { |
| 83 | + return model == OpenAI::Model::GPT_5 || model == OpenAI::Model::GPT_5_Mini || |
| 84 | + model == OpenAI::Model::GPT_5_Nano || model == OpenAI::Model::O3 || |
| 85 | + model == OpenAI::Model::O3_Mini || model == OpenAI::Model::O1 || |
| 86 | + model == OpenAI::Model::O1_Mini || model == OpenAI::Model::O1_Preview || |
| 87 | + model == OpenAI::Model::O1_Pro || model == OpenAI::Model::O4_Mini; |
| 88 | +} |
| 89 | + |
| 90 | +TEST_CASE("OpenAI model benchmarks (structured outputs)", "[openai][integration][benchmark]") { |
| 91 | + const char* runBenchEnv = std::getenv("LLMCPP_RUN_BENCHMARKS"); |
| 92 | + if (!runBenchEnv || std::string(runBenchEnv) != "1") { |
| 93 | + SUCCEED("Benchmarks skipped. Set LLMCPP_RUN_BENCHMARKS=1 to enable."); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const char* apiKey = std::getenv("OPENAI_API_KEY"); |
| 98 | + REQUIRE(apiKey != nullptr); |
| 99 | + |
| 100 | + OpenAIClient client(apiKey); |
| 101 | + |
| 102 | + // Minimal structured output schema |
| 103 | + json schema = {{"type", "object"}, |
| 104 | + {"properties", {{"answer", {{"type", "string"}}}}}, |
| 105 | + {"required", json::array({"answer"})}, |
| 106 | + {"additionalProperties", false}}; |
| 107 | + |
| 108 | + // Simple input |
| 109 | + auto input = OpenAI::ResponsesInput::fromText("Reply with the word OK."); |
| 110 | + |
| 111 | + // Iterate through response-capable models |
| 112 | + for (const auto& modelName : OpenAI::RESPONSES_MODELS) { |
| 113 | + DYNAMIC_SECTION("Benchmark model: " << modelName) { |
| 114 | + OpenAI::ResponsesRequest req; |
| 115 | + req.model = modelName; |
| 116 | + req.input = input; |
| 117 | + req.text = OpenAI::TextOutputConfig("bench_schema", schema, true); |
| 118 | + req.maxOutputTokens = 16; |
| 119 | + |
| 120 | + // Tweak reasoning parameters when appropriate |
| 121 | + auto modelEnum = OpenAI::modelFromString(modelName); |
| 122 | + if (isReasoningModel(modelEnum)) { |
| 123 | + req.reasoning = json{{"effort", "low"}}; |
| 124 | + } |
| 125 | + |
| 126 | + const auto start = steady_clock::now(); |
| 127 | + auto response = client.sendResponsesRequest(req); |
| 128 | + const auto end = steady_clock::now(); |
| 129 | + |
| 130 | + const auto elapsedMs = duration_cast<milliseconds>(end - start).count(); |
| 131 | + std::cout << "[BENCH] model=" << modelName << ", ms=" << elapsedMs |
| 132 | + << ", success=" << (response.isCompleted() && !response.hasError()) |
| 133 | + << std::endl; |
| 134 | + |
| 135 | + // Sanity: we should at least get a response object back; don't assert success to avoid |
| 136 | + // flakes |
| 137 | + REQUIRE(!response.id.empty()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments