Add benchmark reports and test results with metrics and visualizations
This commit is contained in:
1 parent
a6f82d5a0b
commit
1838c1d559
23 files changed
+1242
-187
No files matched your search
+129
-173
@@ -38,6 +38,8 @@ type Choice struct {
|
||||
Message Message `json:"message"`
|
||||
Index int `json:"index"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
// reasoning_content: 思维链内容
|
||||
ReasoningContent string `json:"reasoning_content"`
|
||||
}
|
||||
|
||||
// Usage 表示API响应中的使用情况
|
||||
@@ -69,6 +71,8 @@ type Result struct {
|
||||
ResponseTime time.Duration // 响应时间
|
||||
FirstTokenTime time.Duration // 首个令牌生成时间(TTFT)
|
||||
Response string // 响应内容
|
||||
// 推理模型的思维链内容
|
||||
ReasoningContent string
|
||||
Error error // 错误信息
|
||||
}
|
||||
|
||||
@@ -107,7 +111,7 @@ func NewBenchmark(cfg *config.Config) (*Benchmark, error) {
|
||||
return &Benchmark{
|
||||
config: cfg,
|
||||
generator: gen,
|
||||
httpClient: &http.Client{},
|
||||
httpClient: &http.Client{Timeout: time.Duration(cfg.Timeout) * time.Second},
|
||||
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
}, nil
|
||||
}
|
||||
@@ -145,20 +149,23 @@ func (b *Benchmark) executeRequest(concurrencyStep int) Result {
|
||||
promptType := b.generator.SelectPromptType()
|
||||
result.PromptType = promptType
|
||||
|
||||
// 生成提示词
|
||||
// 生成并统计提示词
|
||||
prompt, err := b.generator.GeneratePrompt(promptType)
|
||||
if err != nil {
|
||||
result.Error = fmt.Errorf("生成提示词失败: %w", err)
|
||||
return result
|
||||
}
|
||||
promptTokens := b.generator.CountTokens(prompt)
|
||||
result.PromptTokenCount = promptTokens
|
||||
result.PromptTokens = promptTokens
|
||||
|
||||
// 创建请求
|
||||
// 构造请求对象
|
||||
reqObj := Request{
|
||||
Model: b.config.API.Model,
|
||||
Messages: []Message{{Role: "user", Content: prompt}},
|
||||
MaxTokens: 2048,
|
||||
Temperature: 0.7,
|
||||
Stream: true, // 启用流式响应
|
||||
MaxTokens: b.config.API.MaxTokens,
|
||||
Temperature: b.config.API.Temperature,
|
||||
Stream: true,
|
||||
}
|
||||
|
||||
reqBody, err := json.Marshal(reqObj)
|
||||
@@ -168,11 +175,9 @@ func (b *Benchmark) executeRequest(concurrencyStep int) Result {
|
||||
}
|
||||
|
||||
requestID := fmt.Sprintf("request-%s", result.Timestamp.Format("20060102-150405.000"))
|
||||
requestLog := fmt.Sprintf("===== 请求 ID: %s =====\n时间: %s\n并发级别: %d\n提示词类型: %s\n端点: %s\n请求体:\n%s\n",
|
||||
requestID, result.Timestamp.Format(time.RFC3339), concurrencyStep, promptType, b.config.API.Endpoint, string(reqBody))
|
||||
logger.LogToFile(requestLog)
|
||||
logger.LogToFile(fmt.Sprintf("===== 请求 ID: %s =====\n并发: %d, 类型: %s, 端点: %s\n请求体:\n%s\n", requestID, concurrencyStep, promptType, b.config.API.Endpoint, string(reqBody)))
|
||||
|
||||
// 构造 http.Request
|
||||
// 构造 HTTP 请求并绑定超时上下文
|
||||
httpReq, err := http.NewRequest("POST", b.config.API.Endpoint, bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
result.Error = fmt.Errorf("创建HTTP请求失败: %w", err)
|
||||
@@ -182,26 +187,36 @@ func (b *Benchmark) executeRequest(concurrencyStep int) Result {
|
||||
httpReq.Header.Set("Authorization", "Bearer "+b.config.API.APIKey)
|
||||
httpReq.Header.Set("Accept", "text/event-stream")
|
||||
|
||||
ctxReq, cancel := context.WithTimeout(httpReq.Context(), time.Duration(b.config.Timeout)*time.Second)
|
||||
defer cancel()
|
||||
httpReq = httpReq.WithContext(ctxReq)
|
||||
|
||||
startTime := time.Now()
|
||||
resp, err := b.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
result.Error = fmt.Errorf("发送请求失败: %w", err)
|
||||
logger.Error("请求失败: %v", err)
|
||||
logger.Error("请求错误: %v", err)
|
||||
return result
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
result.Error = fmt.Errorf("HTTP状态码错误: %d", resp.StatusCode)
|
||||
logger.Warn("HTTP状态码非200: %d", resp.StatusCode)
|
||||
return result
|
||||
}
|
||||
|
||||
// 记录响应时间
|
||||
responseTime := time.Since(startTime)
|
||||
result.ResponseTime = responseTime
|
||||
|
||||
// 记录响应日志
|
||||
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4096)) // 只记录前4K
|
||||
responseLog := fmt.Sprintf("===== 响应 ID: %s =====\n时间: %s\n响应时间: %v\n状态码: %d\n响应头:\n%s\n响应体(前4K):\n%s\n",
|
||||
requestID, time.Now().Format(time.RFC3339), responseTime, resp.StatusCode, resp.Header, string(respBody))
|
||||
logger.LogToFile(responseLog)
|
||||
|
||||
// 重置 resp.Body 以便流式处理
|
||||
resp.Body = io.NopCloser(io.MultiReader(bytes.NewReader(respBody), resp.Body))
|
||||
// 非流式记录前4K
|
||||
if !reqObj.Stream {
|
||||
head, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
logger.LogToFile(fmt.Sprintf("===== 响应头部 ID: %s =====\n状态码: %d\n前4K响应体:\n%s\n", requestID, resp.StatusCode, string(head)))
|
||||
resp.Body = io.NopCloser(io.MultiReader(bytes.NewReader(head), resp.Body))
|
||||
}
|
||||
|
||||
if reqObj.Stream {
|
||||
return b.handleStreamResponse(resp, result, startTime)
|
||||
@@ -230,12 +245,13 @@ func (b *Benchmark) runConcurrencyStep(concurrencyStep int) ([]Result, error) {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
result := b.executeRequest(concurrencyStep)
|
||||
res := b.executeRequest(concurrencyStep)
|
||||
resultsMutex.Lock()
|
||||
results = append(results, result)
|
||||
results = append(results, res)
|
||||
resultsMutex.Unlock()
|
||||
// 简单限速,可根据需要调整
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
// 按泊松分布间隔
|
||||
interval := b.rand.ExpFloat64() / b.config.PoissonLambda
|
||||
time.Sleep(time.Duration(interval * float64(time.Second)))
|
||||
}
|
||||
}
|
||||
}(i)
|
||||
@@ -245,177 +261,105 @@ func (b *Benchmark) runConcurrencyStep(concurrencyStep int) ([]Result, error) {
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// handleStreamResponse 处理流式响应(net/http 实现)
|
||||
// handleStreamResponse 处理流式响应
|
||||
func (b *Benchmark) handleStreamResponse(resp *http.Response, result Result, startTime time.Time) Result {
|
||||
responseContent := ""
|
||||
var builder strings.Builder
|
||||
// 聚合思维链
|
||||
var reasoningBuilder strings.Builder
|
||||
tokenCount := 0
|
||||
firstTokenReceived := false
|
||||
var firstTokenTime time.Duration
|
||||
|
||||
// 记录响应头接收时间 - 这是我们开始计算TTFT的时间点
|
||||
|
||||
reader := bufio.NewReaderSize(resp.Body, 4096)
|
||||
responseHeaderTime := time.Now()
|
||||
logger.Debug("接收到响应头,网络请求耗时: %v", responseHeaderTime.Sub(startTime))
|
||||
|
||||
// 创建一个带缓冲的读取器,这样我们可以更快地读取数据
|
||||
reader := bufio.NewReaderSize(resp.Body, 4096)
|
||||
|
||||
requestID := fmt.Sprintf("request-%s", result.Timestamp.Format("20060102-150405.000"))
|
||||
streamLogHeader := fmt.Sprintf("===== 流式响应开始 ID: %s =====\n时间: %s\n响应头: %v\n",
|
||||
requestID, responseHeaderTime.Format(time.RFC3339), resp.Header)
|
||||
logger.LogToFile(streamLogHeader)
|
||||
|
||||
// 使用更高效的方式处理流式响应
|
||||
|
||||
chunkIdx := 0
|
||||
buffer := make([]byte, 1024)
|
||||
var line []byte
|
||||
|
||||
for {
|
||||
n, err := reader.Read(buffer)
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
logger.Warn("读取响应流出错: %v", err)
|
||||
logger.Warn("读取流式响应出错: %v", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
// 处理读取到的数据
|
||||
data := buffer[:n]
|
||||
lines := bytes.Split(data, []byte("\n"))
|
||||
|
||||
for _, currentLine := range lines {
|
||||
if len(currentLine) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 如果行不完整,先缓存起来
|
||||
if !bytes.HasSuffix(currentLine, []byte("\n")) {
|
||||
line = append(line, currentLine...)
|
||||
continue
|
||||
}
|
||||
|
||||
// 处理完整的行
|
||||
line = append(line, currentLine...)
|
||||
|
||||
// 检查是否为数据行
|
||||
if !bytes.HasPrefix(line, []byte("data: ")) {
|
||||
line = nil
|
||||
continue
|
||||
}
|
||||
|
||||
// 移除 "data: " 前缀
|
||||
data := line[6:]
|
||||
line = nil
|
||||
|
||||
// 检查是否为结束标记
|
||||
if bytes.Equal(data, []byte("[DONE]")) {
|
||||
logger.Debug("收到流式响应结束标记")
|
||||
break
|
||||
}
|
||||
|
||||
// 解析JSON
|
||||
var chunk map[string]interface{}
|
||||
if err := json.Unmarshal(data, &chunk); err != nil {
|
||||
logger.Warn("解析JSON数据块失败: %v, 数据: %s", err, string(data))
|
||||
continue
|
||||
}
|
||||
|
||||
// 提取内容
|
||||
choices, ok := chunk["choices"].([]interface{})
|
||||
if !ok || len(choices) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
choice, ok := choices[0].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
delta, ok := choice["delta"].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
content, ok := delta["content"].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// 记录当前时间
|
||||
now := time.Now()
|
||||
|
||||
responseContent += content
|
||||
tokenCount += len(strings.Split(content, " "))
|
||||
chunkIdx++
|
||||
chunkLog := fmt.Sprintf("[Chunk %d] %s 内容: %s\n", chunkIdx, now.Format(time.RFC3339Nano), content)
|
||||
logger.LogToFile(chunkLog)
|
||||
|
||||
// 记录首个令牌时间 - 从响应头接收时间开始计算
|
||||
if !firstTokenReceived && len(content) > 0 {
|
||||
if !strings.HasPrefix(line, "data: ") {
|
||||
continue
|
||||
}
|
||||
data := strings.TrimPrefix(line, "data: ")
|
||||
data = strings.TrimSuffix(data, "\n")
|
||||
if data == "[DONE]" {
|
||||
logger.Debug("收到流式响应结束标记")
|
||||
break
|
||||
}
|
||||
var chunk map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(data), &chunk); err != nil {
|
||||
logger.Warn("JSON解析失败: %v", err)
|
||||
continue
|
||||
}
|
||||
choices, ok := chunk["choices"].([]interface{})
|
||||
if !ok || len(choices) == 0 {
|
||||
continue
|
||||
}
|
||||
choiceIface := choices[0]
|
||||
choiceMap, ok := choiceIface.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
deltaIface, ok := choiceMap["delta"]
|
||||
deltaMap, ok := deltaIface.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
contentVal, ok := deltaMap["content"]
|
||||
content, ok := contentVal.(string)
|
||||
if !ok || content == "" {
|
||||
continue
|
||||
}
|
||||
now := time.Now()
|
||||
builder.WriteString(content)
|
||||
tokenCount += len(strings.Split(content, " "))
|
||||
chunkIdx++
|
||||
logger.LogToFile(fmt.Sprintf("[Chunk %d] %s 内容: %s", chunkIdx, now.Format(time.RFC3339Nano), content))
|
||||
// 初次令牌检测:推理模型优先考虑思维链内容
|
||||
rcVal, rcOk := deltaMap["reasoning_content"]
|
||||
rcStr, _ := rcVal.(string)
|
||||
if !firstTokenReceived {
|
||||
if rcOk && rcStr != "" {
|
||||
firstTokenTime = now.Sub(responseHeaderTime)
|
||||
firstTokenReceived = true
|
||||
logger.Debug("收到首个令牌,TTFT: %v (从接收响应头开始计算)", firstTokenTime)
|
||||
|
||||
// 记录更详细的TTFT信息
|
||||
ttftLog := fmt.Sprintf("===== TTFT详情 ID: %s =====\n请求开始时间: %s\n响应头接收时间: %s\n首个令牌接收时间: %s\n总请求耗时: %v\n网络请求耗时: %v\nTTFT(从响应头计算): %v\n",
|
||||
requestID,
|
||||
startTime.Format(time.RFC3339Nano),
|
||||
responseHeaderTime.Format(time.RFC3339Nano),
|
||||
now.Format(time.RFC3339Nano),
|
||||
now.Sub(startTime),
|
||||
responseHeaderTime.Sub(startTime),
|
||||
firstTokenTime)
|
||||
logger.LogToFile(ttftLog)
|
||||
logger.Debug("收到首个思维链令牌,TTFT: %v", firstTokenTime)
|
||||
} else if content != "" {
|
||||
firstTokenTime = now.Sub(responseHeaderTime)
|
||||
firstTokenReceived = true
|
||||
logger.Debug("收到首个内容令牌,TTFT: %v", firstTokenTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
streamLogFooter := fmt.Sprintf("===== 流式响应结束 ID: %s =====\n累计响应内容: %s\n", requestID, responseContent)
|
||||
logger.LogToFile(streamLogFooter)
|
||||
|
||||
// 计算总响应时间
|
||||
result.ResponseTime = time.Since(startTime)
|
||||
|
||||
// 设置TTFT
|
||||
if firstTokenReceived {
|
||||
// 使用更精确的TTFT计算方式
|
||||
// 实际测试中,我们发现首个令牌的生成时间应该非常短
|
||||
// 如果测量值超过1秒,可能是由于网络延迟或其他因素导致的
|
||||
// 因此,我们使用一个更合理的估计值
|
||||
if firstTokenTime > 1*time.Second {
|
||||
// 使用更合理的估计值,基于实际测试结果
|
||||
estimatedTTFT := 200 * time.Millisecond
|
||||
logger.Debug("测量的TTFT(%v)可能不准确,使用估计值: %v", firstTokenTime, estimatedTTFT)
|
||||
result.FirstTokenTime = estimatedTTFT
|
||||
} else {
|
||||
result.FirstTokenTime = firstTokenTime
|
||||
// 聚合思维链字段
|
||||
if rcOk && rcStr != "" {
|
||||
reasoningBuilder.WriteString(rcStr)
|
||||
}
|
||||
} else {
|
||||
// 如果没有检测到首个令牌,使用默认估计值
|
||||
estimatedTTFT := 500 * time.Millisecond
|
||||
logger.Warn("未检测到首个令牌,使用估计值作为TTFT: %v", estimatedTTFT)
|
||||
result.FirstTokenTime = estimatedTTFT
|
||||
}
|
||||
|
||||
// 设置结果
|
||||
responseContent := builder.String()
|
||||
|
||||
result.Response = responseContent
|
||||
result.PromptTokenCount = len(result.PromptType) / 2 // 简单估计
|
||||
result.PromptTokens = len(result.PromptType) / 2 // 简单估计
|
||||
// CompletionTokens 和总Token
|
||||
result.CompletionTokens = tokenCount
|
||||
result.TotalTokens = result.PromptTokens + result.CompletionTokens
|
||||
|
||||
// 记录处理后的响应
|
||||
processedLog := fmt.Sprintf("===== 处理后的响应 ID: request-%s =====\n响应时间: %v\nTTFT: %v\n提示词Token: %d\n完成Token: %d\n总Token: %d\n响应内容:\n%s\n",
|
||||
result.Timestamp.Format("20060102-150405.000"), result.ResponseTime, result.FirstTokenTime,
|
||||
result.PromptTokens, result.CompletionTokens, result.TotalTokens, result.Response)
|
||||
logger.LogToFile(processedLog)
|
||||
|
||||
logger.Debug("流式响应处理完成,总响应时间: %v, TTFT: %v, 提示词Token: %d, 完成Token: %d, 总Token: %d",
|
||||
result.ResponseTime, result.FirstTokenTime, result.PromptTokens, result.CompletionTokens, result.TotalTokens)
|
||||
|
||||
// 设置思维链内容
|
||||
result.ReasoningContent = reasoningBuilder.String()
|
||||
// 设置响应时间和首个令牌时间 (TTFT)
|
||||
result.ResponseTime = time.Since(startTime)
|
||||
if firstTokenReceived {
|
||||
result.FirstTokenTime = firstTokenTime
|
||||
} else {
|
||||
// 未检测到首个令牌,使用响应时间作为 TTFT
|
||||
result.FirstTokenTime = result.ResponseTime
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// handleNormalResponse 处理普通响应(net/http 实现)
|
||||
// handleNormalResponse 处理普通响应
|
||||
func (b *Benchmark) handleNormalResponse(resp *http.Response, result Result) Result {
|
||||
var response Response
|
||||
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
|
||||
@@ -423,11 +367,23 @@ func (b *Benchmark) handleNormalResponse(resp *http.Response, result Result) Res
|
||||
logger.Error("解析响应失败: %v", err)
|
||||
return result
|
||||
}
|
||||
result.PromptTokens = response.Usage.PromptTokens
|
||||
result.CompletionTokens = response.Usage.CompletionTokens
|
||||
result.TotalTokens = response.Usage.TotalTokens
|
||||
// 先获取响应内容
|
||||
if len(response.Choices) > 0 {
|
||||
result.Response = response.Choices[0].Message.Content
|
||||
choice := response.Choices[0]
|
||||
result.Response = choice.Message.Content
|
||||
// 推理模型思维链
|
||||
result.ReasoningContent = choice.ReasoningContent
|
||||
}
|
||||
// 根据 Usage 判断是否兼容推理模型
|
||||
if response.Usage.TotalTokens > 0 {
|
||||
result.PromptTokens = response.Usage.PromptTokens
|
||||
result.CompletionTokens = response.Usage.CompletionTokens
|
||||
result.TotalTokens = response.Usage.TotalTokens
|
||||
} else {
|
||||
// 推理模型未返回 Usage,使用本地统计
|
||||
result.PromptTokens = result.PromptTokenCount
|
||||
result.CompletionTokens = b.generator.CountTokens(result.Response)
|
||||
result.TotalTokens = result.PromptTokens + result.CompletionTokens
|
||||
}
|
||||
result.FirstTokenTime = result.ResponseTime
|
||||
logger.Debug("非流式响应,使用响应时间作为TTFT: %v", result.FirstTokenTime)
|
||||
|
||||
Reference in new issue
Block a user