272 lines
11 KiB
Go
272 lines
11 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// This wrapper models the previous list-progress read cost. The API's retained
|
|
// latest-selection logic makes its result equivalent while the store returns
|
|
// full history, as it did before this change.
|
|
type sidebarHistoryBaseline struct{ store.Store }
|
|
|
|
func (s sidebarHistoryBaseline) ListLatestSliceProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.SliceRevisionRecord, error) {
|
|
return s.ListSliceRevisionsByTaskIDs(ctx, org, ids)
|
|
}
|
|
func (s sidebarHistoryBaseline) ListLatestAnnotationProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.AnnotationRevisionRecord, error) {
|
|
return s.ListAnnotationRevisionsByTaskIDs(ctx, org, ids)
|
|
}
|
|
|
|
// Fixed loopback Mongo plus a unique disposable database: this opt-in test
|
|
// cannot seed or drop any customer database. It exercises real HTTP and Mongo
|
|
// with 100 distinct actors, not 100 requests sharing an identity.
|
|
func TestAnnotationSidebar100ActorsMongoHTTP(t *testing.T) {
|
|
if os.Getenv("FANYU_SIDEBAR_MONGO_LOAD") != "1" {
|
|
t.Skip("opt-in local Mongo sidebar acceptance")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
|
defer cancel()
|
|
uri := os.Getenv("FANYU_SIDEBAR_MONGO_URI")
|
|
if uri == "" {
|
|
uri = "mongodb://127.0.0.1:27018/?directConnection=true"
|
|
}
|
|
if uri != "mongodb://127.0.0.1:27018/?directConnection=true" && uri != "mongodb://127.0.0.1:27038/?directConnection=true" {
|
|
t.Fatal("isolated loopback Mongo only")
|
|
}
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetServerSelectionTimeout(5*time.Second))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db := client.Database(fmt.Sprintf("sidebar_http_test_%d", time.Now().UnixNano()))
|
|
defer func() {
|
|
cleanup, stop := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer stop()
|
|
if err := db.Drop(cleanup); err != nil {
|
|
t.Error(err)
|
|
}
|
|
client.Disconnect(cleanup)
|
|
}()
|
|
s := store.NewMongoStore(db)
|
|
if err := s.EnsureIndexes(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemInProgress, "annotator-v5", 1)
|
|
project, _ := f.store.GetProject(ctx, f.task.ProjectID)
|
|
supplier, _ := f.store.GetSupplier(ctx, f.annotator.SupplierID)
|
|
if err := s.UpsertProject(ctx, *project); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.UpsertSupplier(ctx, *supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
actors := map[string]domain.UserRecord{}
|
|
documents := map[string][]interface{}{}
|
|
for a := 0; a < 100; a++ {
|
|
actor := f.annotator
|
|
actor.ID = fmt.Sprintf("sidebar-actor-%03d", a)
|
|
actor.Phone = actor.ID
|
|
actors[actor.ID] = actor
|
|
documents["users"] = append(documents["users"], actor)
|
|
documents["projectMembers"] = append(documents["projectMembers"], domain.ProjectMemberRecord{ID: "member-" + actor.ID, OrganizationID: actor.OrganizationID, ProjectID: f.task.ProjectID, UserID: actor.ID, SupplierID: actor.SupplierID, Roles: domain.AccountProductionRoles(actor)})
|
|
for n := 0; n < 8; n++ {
|
|
id := fmt.Sprintf("task-%03d-%02d", a, n)
|
|
task := f.task
|
|
task.ID = id
|
|
task.RunID = "run-" + id
|
|
task.ArtifactID = "artifact-" + id
|
|
task.CreatedAt = fmt.Sprintf("2026-10-17T00:00:%02dZ", n)
|
|
task.CurrentRevision = 10
|
|
item := f.item
|
|
item.ID = "item-" + id
|
|
item.LegacyTaskID = id
|
|
item.AssignedUserID = actor.ID
|
|
item.Subject.ItemKey = id
|
|
item.BatchID = "batch-" + id
|
|
item.CreatedAt = task.CreatedAt
|
|
item.UpdatedAt = "2026-10-17T00:00:00Z"
|
|
batch := f.batch
|
|
batch.ID = item.BatchID
|
|
batch.RunID = task.RunID
|
|
item.RunID = task.RunID
|
|
documents["annotationTasks"] = append(documents["annotationTasks"], task)
|
|
documents["humanItems"] = append(documents["humanItems"], item)
|
|
documents["humanBatches"] = append(documents["humanBatches"], batch)
|
|
for rev := 1; rev <= 10; rev++ {
|
|
slices := []domain.SliceSegment{{Start: 0, End: 5, AnnotationStatus: domain.SliceAnnotationLabeled}, {Start: 5, End: 10, AnnotationStatus: domain.SliceAnnotationEmptyConfirmed}, {Start: 10, End: 15, DeliveryDisposition: "discard"}, {Start: 15, End: 20}}
|
|
annotations := []domain.AnnotationSegment{{ID: "event", Start: 0, End: 5, Description: strings.Repeat("history-description-", 100)}}
|
|
documents["sliceRevisions"] = append(documents["sliceRevisions"], domain.SliceRevisionRecord{ID: fmt.Sprintf("slice-%s-%d", id, rev), OrganizationID: actor.OrganizationID, TaskID: id, Revision: rev, CreatedAt: "2026-10-17T01:00:00Z", Segments: slices, ChangeReason: strings.Repeat("operation-history-", 100)})
|
|
documents["annotationRevisions"] = append(documents["annotationRevisions"], domain.AnnotationRevisionRecord{ID: fmt.Sprintf("annotation-%s-%d", id, rev), OrganizationID: actor.OrganizationID, TaskID: id, Revision: rev, AuthorUserID: actor.ID, Segments: annotations, ObjectHints: []string{strings.Repeat("large hint ", 100)}})
|
|
}
|
|
}
|
|
}
|
|
for collection, items := range documents {
|
|
if _, err := db.Collection(collection).InsertMany(ctx, items); err != nil {
|
|
t.Fatalf("seed %s: %v", collection, err)
|
|
}
|
|
}
|
|
// Organization-wide normal backlog matches the production scan cardinality.
|
|
// Inert payload also prevents tiny batch documents hiding FETCH work.
|
|
for start := 0; start < 25000; start += 500 {
|
|
backlog := make([]interface{}, 0, 500)
|
|
for i := start; i < start+500; i++ {
|
|
backlog = append(backlog, bson.M{
|
|
"id": fmt.Sprintf("backlog-batch-%05d", i), "organizationId": f.annotator.OrganizationID,
|
|
"runId": fmt.Sprintf("backlog-run-%05d", i), "gateNodeId": "gate",
|
|
"stages": domain.DefaultFanyuV58HumanStages(), "syntheticPayload": strings.Repeat("synthetic-batch-payload-", 360),
|
|
})
|
|
}
|
|
if _, err := db.Collection("humanBatches").InsertMany(ctx, backlog); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
handlers := []http.Handler{NewHandler(sidebarHistoryBaseline{s}, nil), NewHandler(s, nil)}
|
|
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
actor, ok := actors[r.Header.Get("X-Synthetic-Actor")]
|
|
if !ok {
|
|
http.Error(w, "fixture actor required", 403)
|
|
return
|
|
}
|
|
r = r.WithContext(context.WithValue(r.Context(), authContextKey{}, &authenticatedPrincipal{User: actor, Organization: domain.OrganizationRecord{ID: actor.OrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
index := 1
|
|
if r.Header.Get("X-Baseline") == "1" {
|
|
index = 0
|
|
}
|
|
handlers[index].ServeHTTP(w, r)
|
|
}))
|
|
defer httpServer.Close()
|
|
type page struct {
|
|
Items []domain.HumanItemRecord `json:"items"`
|
|
Tasks []annotationTaskListRecord `json:"tasks"`
|
|
}
|
|
call := func(actor, path string, baseline bool, target any) error {
|
|
req, e := http.NewRequestWithContext(ctx, http.MethodGet, httpServer.URL+path, nil)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
req.Header.Set("X-Synthetic-Actor", actor)
|
|
if baseline {
|
|
req.Header.Set("X-Baseline", "1")
|
|
}
|
|
response, e := httpServer.Client().Do(req)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != 200 {
|
|
io.Copy(io.Discard, response.Body)
|
|
return fmt.Errorf("HTTP %d", response.StatusCode)
|
|
}
|
|
return json.NewDecoder(response.Body).Decode(target)
|
|
}
|
|
journey := func(actor string, baseline bool) error {
|
|
var result page
|
|
prefix := "?projectId=" + f.task.ProjectID
|
|
if !baseline {
|
|
if e := call(actor, "/api/plugins/annotation-qa/sidebar"+prefix+"&annotationView=active", false, &result); e != nil {
|
|
return e
|
|
}
|
|
} else {
|
|
if e := call(actor, "/api/human-items"+prefix+"&pluginId=annotation-qa&pool=mine&annotationView=active", true, &result); e != nil {
|
|
return e
|
|
}
|
|
ids := []string{}
|
|
for _, item := range result.Items {
|
|
ids = append(ids, item.LegacyTaskID)
|
|
}
|
|
var tasks struct {
|
|
Items []annotationTaskListRecord `json:"items"`
|
|
}
|
|
if e := call(actor, "/api/plugins/annotation-qa/tasks"+prefix+"&ids="+strings.Join(ids, ","), true, &tasks); e != nil {
|
|
return e
|
|
}
|
|
result.Tasks = tasks.Items
|
|
}
|
|
if len(result.Items) != 8 || len(result.Tasks) != 8 {
|
|
return fmt.Errorf("actor %s items=%d tasks=%d", actor, len(result.Items), len(result.Tasks))
|
|
}
|
|
for _, item := range result.Items {
|
|
if item.AssignedUserID != actor {
|
|
return fmt.Errorf("cross-actor item")
|
|
}
|
|
}
|
|
for _, task := range result.Tasks {
|
|
if task.SliceCount != 4 || task.CompletedSliceCount != 3 || task.PendingSliceCount != 1 || task.AnnotationEventCount != 1 {
|
|
return fmt.Errorf("progress changed: %+v", task)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
type measure struct {
|
|
Count int `json:"count"`
|
|
P95 float64 `json:"p95Ms"`
|
|
Max float64 `json:"maxMs"`
|
|
}
|
|
measureRound := func(baseline bool, concurrency int) measure {
|
|
values := []float64{}
|
|
var mu sync.Mutex
|
|
var wg sync.WaitGroup
|
|
var failures []string
|
|
for n := 0; n < concurrency; n++ {
|
|
wg.Add(1)
|
|
go func(n int) {
|
|
defer wg.Done()
|
|
rounds := 2
|
|
if concurrency == 1 {
|
|
rounds = 20
|
|
}
|
|
for round := 0; round < rounds; round++ {
|
|
started := time.Now()
|
|
err := journey(fmt.Sprintf("sidebar-actor-%03d", n), baseline)
|
|
elapsed := float64(time.Since(started).Microseconds()) / 1000
|
|
mu.Lock()
|
|
values = append(values, elapsed)
|
|
if err != nil {
|
|
failures = append(failures, err.Error())
|
|
}
|
|
mu.Unlock()
|
|
}
|
|
}(n)
|
|
}
|
|
wg.Wait()
|
|
if len(failures) > 0 {
|
|
t.Fatalf("%d failures: %s", len(failures), failures[0])
|
|
}
|
|
sort.Float64s(values)
|
|
return measure{len(values), values[(len(values)*95-1)/100], values[len(values)-1]}
|
|
}
|
|
// Warm both paths before measuring; local results exclude WAN/browser/media.
|
|
if err := journey("sidebar-actor-000", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := journey("sidebar-actor-000", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := map[string]any{"scope": "isolated local Mongo + HTTP; 100 distinct actors; eight tasks and ten revisions each; 25k normal batches with 8640-byte payload; history/two-endpoint baseline uses current query and progress concurrency; excludes production WAN/media", "baselineSingle": measureRound(true, 1), "optimizedSingle": measureRound(false, 1), "baseline100": measureRound(true, 100), "optimized100": measureRound(false, 100)}
|
|
body, _ := json.MarshalIndent(result, "", " ")
|
|
t.Log(string(body))
|
|
if output := os.Getenv("FANYU_SIDEBAR_LOAD_REPORT"); output != "" {
|
|
if err := os.WriteFile(output, append(body, '\n'), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if result["optimizedSingle"].(measure).P95 > 500 || result["optimized100"].(measure).P95 > 1500 {
|
|
t.Fatal("local sidebar latency budget exceeded")
|
|
}
|
|
}
|