54 lines
4.0 KiB
C#
54 lines
4.0 KiB
C#
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Text.Json;
|
|
using Dapper;
|
|
using PlotLine.Services;
|
|
|
|
internal static class SqlLaunchPollingTests
|
|
{
|
|
public static async Task Run()
|
|
{
|
|
using var db=await SqlLaunchDatabaseTests.Open();
|
|
if(await db.ExecuteScalarAsync<int>("SELECT (SELECT COUNT(*) FROM dbo.StoryIntelligenceRuns WHERE Status IN(N'Pending',N'Running'))+(SELECT COUNT(*) FROM dbo.WholeBookPlotSynthesisRuns WHERE Status IN(N'Pending',N'Running'))+(SELECT COUNT(*) FROM dbo.CharacterEnrichmentRuns WHERE Status IN(N'Pending',N'Running'))")!=0)throw new Exception("Idle clone required; no existing jobs will be claimed");
|
|
var user=await db.ExecuteScalarAsync<int>("SELECT TOP(1) UserID FROM dbo.ProjectUserAccess WHERE AccessRole=N'Owner' AND IsActive=1");
|
|
var results=new List<object>();
|
|
foreach(var optimized in new[]{false,true})
|
|
{
|
|
int marker=0;var calls=0;var steadyCalls=0;double sqlMs=0;double queuedAt=0;var timer=Stopwatch.StartNew();var idle=new QueueIdleBackoff(TimeSpan.FromSeconds(3),TimeSpan.FromSeconds(6));
|
|
try
|
|
{
|
|
var enqueue=Task.Run(async()=>{
|
|
await Task.Delay(TimeSpan.FromSeconds(60.2));
|
|
using var queue=await SqlLaunchDatabaseTests.Open();
|
|
var id=await queue.QuerySingleAsync<int>("dbo.StoryIntelligenceRun_QueueAdminText",new{UserID=user,SourceType="AdminText",SourceLabel="SQL launch synthetic polling marker",SourceText="Synthetic readiness marker; never sent to an AI provider.",Model="fixture-no-api",PromptVersionsSummary="sql-launch"},commandType:CommandType.StoredProcedure);
|
|
queuedAt=timer.Elapsed.TotalMilliseconds;Volatile.Write(ref marker,id);
|
|
});
|
|
while(timer.Elapsed<TimeSpan.FromSeconds(75))
|
|
{
|
|
var started=timer.Elapsed.TotalSeconds;var sqlTimer=Stopwatch.StartNew();
|
|
var claimed=await db.QueryFirstOrDefaultAsync("dbo.StoryIntelligenceRun_ClaimNextPendingFair",new{MaxConcurrentRunsPerBook=1,MaxConcurrentRunsPerUser=1,ClaimLeaseMinutes=15},commandType:CommandType.StoredProcedure);calls++;
|
|
if(claimed is not null)
|
|
{
|
|
await enqueue;
|
|
if((int)claimed.StoryIntelligenceRunID!=marker)throw new Exception("Unexpected clone queue work");
|
|
results.Add(new{optimized,elapsedMs=timer.Elapsed.TotalMilliseconds,commands=calls,steadyCommands=steadyCalls,steadyWindowSeconds=30,sqlMs,jobStartLatencyMs=timer.Elapsed.TotalMilliseconds-queuedAt});
|
|
Console.WriteLine("PASS actual clone idle polling and synthetic claim: "+(optimized?"bounded backoff":"original cadence"));break;
|
|
}
|
|
await db.QueryAsync("dbo.StoryIntelligenceBookPipeline_FindNextReadySceneAnalysis",commandType:CommandType.StoredProcedure);calls++;
|
|
await db.QueryAsync("dbo.WholeBookPlotSynthesisRun_ClaimNext",new{LeaseMinutes=15},commandType:CommandType.StoredProcedure);calls++;
|
|
await db.QueryAsync("dbo.CharacterEnrichmentRun_ClaimNext",new{LeaseMinutes=15},commandType:CommandType.StoredProcedure);calls++;
|
|
sqlTimer.Stop();sqlMs+=sqlTimer.Elapsed.TotalMilliseconds;if(started>=30&&started<60)steadyCalls+=4;
|
|
await Task.Delay(optimized?idle.Next(false):TimeSpan.FromSeconds(3));
|
|
}
|
|
await enqueue;
|
|
if(results.Count!=(optimized?2:1))throw new Exception("Synthetic claim did not arrive within bounded cadence");
|
|
}
|
|
finally
|
|
{
|
|
if(marker>0)await db.ExecuteAsync("DELETE dbo.StoryIntelligenceRuns WHERE StoryIntelligenceRunID=@marker AND SourceLabel=N'SQL launch synthetic polling marker'",new{marker});
|
|
}
|
|
}
|
|
File.WriteAllText("/home/nick/sql-launch-20261119/polling.json",JsonSerializer.Serialize(results,new JsonSerializerOptions{WriteIndented=true}));
|
|
}
|
|
}
|