Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 6C206200C61 for ; Tue, 25 Apr 2017 21:55:37 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 6AC31160BB3; Tue, 25 Apr 2017 19:55:37 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B154A160B8E for ; Tue, 25 Apr 2017 21:55:36 +0200 (CEST) Received: (qmail 73901 invoked by uid 500); 25 Apr 2017 19:55:35 -0000 Mailing-List: contact dev-help@jmeter.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jmeter.apache.org Delivered-To: mailing list dev@jmeter.apache.org Received: (qmail 73890 invoked by uid 99); 25 Apr 2017 19:55:35 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Apr 2017 19:55:35 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 868D2DFBDA; Tue, 25 Apr 2017 19:55:35 +0000 (UTC) From: pmouawad To: dev@jmeter.apache.org Reply-To: dev@jmeter.apache.org References: In-Reply-To: Subject: [GitHub] jmeter pull request #291: Add time shifting function Content-Type: text/plain Message-Id: <20170425195535.868D2DFBDA@git1-us-west.apache.org> Date: Tue, 25 Apr 2017 19:55:35 +0000 (UTC) archived-at: Tue, 25 Apr 2017 19:55:37 -0000 Github user pmouawad commented on a diff in the pull request: https://github.com/apache/jmeter/pull/291#discussion_r113294069 --- Diff: src/functions/org/apache/jmeter/functions/TimeShiftingFunction.java --- @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.jmeter.functions; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.Year; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoField; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.jmeter.engine.util.CompoundVariable; +import org.apache.jmeter.samplers.SampleResult; +import org.apache.jmeter.samplers.Sampler; +import org.apache.jmeter.threads.JMeterVariables; +import org.apache.jmeter.util.JMeterUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * timeShifting Function permit to shift a date + * + * Parameters: + * - format date @see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html (optional - defaults to epoch time in millisecond) + * - date to shift formated as first param (optional - defaults now) + * - amount of (seconds / minutes / hours / days / months ) to add (optional - default nothing is add ) + * - variable name ( optional ) + * + * Returns: + * - Returns a formated date with the specified number of (seconds / minutes / hours / days / months ) added. + * - value is also saved in the variable for later re-use. + * + * @since 3.3 + */ +public class TimeShiftingFunction extends AbstractFunction { + private static final Logger log = LoggerFactory.getLogger(TimeShiftingFunction.class); + + private static final String KEY = "__timeShifting"; // $NON-NLS-1$ + + private static final List desc = new LinkedList<>(); + + static { + desc.add(JMeterUtils.getResString("time_format_shift")); //$NON-NLS-1$ + desc.add(JMeterUtils.getResString("date_to_shift")); //$NON-NLS-1$ + desc.add(JMeterUtils.getResString("value_to_shift")); //$NON-NLS-1$ + desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$ + } + + // Ensure that these are set, even if no paramters are provided + private String format = ""; //$NON-NLS-1$ + private String dateToShift = ""; //$NON-NLS-1$ + private String shift = ""; //$NON-NLS-1$ + private String variable = ""; //$NON-NLS-1$ + + public TimeShiftingFunction() { + super(); + } + + /** {@inheritDoc} */ + @Override + public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { + String dateString; + LocalDateTime localDateTimeToShift = LocalDateTime.now( ZoneId.systemDefault() ); + DateTimeFormatter formatter = null; + if (!StringUtils.isEmpty(format)) { + try { + formatter = new DateTimeFormatterBuilder().appendPattern(format) + .parseDefaulting(ChronoField.NANO_OF_SECOND, 0) + .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0) + .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) + .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) + .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) + .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) + .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1) + .parseDefaulting(ChronoField.YEAR_OF_ERA, Year.now().getValue()) + .toFormatter(JMeterUtils.getLocale()); + } catch (IllegalArgumentException ex) { + log.error("Pattern is invalid", ex); // $NON-NLS-1$ + } + } + + if (!dateToShift.isEmpty()) { + try { + if (formatter != null) { + localDateTimeToShift = LocalDateTime.parse(dateToShift, formatter); + } else { + localDateTimeToShift = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)), ZoneId.systemDefault()); + } + } catch (DateTimeParseException | NumberFormatException ex) { + log.error("Failed to parse date to shift", ex); // $NON-NLS-1$ --- End diff -- Mention dateToShift --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---