
    #                        S r SSKJr  SSKrSSKrSSKJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SSKJr  S	rS
rSrSrSrSrSrSrS
rSrSrSrSS jrSS jr " S S\R<                  5      r " S S\R@                  5      r!g)Upload Throughput Diagnostic.    )annotationsN)List)storage_url)
diagnostic)log)scaled_integer   i   2CLOUDSDK_STORAGE_PARALLEL_COMPOSITE_UPLOAD_ENABLED4CLOUDSDK_STORAGE_PARALLEL_COMPOSITE_UPLOAD_THRESHOLD9CLOUDSDK_STORAGE_PARALLEL_COMPOSITE_UPLOAD_COMPONENT_SIZE50Mupload_throughputzUpload Throughput Diagnostic-ziProcess and/or thread count is set but streaming uploads dont support parallelism. Ignoring these values.zupload throughputc                :    SU  S[         R                  " USS9 S3$ )zDReturns the payload description for the given object count and size.zTransferred z& objects for a total transfer size of    decimal_places.r	   FormatBinaryNumber)object_countobject_sizes     Olib/googlecloudsdk/command_lib/storage/diagnose/upload_throughput_diagnostic.py_get_payload_descriptionr   5   s0     	\N #


+
+K
J	K1N    c                6    [         R                  " U SS9nU S3$ )z9Formats the upload throughput to a human readable format.r   r   z/secr   )r   scaled_upload_throughputs     r    _get_formatted_upload_throughputr   =   s(    +>> %%T	**r   c                  $    \ rS rSrSrSrSrSrSrg)
UploadTypeE   z;Enum class for specifying upload type for diagnostic tests.PARALLEL_COMPOSITE	STREAMINGFILE N)	__name__
__module____qualname____firstlineno____doc__r#   r$   r%   __static_attributes__r&   r   r   r!   r!   E   s    C+)	$r   r!   c                     ^  \ rS rSrSr   S         SS jjr\SS j5       rU 4S jrS r	S r
S rU 4S	 jr\SS
 j5       rSrU =r$ )UploadThroughputDiagnosticM   r   c                x   Xl         XPl        X@l        X l        / U l        0 U l        SU l        0 U l        U(       a  X0l        O8U R                  [        R                  :w  a  [        /[        -  O[        /U l        [        U R                  5      U l        S[!        ["        R$                  " 5       5      -   U l        g)a@  Initializes the Upload Throughput Diagnostic.

Args:
  test_bucket_url: The bucket to upload to.
  upload_type: The type of upload to perform.
  object_sizes: The sizes of the objects to upload.
  thread_count: The number of threads to use for the upload.
  process_count: The number of processes to use for the upload.
Nupload_throughput_diagnostics_)
bucket_url_process_count_thread_count_upload_type_files_old_env_varstemp_dir_result_object_sizesr!   r$   _DEFAULT_OBJECT_SIZE_DEFAULT_OBJECT_COUNT_DEFAULT_STREAMING_SIZElen_object_countstruuiduuid4object_prefix)selftest_bucket_urlupload_typeobject_sizesthread_countprocess_counts         r   __init__#UploadThroughputDiagnostic.__init__P   s    " &O'%#DKDDMDL' *"6"66  
 #8
8'( 
 T//0D :C

<MMDr   c                    [         $ )N)_DIAGNOSTIC_NAMErD   s    r   nameUploadThroughputDiagnostic.namev   s    r   c                  > [         [        U ]  5         U R                  [        R
                  :X  aW  [        U l        [        U R                  5      S:  a  [        R                  " S[         35        gU R                  S   U l        gU R                  U R                  U R                  5      (       d  [        R                  " S5      eg)z1Prepares the environment for the diagnostic test.r   zgStreaming uploads do not support multiple objects. Ignoring the object count and using default size. : r   NzFailed to create test files.)superr.   _pre_processr5   r!   r$   r=   streaming_sizer>   r:   r   warning_create_test_filesrC   r   DiagnosticIgnorableError)rD   	__class__s    r   rS   'UploadThroughputDiagnostic._pre_processz   s    	
$d8:J0003d	T	 1	$'(*	
  #003""4#5#5t7I7IJJ//0NOO Kr   c                @   U R                  5         U R                  [        S5        U R                  [        S5        [	        U R
                  5      nU[        R                  " [        5      ::  a/  [        R                  " US-  SS9nU R                  [        U5        gg)z>Sets the environment variables for parallel composite uploads.truer      r   N)_set_parallelism_env_vars_set_env_variable"_ENABLE_PARALLEL_COMPOSITE_ENV_VAR,_PARALLEL_COMPOSITE_UPLOAD_THRESHOLD_ENV_VARminr:   r	   ParseBinaryInteger1_DEFAULT_PARALLEL_COMPOSITE_UPLOAD_COMPONENT_SIZEr   1_PARALLEL_COMPOSITE_UPLOAD_COMPONENT_SIZE_ENV_VAR)rD   min_object_sizeoptimal_component_sizes      r    _set_parallel_composite_env_vars;UploadThroughputDiagnostic._set_parallel_composite_env_vars   s    ""$=vF4	 $,,-O.;;9   .@@
A
a  
;
 r   c                h   U R                   [        R                  :X  a6  U R                  c  U R                  b  [
        R                  " [        5        ggU R                   [        R                  :X  a  U R                  5         gU R                   [        R                  :X  a  U R                  5         gg)zOSets the environment variables for the diagnostic depending on the upload type.N)r5   r!   r$   r3   r4   r   rU   %_STREAMING_UPLOAD_PARALLELISM_WARNINGr#   rg   r%   r]   rN   s    r   _set_cloud_sdk_env_vars2UploadThroughputDiagnostic._set_cloud_sdk_env_vars   s    J000				(D,>,>,J9: -K			j;;	;
++-			joo	-
$$& 
.r   c                   U R                  5         U R                  [        R                  :X  a  [        R
                  " [        U R                  5         [        R                  R                  SR                  U R                  U R                  5      5        U R                  [        U R                  R                   U R"                  -   U R%                  U R                  5      S9  SSS5        gU R                  [        R&                  :X  d  U R                  [        R(                  :X  a  [        R                  R                  SU R*                   SU R                   SU R                  R,                   35        [        R
                  " [        U R                  5         U R                  U R.                  R0                  S-   U R"                  -   S-   U R                  R                   5        SSS5        g[        R2                  " S	R                  U R4                  U R                  5      5      e! , (       d  f       g= f! , (       d  f       g= f)
zRuns the diagnostic test.

This involves running the gcloud command to upload the files and measuring
the time it takes to upload the files.
z-Starting streaming upload of {} bytes to : {})in_strNzStarting upload of z objects to : z with upload type: /*z{} : Unknown upload type: {})rk   r5   r!   r$   r   time_recorder_UPLOAD_THROUGHPUT_RESULT_KEYr9   r   statusPrintformatrT   r2   _run_cp_STREAMING_UPLOAD_SOURCE
url_stringrC   _generate_random_stringr#   r%   r?   valuer8   pathrW   rO   rN   s    r   _runUploadThroughputDiagnostic._run   s    	  "J000##
' 	

;BB##T__	

 	$OO&&););;//0C0CD 	 	
  	Z:::
/	jj 2 23 4oo1$2C2C2I2I1JL ##
' 	MM$t'9'99C?OO&&	
  //
(
/
/		4;L;L
M ; * s   BH2!AI2
I 
Ic                  > [         [        U ]  5         U R                  (       a:   U R                  R	                  5         [
        R                  R                  S5        U R                  U R                  R                  U R                  5        g! [         a/  n[
        R                  " U R                   SU 35         SnANeSnAff = f)z<Restores the environment variables and cleans up temp files.zCleaned up temp files.z" : Failed to clean up temp files. N)rR   r.   _post_processr8   Closer   rs   rt   OSErrorrU   rO   _clean_up_objectsr2   rx   rC   )rD   erX   s     r   r   (UploadThroughputDiagnostic._post_process   s    	
$d9;}}I

12 	4??55t7I7IJ  Ityyk!CA3GHHIs   9B 
C	%CC	c                   U R                   (       a  [        U R                   ;  a  gU R                   [           n[        U R                  5      n[        R
                  " US5      (       a  [        R                  nO[        [        X!-  S5      5      n[        R                  " [        U[        U R                  U5      S9n[        R                  " U R                  U/S9$ )z:Returns the summarized result of the diagnostic execution.Ng        r\   )rO   resultpayload_description)rO   operation_results)r9   rr   sumr:   mathiscloser   PLACEHOLDER_METRIC_VALUEr   roundDiagnosticOperationResult_METRIC_NAMEr   r?   DiagnosticResultrO   )rD   upload_timeupload_payload_sizer   operation_results        r   r   !UploadThroughputDiagnostic.result   s     <<8L,,<=Kd001||K%%$==:
#11
5 ";; 4 3
 &&YY+, r   )r6   r?   r:   r7   r3   r9   r4   r5   r2   rC   rT   r8   )NNN)
rE   zstorage_url.CloudUrlrF   r!   rG   z	List[int]rH   intrI   r   )returnr@   )r   z"diagnostic.DiagnosticResult | None)r'   r(   r)   r*   r+   rJ   propertyrO   rS   rg   rk   r|   r   r   r,   __classcell__)rX   s   @r   r.   r.   M   s    % !%$N+$N $N 	$N
 $N $NL  P&('(T	K  r   r.   )r   r   r   r   r   r@   )r   floatr   r@   )"r+   
__future__r   enumr   typingr   rA   "googlecloudsdk.command_lib.storager   +googlecloudsdk.command_lib.storage.diagnoser   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r<   r;   r_   r`   rd   rc   rr   rM   r=   rw   rj   r   r   r   Enumr!   
Diagnosticr.   r&   r   r   <module>r      s    $ "     : B # 3  " 8 # ; - @ 2 5: 1 3 1 %  3 & #+ l!6!6 lr   