Skip to content

Commit

Permalink
Show token consumption in AI Test Toolkit (#2022)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
Log token consumption while running the tests using AI Test Toolkit

Screenshots:


![image](https://github.com/user-attachments/assets/8bea52a9-20aa-47c1-ace7-f9a52ebfd996)


![image](https://github.com/user-attachments/assets/3c84178d-b480-4738-b0d5-4ae1a9281552)


![image](https://github.com/user-attachments/assets/26175d46-c6e7-4f17-88b7-66912a5d4b21)



#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#540454](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/540454)
  • Loading branch information
t-prda authored Sep 12, 2024
1 parent be13b5a commit 8a8b8da
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ codeunit 7759 "AOAI Token"
begin
exit(AzureOpenAIImpl.GetTokenCount(Input, Encodingp50kbaseLbl));
end;


/// <summary>
/// Gets the total tokens used till now for the server session.
/// The total tokens used is aggregated for all the models.
/// Note: this method is expected to change in future.
/// </summary>
/// <returns>The total token consumed for the session.</returns>
procedure GetTotalServerSessionTokensConsumed(): Integer
begin
exit(AzureOpenAIImpl.GetTotalServerSessionTokensConsumed());
end;
}
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ codeunit 7772 "Azure OpenAI Impl"
TokenCount := ALCopilotFunctions.GptTokenCount(Input, Encoding);
end;

procedure GetTotalServerSessionTokensConsumed(): Integer
begin
exit(SessionInformation.AITokensUsed);
end;

[NonDebuggable]
internal procedure IsTenantAllowlistedForFirstPartyCopilotCalls(): Boolean
var
Expand Down
19 changes: 19 additions & 0 deletions src/Tools/AI Test Toolkit/src/AITTestRunIteration.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace System.TestTools.AITestToolkit;

using System.AI;
using System.TestTools.TestRunner;

codeunit 149042 "AIT Test Run Iteration"
Expand All @@ -19,6 +20,8 @@ codeunit 149042 "AIT Test Run Iteration"
ActiveAITTestSuite: Record "AIT Test Suite";
GlobalTestMethodLine: Record "Test Method Line";
NoOfInsertedLogEntries: Integer;
GlobalAITokenUsedByLastTestMethodLine: Integer;
GlobalSessionAITokenUsed: Integer;

trigger OnRun()
begin
Expand All @@ -27,6 +30,7 @@ codeunit 149042 "AIT Test Run Iteration"
SetAITTestMethodLine(Rec);

NoOfInsertedLogEntries := 0;
GlobalAITokenUsedByLastTestMethodLine := 0;

InitializeAITTestMethodLineForRun(Rec, ActiveAITTestSuite);
SetAITTestSuite(ActiveAITTestSuite);
Expand Down Expand Up @@ -115,6 +119,11 @@ codeunit 149042 "AIT Test Run Iteration"
exit(GlobalTestMethodLine);
end;

procedure GetAITokenUsedByLastTestMethodLine(): Integer
begin
exit(GlobalAITokenUsedByLastTestMethodLine);
end;

[InternalEvent(false)]
procedure OnBeforeRunIteration(var AITTestSuite: Record "AIT Test Suite"; var AITTestMethodLine: Record "AIT Test Method Line")
begin
Expand All @@ -124,6 +133,7 @@ codeunit 149042 "AIT Test Run Iteration"
local procedure OnBeforeTestMethodRun(var CurrentTestMethodLine: Record "Test Method Line"; CodeunitID: Integer; CodeunitName: Text[30]; FunctionName: Text[128]; FunctionTestPermissions: TestPermissions)
var
AITContextCU: Codeunit "AIT Test Context Impl.";
AOAIToken: Codeunit "AOAI Token";
begin
if ActiveAITTestSuite.Code = '' then
exit;
Expand All @@ -132,13 +142,18 @@ codeunit 149042 "AIT Test Run Iteration"

GlobalTestMethodLine := CurrentTestMethodLine;

// Update AI Token Consumption
GlobalAITokenUsedByLastTestMethodLine := 0;
GlobalSessionAITokenUsed := AOAIToken.GetTotalServerSessionTokensConsumed();

AITContextCU.StartRunProcedureScenario();
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Test Runner - Mgt", OnAfterTestMethodRun, '', false, false)]
local procedure OnAfterTestMethodRun(var CurrentTestMethodLine: Record "Test Method Line"; CodeunitID: Integer; CodeunitName: Text[30]; FunctionName: Text[128]; FunctionTestPermissions: TestPermissions; IsSuccess: Boolean)
var
AITContextCU: Codeunit "AIT Test Context Impl.";
AOAIToken: Codeunit "AOAI Token";
begin
if ActiveAITTestSuite.Code = '' then
exit;
Expand All @@ -147,6 +162,10 @@ codeunit 149042 "AIT Test Run Iteration"
exit;

GlobalTestMethodLine := CurrentTestMethodLine;

// Update AI Token Consumption
GlobalAITokenUsedByLastTestMethodLine := AOAIToken.GetTotalServerSessionTokensConsumed() - GlobalSessionAITokenUsed;

AITContextCU.EndRunProcedureScenario(CurrentTestMethodLine, IsSuccess);
Commit();
end;
Expand Down
4 changes: 4 additions & 0 deletions src/Tools/AI Test Toolkit/src/API/AITLogEntryAPI.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ page 149038 "AIT Log Entry API"
{
Caption = 'Version';
}
field(tokensConsumed; Rec."Tokens Consumed")
{
Caption = 'Total Tokens Consumed';
}
field("startTime"; Rec."Start Time")
{
Caption = 'Start Time';
Expand Down
3 changes: 3 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITLogEntries.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ page 149033 "AIT Log Entries"
Message(Rec.GetOutputBlob());
end;
}
field("Tokens Consumed"; Rec."Tokens Consumed")
{
}
field(TestRunDuration; TestRunDuration)
{
Caption = 'Duration';
Expand Down
5 changes: 5 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITLogEntry.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ table 149034 "AIT Log Entry"
{
Caption = 'Output Data';
}
field(50; "Tokens Consumed"; Integer)
{
Caption = 'Total Tokens Consumed';
ToolTip = 'Specifies the aggregated number of tokens consumed by the test. This is applicable only when using Microsoft AI Module.';
}
}

keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ page 149035 "AIT Test Method Lines Compare"
Caption = 'Total Duration (ms)';
ToolTip = 'Specifies Total Duration of the test for given version.';
}
label(TokensConsumed)
{
Caption = 'Total Tokens Consumed';
ToolTip = 'Specifies the aggregated number of tokens consumed by the test. This is applicable only when using Microsoft AI Module.';
}
}
group("Latest Version")
{
Expand Down Expand Up @@ -107,6 +112,10 @@ page 149035 "AIT Test Method Lines Compare"
ToolTip = 'Specifies Total Duration of the tests for this version.';
ShowCaption = false;
}
field("Tokens Consumed"; Rec."Tokens Consumed")
{
ShowCaption = false;
}
}
group("Base Version")
{
Expand Down Expand Up @@ -142,6 +151,10 @@ page 149035 "AIT Test Method Lines Compare"
Caption = 'Total Duration Base (ms)';
ShowCaption = false;
}
field("Tokens Consumed - Base"; Rec."Tokens Consumed - Base")
{
ShowCaption = false;
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/Tools/AI Test Toolkit/src/Logs/AITTestSuiteCompare.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ page 149036 "AIT Test Suite Compare"
Caption = 'Total Duration (ms)';
ToolTip = 'Specifies Total Duration of the tests for the version.';
}
label(TokensConsumed)
{
Caption = 'Total Tokens Consumed';
ToolTip = 'Specifies the aggregated number of tokens consumed by the test. This is applicable only when using Microsoft AI Module.';
}
}
group("Latest Version")
{
Expand Down Expand Up @@ -110,6 +115,10 @@ page 149036 "AIT Test Suite Compare"
ToolTip = 'Specifies Total Duration of the tests for this version.';
ShowCaption = false;
}
field("Tokens Consumed"; Rec."Tokens Consumed")
{
ShowCaption = false;
}
}
group("Base Version")
{
Expand Down Expand Up @@ -146,6 +155,10 @@ page 149036 "AIT Test Suite Compare"
Caption = 'Total Duration Base (ms)';
ShowCaption = false;
}
field("Tokens Consumed - Base"; Rec."Tokens Consumed - Base")
{
ShowCaption = false;
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/Tools/AI Test Toolkit/src/TestSuite/AITTestMethodLine.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ table 149032 "AIT Test Method Line"
Caption = 'AL Test Suite';
Editable = false;
}
field(120; "Tokens Consumed"; Integer)
{
Caption = 'Total Tokens Consumed';
ToolTip = 'Specifies the number of tokens consumed by the test in the current version. This is applicable only when using Microsoft AI Module.';
Editable = false;
FieldClass = FlowField;
CalcFormula = sum("AIT Log Entry"."Tokens Consumed" where("Test Suite Code" = field("Test Suite Code"), "Test Method Line No." = field("Line No."), Version = field("Version Filter"), Operation = const('Run Procedure'), "Procedure Name" = filter(<> '')));
}
field(121; "Tokens Consumed - Base"; Integer)
{
Caption = 'Tokens Consumed - Base';
ToolTip = 'Specifies the number of tokens consumed by the test in the base version. This is applicable only when using Microsoft AI Module.';
Editable = false;
FieldClass = FlowField;
CalcFormula = sum("AIT Log Entry"."Tokens Consumed" where("Test Suite Code" = field("Test Suite Code"), "Test Method Line No." = field("Line No."), Version = field("Base Version Filter"), Operation = const('Run Procedure'), "Procedure Name" = filter(<> '')));
}
}

