<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to group and calculate the program execute time in group? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220386#M188094</link>
    <description>&lt;P&gt;IF and only if they're always in order like that, you could use &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Streamstats"&gt;streamstats&lt;/A&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... my root search ... | rex field=_raw "^(?&amp;lt;datestring&amp;gt;\S+\s+\S+*)\s+(?&amp;lt;controlpoint&amp;gt;\S+"
| streamstats window=2 first(controlpoint) as portion, first(datestring) as start, last(datestring) as end
| table controlpoint, start, end
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Obviously this isn't well tested.  I think the rex should be right but if it isn't just post back.  &lt;/P&gt;

&lt;P&gt;The big drawback to this method is it'll treat those dates as strings.  If you want to do math on them, and if they're the _time field (e.g. they're already extracted as the timestamp of the event), you could just ADD to the above...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... my root search ... | rex field=_raw "^(?&amp;lt;datestring&amp;gt;\S+\s+\S+*)\s+(?&amp;lt;controlpoint&amp;gt;\S+"
| streamstats window=2 first(controlpoint) as portion, first(datestring) as start, last(datestring) as end
  first(_time) as starttime, last(_time) as endtime
| eval elapsed=starttime-endtime
| table controlpoint, start, end, elapsed
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, obviously, even LESS tested, but now it should show the elapsed time between each subsequent set of events.  As always when I deal with streamstats, I'm positive I've gotten the first and last backwards so double-check all that.  Probably did my math backwards then too.  &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 22 Apr 2016 12:33:55 GMT</pubDate>
    <dc:creator>Richfez</dc:creator>
    <dc:date>2016-04-22T12:33:55Z</dc:date>
    <item>
      <title>How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220385#M188093</link>
      <description>&lt;P&gt;Hi expert, currently I am study Splunk and have some question, could you help me to resolve them? Thank you in advance.&lt;/P&gt;

&lt;P&gt;22-Apr-2016 12:04:56.213   &lt;STRONG&gt;start-1&lt;/STRONG&gt;&lt;BR /&gt;
22-Apr-2016 12:04:57.228     -exec_1&lt;BR /&gt;
22-Apr-2016 12:04:58.123     -exec_2&lt;BR /&gt;
22-Apr-2016 12:04:59.957     -exec_3&lt;BR /&gt;
22-Apr-2016 12:05:00.854     -exec_4&lt;BR /&gt;
22-Apr-2016 12:05:01.364     -exec_5&lt;BR /&gt;
22-Apr-2016 12:05:02.951   &lt;STRONG&gt;end-1&lt;/STRONG&gt;&lt;BR /&gt;
22-Apr-2016 12:05:03.665   &lt;STRONG&gt;start-2&lt;/STRONG&gt;&lt;BR /&gt;
22-Apr-2016 12:05:04.751     -exec_1&lt;BR /&gt;
22-Apr-2016 12:05:05.598     -exec_2&lt;BR /&gt;
22-Apr-2016 12:05:06.165   &lt;STRONG&gt;end-2&lt;/STRONG&gt;&lt;BR /&gt;
22-Apr-2016 12:05:07.977   &lt;STRONG&gt;start-1&lt;/STRONG&gt;&lt;BR /&gt;
22-Apr-2016 12:05:08.315     -exec_1&lt;BR /&gt;
22-Apr-2016 12:05:09.671     -exec_2&lt;BR /&gt;
22-Apr-2016 12:05:10.139   &lt;STRONG&gt;end-1&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;As above example, I want to calculate only between &lt;STRONG&gt;start-1&lt;/STRONG&gt; and &lt;STRONG&gt;end-1&lt;/STRONG&gt; all -exec_*  (*=1, 2, 3, ...) execute time, how can I do?&lt;BR /&gt;
Currently I know can use '&lt;EM&gt;transaction source startswith="start-1" endswith="end-1"&lt;/EM&gt;' to group them, but I don't know how to calculate the all -exec_*  (*=1, 2, 3, ...) execute time?&lt;/P&gt;

&lt;P&gt;I want to the result like:&lt;BR /&gt;
-exec_1    0.895  (22-Apr-2016 12:04:58.123 - 22-Apr-2016 12:04:57.228)&lt;BR /&gt;
-exec_1    1.356  (22-Apr-2016 12:05:09.671 - 22-Apr-2016 12:05:08.315)&lt;BR /&gt;
-exec_2    1.834  (22-Apr-2016 12:04:59.957 - 22-Apr-2016 12:04:58.123)&lt;BR /&gt;
-exec_2    0.468  (22-Apr-2016 12:05:10.139 - 22-Apr-2016 12:05:09.671)&lt;BR /&gt;
-exec_3  0.897  (22-Apr-2016 12:05:00.854 - 22-Apr-2016 12:04:59.957)&lt;BR /&gt;
-exec_4  0.510  (22-Apr-2016 12:05:01.364 - 22-Apr-2016 12:05:00.854)&lt;BR /&gt;
-exec_5  1.587  (22-Apr-2016 12:05:02.951 - 22-Apr-2016 12:05:01.364)&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 09:32:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220385#M188093</guid>
      <dc:creator>blueyuan</dc:creator>
      <dc:date>2020-09-29T09:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220386#M188094</link>
      <description>&lt;P&gt;IF and only if they're always in order like that, you could use &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Streamstats"&gt;streamstats&lt;/A&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... my root search ... | rex field=_raw "^(?&amp;lt;datestring&amp;gt;\S+\s+\S+*)\s+(?&amp;lt;controlpoint&amp;gt;\S+"
| streamstats window=2 first(controlpoint) as portion, first(datestring) as start, last(datestring) as end
| table controlpoint, start, end
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Obviously this isn't well tested.  I think the rex should be right but if it isn't just post back.  &lt;/P&gt;

&lt;P&gt;The big drawback to this method is it'll treat those dates as strings.  If you want to do math on them, and if they're the _time field (e.g. they're already extracted as the timestamp of the event), you could just ADD to the above...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... my root search ... | rex field=_raw "^(?&amp;lt;datestring&amp;gt;\S+\s+\S+*)\s+(?&amp;lt;controlpoint&amp;gt;\S+"
| streamstats window=2 first(controlpoint) as portion, first(datestring) as start, last(datestring) as end
  first(_time) as starttime, last(_time) as endtime
| eval elapsed=starttime-endtime
| table controlpoint, start, end, elapsed
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, obviously, even LESS tested, but now it should show the elapsed time between each subsequent set of events.  As always when I deal with streamstats, I'm positive I've gotten the first and last backwards so double-check all that.  Probably did my math backwards then too.  &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 12:33:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220386#M188094</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-04-22T12:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220387#M188095</link>
      <description>&lt;P&gt;Thank you for your help, but I still can't catch, could you please explain more detail? thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 09:14:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220387#M188095</guid>
      <dc:creator>blueyuan</dc:creator>
      <dc:date>2016-04-25T09:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220388#M188096</link>
      <description>&lt;P&gt;First off, though - does it work and solve your problem?&lt;/P&gt;

&lt;P&gt;Some explanations.&lt;/P&gt;

&lt;P&gt;The rex uses a regular expression to create to fields, one called "datestring" which is two sets of characters ( &lt;CODE&gt;\S+&lt;/CODE&gt; which means non-space) separated by a space ( &lt;CODE&gt;\s+&lt;/CODE&gt; ) (like "22-Apr-2016 12:05:04.751") and another called "controlpoint" which is a bunch of non-space characters ( &lt;CODE&gt;\S+&lt;/CODE&gt; ).&lt;/P&gt;

&lt;P&gt;For more on rex and regular expressions, along with some links of things to learn more, see the &lt;STRONG&gt;comment&lt;/STRONG&gt; on the answer I wrote on the question &lt;A href="https://answers.splunk.com/answers/395336/how-to-extract-a-value-of-a-field-when-the-field-c.html#answer-395372"&gt;How to extract a value of a field, when the field contains quotes(") Inside?&lt;/A&gt;.&lt;/P&gt;

&lt;P&gt;The easiest way to think of streamstats is to think of a rolling window.  As events stream by in the search results (first event, second event, third event and so on), streamstats groups each of these with previous ones.  In this case &lt;CODE&gt;window=2&lt;/CODE&gt; so it groups the current event it's looking at with the previous event it just had.  Setting &lt;CODE&gt;window=8&lt;/CODE&gt; would do 8 at a time.  As it does so, it can calculate various statistics on those 2 or 8 or whatever.  If you did an average, you'd have a running average of the past few events.  I'm not doing that style of work with streamstats but am instead just grabbing values. First() and last() grab whichever as appropriate.  You can read &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Streamstats"&gt;about streamstats&lt;/A&gt; (especially check the examples) or about the &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.4.0/SearchReference/CommonStatsFunctions"&gt;sorts of calculations it can do&lt;/A&gt; in the docs. &lt;/P&gt;

&lt;P&gt;The eval statement created a new field elapsed, which is your starttime minus the endtime.  Starttime being the first _time of each pair of events, endtime being the last _time of the pair, hence giving you an elapsed between each set of events.  (Or they're backwards, again you may need to adjust because I &lt;EM&gt;always&lt;/EM&gt; get those wrong in my head...)&lt;/P&gt;

&lt;P&gt;Then I just added elapsed to your table.&lt;/P&gt;

&lt;P&gt;Does that help?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2016 11:53:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220388#M188096</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-04-25T11:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220389#M188097</link>
      <description>&lt;P&gt;Thank you very much, I will try it again.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2016 06:15:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220389#M188097</guid>
      <dc:creator>blueyuan</dc:creator>
      <dc:date>2016-04-27T06:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220390#M188098</link>
      <description>&lt;P&gt;When I try this but show error message:&lt;BR /&gt;
⚠ Error in 'rex' command: Encountered the following error while compiling the regex '^(?\S+\s+\S+*)\s+(?\S+': Regex: nothing to repeat.&lt;BR /&gt;
How should I fix it?  Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2016 09:12:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220390#M188098</guid>
      <dc:creator>blueyuan</dc:creator>
      <dc:date>2016-04-27T09:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to group and calculate the program execute time in group?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220391#M188099</link>
      <description>&lt;P&gt;I can't tell what rex you used.  Can you please be sure to use the code button (101010) in the mini-editor in these text boxes, then paste your code there?  &lt;CODE&gt;"^(?&amp;lt;datestring&amp;gt;\S+\s+\S+*)\s+(?&amp;lt;controlpoint&amp;gt;\S+"&lt;/CODE&gt; is what you should have.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Apr 2016 11:31:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-group-and-calculate-the-program-execute-time-in-group/m-p/220391#M188099</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2016-04-27T11:31:18Z</dc:date>
    </item>
  </channel>
</rss>

