# TPS (throughput) calculation

**URL:** <https://community.influxdata.com/t/tps-throughput-calculation/21817>\
**Category:** InfluxDB 2\
**Tags:** query, flux\
**Created:** [September 22, 2021, 4:42pm UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817 "2021-09-22T16:42:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Derevyanko](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/michael_derevyanko/32/4742_2.png) [@Michael\_Derevyanko](https://community.influxdata.com/u/Michael_Derevyanko)\
**Post date:** [September 22, 2021, 4:42pm UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/1 "2021-09-22T16:42:09Z")

</div>

Hi all,

I’m looking for the solution to calculate the **Throughput = (number of requests) / (total time).**

I have no issue with the calculation of the requests count

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/2/29c017e6d1bc1fb7f67657e1e9a4a8c31283fcb2.png)

But what is the solution to get the **total time** and put it into the target formula **Throughput = (number of requests) / (total time).**?

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/5/542fc021bc59ad26451d780dc88cf6925ffbc0cf.png)  
 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/10387f861482fc95afdb90d30309e3fa5312afcb.png)

It seems `duration` type has constraint → the String type only can be used as a field (had also issues to fetch the duration).

Very much appreciated your support, I will be able to move on with my public project.

---

<div class="post-metadata">

**Author:** ![FixTestRepeat](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/fixtestrepeat/32/5208_2.png) [@FixTestRepeat](https://community.influxdata.com/u/FixTestRepeat)\
**Post date:** [September 22, 2021, 10:42pm UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/2 "2021-09-22T22:42:55Z")

</div>

> [@Michael\_Derevyanko](#):
>
> It seems `duration` type has constraint

I don’t quite follow this point, but in any case to get tps it comes down to how accurate you need this info. The quick and dirty way is to window your data by 1 second intervals, then group and sum as you have. This page is quite long, but covers the entire process quite well.

> **[Window and aggregate data in InfluxDB with Flux | InfluxDB OSS v2 Documentation](https://docs.influxdata.com/influxdb/v2/query-data/flux/window-aggregate/)**
>
> This guide walks through windowing and aggregating data with Flux and outlines how it shapes your data in the process.

The harder and more accurate way is to calculate tps as a rolling second . If you go this route, if you are dealing with a lot of data, you probably won’t want to do this operation within a dashboard/query window, since the processing load will grow with number of users viewing the dashboard. Instead, do this process once via a task, and add the output as another measurement, or to another bucket even.

---

<div class="post-metadata">

**Author:** ![Michael\_Derevyanko](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/michael_derevyanko/32/4742_2.png) [@Michael\_Derevyanko](https://community.influxdata.com/u/Michael_Derevyanko)\
**Post date:** [September 23, 2021, 6:40am UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/3 "2021-09-23T06:40:52Z")

</div>

Thanks @FixTestRepeat let me describe with details.

I’m looking for the table like this, I do not need any charts

```auto
Request name | TPS | 
DELETE->Remove Specific Item | 10 |
GET->Get All Items | 20 |

```

Where `TPS = (number of requests) / (total time)`

number of requests → count of the items for the specific time range.  
total time → the duration in seconds for the specific time range

The specific time range can be

1. Raw total time. Why raw? → as a total time we are taking the 10 minutes (from user end it is difference between v.timeRangeStart and v.timeRangeStop)

For example in 10 minutes we have the data.

```auto
Request name | Number of requests | 
DELETE->Remove Specific Item | 650 |
GET->Get All Items | 1200 |

```

So I need the final result table with calculation

total time in seconds → 10 min \* 60 = 600 seconds

```auto
Request name | Number of requests/Total time | 
DELETE->Remove Specific Item | 650/600 |
GET->Get All Items | 1200/600 |

```

1. Exact total time. As a total time we are taking the difference between two date time points

`Total time = last request "_time" - first request "_time"`.

Further more, for both cases (1 and 2) total time we need to convert duration to seconds and then to int → to have the possibility to do math deviation.  
Any option (1 or 2) is acceptable.

So I do not see the huge complexity here at the first look. The problem that I’m not really understand now to implement the “total time” calculation and type conversion (duration to seconds to int) to have the proper math. One again, the goal is to have one query to build the table.

Hope now I described more clear, many thanks.

BR,  
Mike

---

<div class="post-metadata">

**Author:** ![FixTestRepeat](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/fixtestrepeat/32/5208_2.png) [@FixTestRepeat](https://community.influxdata.com/u/FixTestRepeat)\
**Post date:** [September 23, 2021, 8:15am UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/4 "2021-09-23T08:15:53Z")

</div>

If you’re looking more at TPS as some average over a longer period like 10mins, maybe take a look at this page

> **[Operate on timestamps with Flux | InfluxDB Cloud Documentation](https://docs.influxdata.com/influxdb/cloud/query-data/flux/manipulate-timestamps/#calculate-the-duration-between-two-timestamps)**
>
> Use Flux to operate on timestamps timestamps.

if I understand you I gather it’s not exactly 10mins or you would have just hard coded 600 in there already. So from the example, the time1 and time2 variables could either be input by you , or they could be determined by queries using first() and last() perhaps.

If you then skip step 3 which converts to a duration , then you’ll have a difference between the two Unix seconds timestamps. That should the plug-in to youre existing code? (After converting from nanoseconds to seconds)

```auto
time1 = uint(v: 2019-09-17T21:12:05Z)
time2 = uint(v: 2019-09-18T22:16:35Z)
TotalTime = time2 - time1

```

---

<div class="post-metadata">

**Author:** ![Michael\_Derevyanko](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/michael_derevyanko/32/4742_2.png) [@Michael\_Derevyanko](https://community.influxdata.com/u/Michael_Derevyanko)\
**Post date:** [September 23, 2021, 9:25am UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/5 "2021-09-23T09:25:31Z")

</div>

Thanks I have read this one [https://docs.influxdata.com/influxdb/cloud/query-data/flux/manipulate-timestamps/#calculate-the-duration-between-two-timestamps](https://docs.influxdata.com/influxdb/cloud/query-data/flux/manipulate-timestamps/#calculate-the-duration-between-two-timestamps) before this topic creation.

I understood that to have a 10 minutes in nanoseconds at least (but I need in seconds) I need to

```auto
time1 = uint(v: v.timeRangeStart)
time2 = uint(v: v.timeRangeStop)

TotalTime = time2 - time1

```

OK if we taking option “1. Raw total time” to take average TPS.

I know how to get count. Then we can play with “map” solution and do the math … or “map” with “join” if we can have calculated the raw TotalTime.

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/3/3057e7f351fd05a26aacf6b59f0d60e51851d294.png)

Can you give me advise/example how query should look like at least to execute the code below and get TotalTime with type which is supporting the division?

---

<div class="post-metadata">

**Author:** ![Michael\_Derevyanko](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/michael_derevyanko/32/4742_2.png) [@Michael\_Derevyanko](https://community.influxdata.com/u/Michael_Derevyanko)\
**Post date:** [September 23, 2021, 2:35pm UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/6 "2021-09-23T14:35:41Z")

</div>

Hey @FixTestRepeat,

Finally, I have made the query

```auto
tpsCalculator = () => {

  count = from(bucket: v.bucket)

  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)

  |> filter(fn: (r) => r["_measurement"] == "requestsRaw")

  |> filter(fn: (r) => r["_field"] == "count")

  |> group(columns: ["requestName"])

  |> count()

  |> toFloat()

  |> group()

  |> keep(columns: ["_value", "requestName"])

  

first = from(bucket: v.bucket)

  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)

  |> filter(fn: (r) => r["_measurement"] == "requestsRaw")

  |> filter(fn: (r) => r["_field"] == "count")

  |> group(columns: ["requestName"])

  |> first()

  |> toFloat()

  |> group()

  |> keep(columns: ["_time", "requestName"])

 last = from(bucket: v.bucket)

  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)

  |> filter(fn: (r) => r["_measurement"] == "requestsRaw")

  |> filter(fn: (r) => r["_field"] == "count")

  |> group(columns: ["requestName"])

  |> last()

  |> toFloat()

  |> group()

  |> keep(columns: ["_time", "requestName"])

  time = join(

      tables:{first:first, last:last},

      on:["requestName"]

    )

    |> map(fn:(r) => ({

             requestName: r.requestName,

             duration: float(v:(int(v:(uint(v: r._time_last) - uint(v: r._time_first))))/1000000000)

                                

    }))   

     return join(

      tables:{time:time, count:count},

      on:["requestName"]

    )

    |> map(fn:(r) => ({

             requestName: r.requestName,

             TPS: float(v: (r._value/r.duration))

                                

    }))   

}

 tpsCalculator()

```

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/5/50ab84192624944f564e37adb94b9810656a0d60.png)

Thanks for the help anyway.  
BR,  
Mike

---

<div class="post-metadata">

**Author:** ![FixTestRepeat](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/fixtestrepeat/32/5208_2.png) [@FixTestRepeat](https://community.influxdata.com/u/FixTestRepeat)\
**Post date:** [September 24, 2021, 10:17pm UTC](https://community.influxdata.com/t/tps-throughput-calculation/21817/7 "2021-09-24T22:17:10Z")

</div>

Nice one, and thanks for sharing your final result there. Just curious, what your use case is for this function? Are you calling it from grafana or another external script?