keys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ page 149034 "AIT Test Method Lines"
field(Duration; Rec."Total Duration (ms)")
{
}
field("Tokens Consumed"; Rec."Tokens Consumed")
{
}
field(AvgDuration; AITTestSuiteMgt.GetAvgDuration(Rec))
{
Caption = 'Average Duration (ms)';
Expand Down
21 changes: 18 additions & 3 deletions src/Tools/AI Test Toolkit/src/TestSuite/AITTestSuite.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ page 149031 "AIT Test Suite"
Caption = 'Average Duration';
ToolTip = 'Specifies the average time taken by the tests in the test suite.';
}
field("Tokens Consumed"; Rec."Tokens Consumed")
{
}
field("Average Tokens Consumed"; AvgTokensConsumed)
{
Editable = false;
Caption = 'Average Tokens Consumed';
ToolTip = 'Specifies the average number of tokens consumed by the tests in the last run.';
}
}

}
Expand Down Expand Up @@ -260,6 +269,7 @@ page 149031 "AIT Test Suite"
AITTestSuiteMgt: Codeunit "AIT Test Suite Mgt.";
EnableActions: Boolean;
AvgTimeDuration: Duration;
AvgTokensConsumed: Integer;
TotalDuration: Duration;
PageCaptionLbl: Label 'AI Test';
TestRunnerDisplayName: Text;
Expand All @@ -284,7 +294,7 @@ page 149031 "AIT Test Suite"
TestSuiteMgt: Codeunit "Test Suite Mgt.";
begin
UpdateTotalDuration();
UpdateAverageExecutionTime();
UpdateAverages();
TestRunnerDisplayName := TestSuiteMgt.GetTestRunnerDisplayName(Rec."Test Runner Id");
end;

