
    "                       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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S jrSS jr " S S\R<                  5      r " S S\R@                  5      r!g)zDownload Throughput Diagnostic.    )annotationsN)List)optimize_parameters_util)storage_url)
diagnostic)log)files)scaled_integer   i   6CLOUDSDK_STORAGE_SLICED_OBJECT_DOWNLOAD_COMPONENT_SIZE1CLOUDSDK_STORAGE_SLICED_OBJECT_DOWNLOAD_THRESHOLDdownload_throughputzDownload Throughput Diagnostic-zkProcess and/or thread count is set but streaming downloads dont support parallelism. Ignoring these values.zdownload 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     Qlib/googlecloudsdk/command_lib/storage/diagnose/download_throughput_diagnostic.py_get_payload_descriptionr   4   s0     	\N #


+
+K
J	K1N    c                6    [         R                  " U SS9nU S3$ )z;Formats the download throughput to a human readable format.r   r   z/secr   )r   scaled_download_throughputs     r   "_get_formatted_download_throughputr   <   s(    -@@!  ''t	,,r   c                  $    \ rS rSrSrSrSrSrSrg)DownloadTypeD   z=Enum class for specifying download type for diagnostic tests.	STREAMINGSLICEDFILE N)	__name__
__module____qualname____firstlineno____doc__r"   r#   r$   __static_attributes__r%   r   r   r    r    D   s    E)&	$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$ )DownloadThroughputDiagnosticL   z*Diagnostic to measure download throughput.c                2   Xl         XPl        X@l        U(       a  UO[        /[        -  U l        [        U R
                  5      U l        X l        / U l	        0 U l
        SU l        SU l        0 U l        S[        [        R                   " 5       5      -   U l        g)aj  Initializes the download throughput diagnostic.

Args:
  test_bucket_url: Bucket url to use for the diagnostic.
  download_type: Type of download to use for the diagnostic.
  object_sizes: List of object sizes to use for the diagnostic.
  thread_count: Number of threads to use for the diagnostic.
  process_count: Number of processes to use for the diagnostic.
N download_throughput_diagnostics_)
bucket_url_process_count_thread_count_DEFAULT_OBJECT_SIZE_DEFAULT_OBJECT_COUNT_object_sizeslen_object_count_download_type_files_old_env_varstemp_dir_download_dir_resultstruuiduuid4object_prefix)selftest_bucket_urldownload_typeobject_sizesthread_countprocess_counts         r   __init__%DownloadThroughputDiagnostic.__init__O   s    " &O'%  	"#&;; 	
 T//0D'DKDDMDDL <c$**,>OODr   c                    [         $ )N)_DIAGNOSTIC_NAMErC   s    r   name!DownloadThroughputDiagnostic.names   s    r   c                J  > [         [        U ]  5         U R                  U R                  U R
                  5      nU(       d  [        R                  " S5      eU R                  U R                  R                  S-   U R
                  -   S-   U R                  R                  5        g)z!Uploads test files to the bucket.zFailed to create test files./*N)superr-   _pre_process_create_test_filesr6   rB   r   DiagnosticIgnorableError_run_cpr<   pathr1   
url_string)rC   is_done	__class__s     r   rT   )DownloadThroughputDiagnostic._pre_processw   s    	
&:<%%d&8&8$:L:LMG//0NOOLLS 4#5#55;""r   c                (   U R                  5         U R                  [        S5        [        U R                  5      nU[
        R                  " [        R                  5      ::  a/  [
        R                  " US-  SS9nU R                  [        U5        gg)z4Sets the environment variables for sliced downloads.r      r   r   N)_set_parallelism_env_vars_set_env_variable)_SLICED_OBJECT_DOWNLOAD_THRESHOLD_ENV_VARminr6   r
   ParseBinaryIntegerr   COMPONENT_SIZEr   ._SLICED_OBJECT_DOWNLOAD_COMPONENT_SIZE_ENV_VAR)rC   min_object_sizeoptimal_component_sizes      r   _set_sliced_download_env_vars:DownloadThroughputDiagnostic._set_sliced_download_env_vars   s    ""$1	 $,,-O.;; //   .@@
A
a  
8
 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)zQSets the environment variables for the diagnostic depending on the download type.N)r9   r    r"   r2   r3   r   warning'_STREAMING_DOWNLOAD_PARALLELISM_WARNINGr#   rh   r$   r_   rM   s    r   _set_cloud_sdk_env_vars4DownloadThroughputDiagnostic._set_cloud_sdk_env_vars   s    l444				(D,>,>,J;< -K			 3 3	3
((*			 1 1	1
$$& 
2r   c                   U R                  5         U R                  [        R                  :X  a  [        R
                  R                  SU R                   S[         SU R                  R                   35        [        R                  " [        U R                  5         U R                  U R                  R                   U R"                  -   S-   [        5        SSS5        gU R                  [        R$                  :X  d  U R                  [        R&                  :X  a  [(        R*                  " 5       U l        [        R
                  R                  SU R                   SU R,                  R.                   SU R                  R                   35        [        R                  " [        U R                  5         U R                  U R                  R                   U R"                  -   S-   U R,                  R.                  5        SSS5        g[        R0                  " SU R                   35      e! , (       d  f       g= f! , (       d  f       g= f)zRuns the diagnostic.zStarting download of z objects to path : z with download type:  rR   Nz with download type zUnknown download type: )rm   r9   r    r"   r   statusPrintr8   _STREAMING_DOWNLOAD_DESTINATIONvaluer   time_recorder_DOWNLOAD_THROUGHPUT_RESULT_KEYr>   rW   r1   rY   rB   r#   r$   
file_utilsTemporaryDirectoryr=   rX   rV   rM   s    r   _run!DownloadThroughputDiagnostic._run   s     "l444	jj!$"4"4!5 6-. /!!''(*
 ##
)4<< 	OO&&););;cA+	
  	|222,"3"33%88:d	jj!$"4"4!5 6  %%& '!!''(*
 ##
)4<< 	OO&&););;cA##	
  //#D$7$7#8
9 3 " s   ;H.7AH?.
H<?
Ic                  > [         [        U ]  5         U R                  (       a:   U R                  R	                  5         [
        R                  R                  S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NS nAff = f! [         a/  n[
        R                  " U R                   SU 35         S nANS nAff = f)NzCleaned up temp files.z" : Failed to clean up temp files. zCleaned up downloaded files.z- : Failed to clean up temp downloaded files. )rS   r-   _post_processr<   Closer   rp   rq   OSErrorrk   rN   r=   _clean_up_objectsr1   rY   rB   )rC   er[   s     r   r{   *DownloadThroughputDiagnostic._post_process   s    	
&;=}}I

12 
  "

78
 	4??55t7I7IJ  Ityyk!CA3GHHI  
yykFqcJ	
 	

s/   9C 19D 
D%%DD
E!%EEc                   U R                   (       d  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$ )z0Returns the summarized result of the diagnostic.Ng        r^   )rN   resultpayload_description)rN   operation_results)r>   ru   sumr6   mathiscloser   PLACEHOLDER_METRIC_VALUEr   roundDiagnosticOperationResult_METRIC_NAMEr   r8   DiagnosticResultrN   )rC   download_timedownload_payload_sizer   operation_results        r   r   #DownloadThroughputDiagnostic.result   s     <<LL!@AM 2 23||M3''&??>
%5q
9 ";;"4 5
 &&YY+, r   )r=   r9   r:   r8   r6   r;   r2   r>   r3   r1   rB   r<   )NNN)
rD   zstorage_url.CloudUrlrE   r    rF   z	List[int]rG   intrH   r   )returnr?   )r   z"diagnostic.DiagnosticResult | None)r&   r'   r(   r)   r*   rI   propertyrN   rT   rh   rm   rx   r{   r   r+   __classcell__)r[   s   @r   r-   r-   L   s    2 !%"P+"P ""P 	"P
 "P "PH  ,'%NK$  r   r-   )r   r   r   r   r   r?   )r   floatr   r?   )"r*   
__future__r   enumr   typingr   r@   "googlecloudsdk.command_lib.storager   r   +googlecloudsdk.command_lib.storage.diagnoser   googlecloudsdk.corer   googlecloudsdk.core.utilr	   rv   r
   r5   r4   re   ra   ru   rL   rr   rl   r   r   r   Enumr    
Diagnosticr-   r%   r   r   <module>r      s     & #     G : B # 8 3  " < / 8 * #8 3 "% 3 ( %-499 k:#8#8 kr   