Skip to content

Commit 0430ce9

Browse files
committed
add upload timings
1 parent 83fa6ca commit 0430ce9

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

zboxcore/sdk/chunked_upload_process_js.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ func (su *ChunkedUpload) processWebWorkerUpload(data *safejs.Value, blobber *Chu
382382
su.updateChunkProgress(chunkEndIndex, pos)
383383
finalRequestObject, _ := data.Get("isFinal")
384384
finalRequest, _ := finalRequestObject.Bool()
385+
totalSizeObject, _ := data.Get("totalSize")
386+
totalSize, _ := totalSizeObject.Int()
387+
timingObject, _ := data.Get("timing")
388+
timing, _ := timingObject.Int()
389+
logEvent := logEntry{
390+
OpType: "upload",
391+
DataSize: totalSize,
392+
TimeTaken: int64(timing),
393+
}
394+
writeLogEntry(blobber.blobber.Baseurl, logEvent)
385395
if finalRequest {
386396
finalResult, err := data.Get("finalResult")
387397
if err != nil {
@@ -442,7 +452,7 @@ func ProcessEventData(data safejs.Value) {
442452
remotePath = fileMeta.RemotePath
443453
}
444454
if err != nil {
445-
selfPostMessage(false, false, err.Error(), remotePath, 0, nil)
455+
selfPostMessage(false, false, err.Error(), remotePath, 0, 0, 0, nil)
446456
return
447457
}
448458
wp, ok := hasherMap[fileMeta.RemotePath]
@@ -464,7 +474,7 @@ func ProcessEventData(data safejs.Value) {
464474
uploadData, err := formBuilder.Build(fileMeta, wp.hasher, formInfo.ConnectionID, blobberID, formInfo.ChunkSize, formInfo.ChunkStartIndex, formInfo.ChunkEndIndex, formInfo.IsFinal, formInfo.EncryptedKey, formInfo.EncryptedKeyPoint,
465475
fileShards, thumbnailChunkData, formInfo.ShardSize)
466476
if err != nil {
467-
selfPostMessage(false, false, err.Error(), remotePath, formInfo.ChunkEndIndex, nil)
477+
selfPostMessage(false, false, err.Error(), remotePath, formInfo.ChunkEndIndex, 0, 0, nil)
468478
return
469479
}
470480
if formInfo.OnlyHash {
@@ -474,27 +484,35 @@ func ProcessEventData(data safejs.Value) {
474484
ValidationRoot: uploadData.formData.ValidationRoot,
475485
ThumbnailContentHash: uploadData.formData.ThumbnailContentHash,
476486
}
477-
selfPostMessage(true, true, "", remotePath, formInfo.ChunkEndIndex, finalResult)
487+
selfPostMessage(true, true, "", remotePath, formInfo.ChunkEndIndex, 0, 0, finalResult)
478488
} else {
479-
selfPostMessage(true, false, "", remotePath, formInfo.ChunkEndIndex, nil)
489+
selfPostMessage(true, false, "", remotePath, formInfo.ChunkEndIndex, 0, 0, nil)
480490
}
481491
return
482492
}
483493
blobberURL := os.Getenv("BLOBBER_URL")
484494
if !formInfo.IsFinal {
485495
wp.wg.Add(1)
486496
}
497+
487498
go func(blobberData blobberData, remotePath string, wg *sync.WaitGroup) {
499+
var (
500+
totalSize int
501+
timing int64
502+
)
503+
for _, dataBuffer := range blobberData.dataBuffers {
504+
totalSize += dataBuffer.Len()
505+
}
488506
if formInfo.IsFinal && len(blobberData.dataBuffers) > 1 {
489-
err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
507+
timing, err = sendUploadRequest(blobberData.dataBuffers[:len(blobberData.dataBuffers)-1], blobberData.contentSlice[:len(blobberData.contentSlice)-1], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
490508
if err != nil {
491-
selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, nil)
509+
selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, totalSize, timing, nil)
492510
return
493511
}
494512
wg.Wait()
495-
err = sendUploadRequest(blobberData.dataBuffers[len(blobberData.dataBuffers)-1:], blobberData.contentSlice[len(blobberData.contentSlice)-1:], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
513+
timing, err = sendUploadRequest(blobberData.dataBuffers[len(blobberData.dataBuffers)-1:], blobberData.contentSlice[len(blobberData.contentSlice)-1:], blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
496514
if err != nil {
497-
selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, nil)
515+
selfPostMessage(false, true, err.Error(), remotePath, formInfo.ChunkEndIndex, totalSize, timing, nil)
498516
return
499517
}
500518
} else {
@@ -503,9 +521,9 @@ func ProcessEventData(data safejs.Value) {
503521
} else {
504522
defer wg.Done()
505523
}
506-
err = sendUploadRequest(blobberData.dataBuffers, blobberData.contentSlice, blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
524+
timing, err = sendUploadRequest(blobberData.dataBuffers, blobberData.contentSlice, blobberURL, formInfo.AllocationID, formInfo.AllocationTx, formInfo.HttpMethod, formInfo.ClientId)
507525
if err != nil {
508-
selfPostMessage(false, formInfo.IsFinal, err.Error(), remotePath, formInfo.ChunkEndIndex, nil)
526+
selfPostMessage(false, formInfo.IsFinal, err.Error(), remotePath, formInfo.ChunkEndIndex, totalSize, timing, nil)
509527
return
510528
}
511529
}
@@ -515,9 +533,9 @@ func ProcessEventData(data safejs.Value) {
515533
ValidationRoot: blobberData.formData.ValidationRoot,
516534
ThumbnailContentHash: blobberData.formData.ThumbnailContentHash,
517535
}
518-
selfPostMessage(true, true, "", remotePath, formInfo.ChunkEndIndex, finalResult)
536+
selfPostMessage(true, true, "", remotePath, formInfo.ChunkEndIndex, totalSize, timing, finalResult)
519537
} else {
520-
selfPostMessage(true, false, "", remotePath, formInfo.ChunkEndIndex, nil)
538+
selfPostMessage(true, false, "", remotePath, formInfo.ChunkEndIndex, totalSize, timing, nil)
521539
}
522540
}(uploadData, remotePath, wp.wg)
523541

@@ -527,13 +545,15 @@ func InitHasherMap() {
527545
hasherMap = make(map[string]workerProcess)
528546
}
529547

530-
func selfPostMessage(success, isFinal bool, errMsg, remotePath string, chunkEndIndex int, finalResult *FinalWorkerResult) {
548+
func selfPostMessage(success, isFinal bool, errMsg, remotePath string, chunkEndIndex, totalSize int, timing int64, finalResult *FinalWorkerResult) {
531549
obj := js.Global().Get("Object").New()
532550
obj.Set("success", success)
533551
obj.Set("error", errMsg)
534552
obj.Set("isFinal", isFinal)
535553
obj.Set("chunkEndIndex", chunkEndIndex)
536554
obj.Set("remotePath", remotePath)
555+
obj.Set("totalSize", totalSize)
556+
obj.Set("timing", timing)
537557
if finalResult != nil {
538558
finalResultJSON, err := json.Marshal(finalResult)
539559
if err != nil {
@@ -619,7 +639,7 @@ func parseEventData(data safejs.Value) (*FileMeta, *ChunkedUploadFormInfo, [][]b
619639
return fileMeta, formInfo, fileShards, thumbnailChunkData, nil
620640
}
621641

622-
func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobberURL, allocationID, allocationTx, httpMethod string, clientId ...string) (err error) {
642+
func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobberURL, allocationID, allocationTx, httpMethod string, clientId ...string) (timing int64, err error) {
623643
eg, _ := errgroup.WithContext(context.TODO())
624644
for dataInd := 0; dataInd < len(dataBuffers); dataInd++ {
625645
ind := dataInd
@@ -639,8 +659,10 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb
639659
err, shouldContinue = func() (err error, shouldContinue bool) {
640660
resp := fasthttp.AcquireResponse()
641661
defer fasthttp.ReleaseResponse(resp)
662+
now := time.Now()
642663
err = zboxutil.FastHttpClient.DoTimeout(req, resp, DefaultUploadTimeOut)
643664
fasthttp.ReleaseRequest(req)
665+
timing = time.Since(now).Milliseconds()
644666
if err != nil {
645667
logger.Logger.Error("Upload : ", err, " baseURL ", blobberURL)
646668
if errors.Is(err, fasthttp.ErrConnectionClosed) || errors.Is(err, syscall.EPIPE) || errors.Is(err, fasthttp.ErrDialTimeout) {
@@ -710,7 +732,7 @@ func sendUploadRequest(dataBuffers []*bytes.Buffer, contentSlice []string, blobb
710732
return err
711733
})
712734
}
713-
return eg.Wait()
735+
return timing, eg.Wait()
714736
}
715737

716738
type eventChanWorker struct {

0 commit comments

Comments
 (0)