小能豆

How to use the pandas df

py

I create a df like this:

df_ts_ids = pd.read_csv('C:\\share\\pythonProject\\TS_ID.csv').head(10)

And I have this function:

def set_ts_ids():
    ts_ids = df_ts_ids.sample()["TS_ID"].iloc[0]
    print("ts_ids : ", ts_ids)
    return ts_ids

It prints like this:

ts_ids :  53005
Response status code:  200
ts_ids :  246388
Response status code:  200
ts_ids :  161700
Response status code:  200
ts_ids :  254692
Response status code:  200
ts_ids :  312003
Response status code:  200
ts_ids :  243898
Response status code:  200
ts_ids :  45032
Response status code:  200
ts_ids :  45032
Response status code:  200

The .csv file looks like this:

TS_ID
53005
246388
312003
243898
161700

I have a Locust file looking like this using the test data:

class LoadValues(FastHttpUser):
    host = server_name

    def _run_read_ts(self, series, resolution, start, end):
        ts_ids = series
        resp = self.client.get(f'/api/loadValues?tsIds={ts_ids}&resolution={resolution}'
                               f'&startUtc={set_from_date()}&endUtc={set_to_date()}',
                               headers={'X-API-KEY': 'xxx'}, name='/api/loadvalues')

        print("Response status code: ", resp.status_code)
        # print("Response text: ", resp.text)

    @task(1)
    def test_get_ts_1(self):
        self._run_read_ts(set_ts_ids(), 'PT15M', set_from_date(), set_to_date())

However I want to call the endpoint with (in this example) 10 TSids (.head(10))

How can I change this line to make the .head value be what says how many TDids each call for each VU puts into the URL?

I guess it needs to be changed here:

ts_ids = df_ts_ids.sample()["TS_ID"].iloc[0]

阅读 76

收藏
2023-11-24

共1个答案

小能豆

To modify your set_ts_ids function to use multiple TS_IDs instead of just one, you can change the line that samples a single TS_ID to sample multiple TS_IDs. Additionally, you might want to adjust the way you generate the URL parameters.

Here’s an updated version of your code:

def set_ts_ids(count=1):
    sampled_ids = df_ts_ids.sample(count)["TS_ID"]
    ts_ids = ",".join(map(str, sampled_ids))
    print("ts_ids : ", ts_ids)
    return ts_ids

This function now takes an additional parameter count, which specifies the number of TS_IDs to sample. It then samples count number of TS_IDs, converts them to a comma-separated string, and returns that string.

Now, in your test_get_ts_1 method, you can call set_ts_ids with the desired count:

@task(1)
def test_get_ts_1(self):
    ts_ids = set_ts_ids(count=10)
    resp = self.client.get(f'/api/loadValues?tsIds={ts_ids}&resolution=PT15M'
                           f'&startUtc={set_from_date()}&endUtc={set_to_date()}',
                           headers={'X-API-KEY': 'xxx'}, name='/api/loadvalues')

    print("Response status code: ", resp.status_code)
    # print("Response text: ", resp.text)

This will result in the test_get_ts_1 task calling set_ts_ids with a count of 10, getting 10 sampled TS_IDs, and then using them in the URL parameter. Adjust the count parameter as needed based on your requirements.

2023-11-24