
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ dd	lmZ d
 ZddZ G d de	j                         Zy)z%Command for spanner samples workload.    )absolute_import)division)unicode_literalsN)arg_parsers)base)samples)execution_utilsc                     | t         j                  vrt        dj                  |             t        j
                  j                  t        j                  |       t         j                  |    j                        S )NzUnknown sample app '{}')	r   APPS
ValueErrorformatospathjoinget_local_bin_pathworkload_bin)appnames    'lib/surface/spanner/samples/workload.py_get_popen_jarr      s[    GLL 
.55g>
??	  )7<<+@+M+M
O O    c                    ddt        |       g}| |j                  dj                  |             |r=t        j                  j                  t        j                  dj                  |             nd}t        j                  ||      S )aU  Run the workload generator executable for the given sample app.

  Args:
    appname: str, Name of the sample app.
    port: int, Port to run the service on.
    capture_logs: bool, Whether to save logs to disk or print to stdout.

  Returns:
    subprocess.Popen or execution_utils.SubprocessTimeoutWrapper, The running
      subprocess.
  javaz-jarNz	--port={}z{}-workload.log)	r   appendr   r   r   r   r   SAMPLES_LOG_PATHrun_proc)r   portcapture_logs	proc_argscapture_logs_fns        r   run_workloadr    %   s|     v~g67)	[''-. 
 	ggll7++->-E-Eg-NO  
		)_	55r   c                   N    e Zd ZdZd ej
                  d      iZed        Zd Z	y)Workloada  Generate gRPC traffic for a given sample app's backend service.

  Before sending traffic to the backend service, create the database and
  start the service with:

      $ {parent_command} init APPNAME --instance-id=INSTANCE_ID
      $ {parent_command} backend APPNAME --instance-id=INSTANCE_ID

  To run all three steps together, use:

      $ {parent_command} run APPNAME --instance-id=INSTANCE_ID
  EXAMPLESzh          To generate traffic for the 'finance' sample app, run:

          $ {command} finance
        c                     | j                  dd       | j                  ddt        j                         d       | j                  dt        d	
       | j                  dt        d
       y)zArgs is called by calliope to gather arguments for this command.

    Args:
      parser: An argparse parser that you can use to add arguments that go on
        the command line after this command. Positional arguments are allowed.
    r   z$The sample app name, e.g. "finance".)helpz
--duration1hz=Duration of time allowed to run before stopping the workload.)defaulttyper%   z--portz$Port of the running backend service.)r(   r%   z--target-qpszTarget requests per second.N)add_argumentr   Durationint)parsers    r   ArgszWorkload.ArgsQ   s{     	(NO
!!#M	  P
 s"H  K
S(E  Hr   c                    t        |j                  |j                        }	 t        j                         5  |j                  |j                        cddd       S # 1 sw Y   yxY w# t        $ r |j                          Y yt        j                  $ r/ |j                          dj                  |j                        cY S w xY w)zThis is what gets called when the user runs this command.

    Args:
      args: an argparse namespace. All the arguments that were provided to this
        command invocation.

    Returns:
      Some value that we want to have printed later.
    NzWorkload generator killedz+Workload generator killed after {duration}s)duration)r    r   r   r	   RaisesKeyboardInterruptwaitr/   KeyboardInterrupt	terminateTIMEOUT_EXPIRED_ERRr   )selfargsprocs      r   RunzWorkload.Rune   s     dii0D	"224yy' 544   )
nn(.. "
nn:AA== B " ""s4   A' A	A' A$ A' $A' 'C>CCN)
__name__
__module____qualname____doc__textwrapdedentdetailed_helpstaticmethodr-   r8    r   r   r"   r"   :   sA     
(//  - H H&r   r"   )NF)r<   
__future__r   r   r   r   r=   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.spannerr   googlecloudsdk.corer	   r   r    Commandr"   rA   r   r   <module>rG      sB    , &  ' 	  / ( 6 /O6*@t|| @r   