Expand All @@ -294,12 +304,17 @@ page 149031 "AIT Test Suite"
TotalDuration := Rec."Total Duration (ms)";
end;

local procedure UpdateAverageExecutionTime()
local procedure UpdateAverages()
begin
Rec.CalcFields("No. of Tests Executed", "Total Duration (ms)", "No. of Tests Executed - Base", "Total Duration (ms) - Base");
Rec.CalcFields("No. of Tests Executed", "Total Duration (ms)", "Tokens Consumed");
if Rec."No. of Tests Executed" > 0 then
AvgTimeDuration := Rec."Total Duration (ms)" div Rec."No. of Tests Executed"
else
AvgTimeDuration := 0;

if Rec."No. of Tests Executed" > 0 then
AvgTokensConsumed := Rec."Tokens Consumed" div Rec."No. of Tests Executed"
else
AvgTokensConsumed := 0;
end;
}
16 changes: 16 additions & 0 deletions src/Tools/AI Test Toolkit/src/TestSuite/AITTestSuite.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ table 149030 "AIT Test Suite"
FieldClass = FlowField;
CalcFormula = count("AIT Log Entry" where("Test Suite Code" = field("Code"), "Version" = field("Version")));
}
field(24; "Tokens Consumed"; Integer)
{
Caption = 'Total Tokens Consumed';
ToolTip = 'Specifies the aggregated number of tokens consumed by the test in the current version. This is applicable only when using Microsoft AI Module.';
Editable = false;
FieldClass = FlowField;
CalcFormula = sum("AIT Log Entry"."Tokens Consumed" where("Test Suite Code" = field("Code"), Version = field("Version"), Operation = const('Run Procedure'), "Procedure Name" = filter(<> '')));
}
field(31; "No. of Tests Executed - Base"; Integer)
{
Caption = 'No. of Tests Executed';
Expand Down Expand Up @@ -183,6 +191,14 @@ table 149030 "AIT Test Suite"
FieldClass = FlowField;
CalcFormula = sum("AIT Log Entry"."Duration (ms)" where("Test Suite Code" = field("Code"), Version = field("Base Version"), Operation = const('Run Procedure'), "Procedure Name" = filter(<> '')));
}
field(35; "Tokens Consumed - Base"; Integer)
{
Caption = 'Total Tokens Consumed - Base';
ToolTip = 'Specifies the aggregated number of tokens consumed by the test in the base version. This is applicable only when using Microsoft AI Module.';
Editable = false;
FieldClass = FlowField;
CalcFormula = sum("AIT Log Entry"."Tokens Consumed" where("Test Suite Code" = field("Code"), Version = field("Base Version"), Operation = const('Run Procedure'), "Procedure Name" = filter(<> '')));
}
field(50; "Test Runner Id"; Integer)
{
Caption = 'Test Runner Id';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ codeunit 149034 "AIT Test Suite Mgt."
AITLogEntry.SetOutputBlob(TestOutput);

AITLogEntry."Procedure Name" := CurrentTestMethodLine.Function;
AITLogEntry."Tokens Consumed" := AITTestRunIteration.GetAITokenUsedByLastTestMethodLine();
AITLogEntry.Insert(true);

Commit();
Expand Down

0 comments on commit 8a8b8da

Please sign in to comment.