Cannot add to context after yield return
I want to get a timestamp before and after a request, and send it to the context to look at.
I set up a coded webtest, then generated a web request then added (at the end)...DateTime current = DateTime.Now;
this.Context.Add("TimeStarted", current.ToString("yyyy-MM-dd HH:mm:ss.fff"));
this.BeginTransaction("TotalRequestTime");
yield return request1;
DateTime current2 = DateTime.Now;
this.Context.Add("TimeEnded", current2.ToString("yyyy-MM-dd HH:mm:ss.fff"));
request1 = null;
this.EndTransaction("TotalRequestTime");However, when I run, only the TimeStarted gets added to the context (when I view results of the webtest and go to the context tab), TimeEnded does not show up
Q1: Is the yield return request1 when the actual request goes to the server?
Q2: Is there a better way of grabbing times when a request is made and returned, the results only show duration (like the transaction time will only show up in a load test and seems to be the average duration of the requests)
Q3: I know I can data bind and grab values from a csv file, but can I output to a csv file during a load test?-NN
Answers
- You could do it with a PostRequest event. Do request1.PostRequest+= <postr request handler> In that method you can add to context.
Blog - http://blogs.msdn.com/slumley/default.aspx- Marked As Answer byslumley MSFTMSFT, ModeratorThursday, November 05, 2009 3:14 PM
All Replies
- Hi NN
As I don't see any iterator in your code, is there any reason that you used yield return in the code?
From the code showed, it seems that the yield return is the cause of not getting the "TimeEnded".
I think it is better for us to understand if we can see the whole code.
In VS2010 for saving and reviewing the log of a load test, there are 2 features should help. One is the ability to capture full test logs: http://blogs.msdn.com/billbar/archive/2009/06/09/vsts-2010-load-test-feature-saving-test-logs.aspx
The other is a view which shows you what each individual user is doing during a test. Check out this blog for how this can help you out: http://blogs.msdn.com/edglas/archive/2009/10/19/using-the-virtual-user-activity-chart-to-understand-the-vs-load-engine.aspx
Although there is no such a feature sweet to do like this in VS2008, we can do some additional work in VS2008 to add console output to load tests running unit tests, see: http://blogs.msdn.com/billbar/pages/adding-console-output-to-load-tests-running-unit-tests.aspx for more details.
If there is any misunderstanding, please let me know.
Thanks.
Figo Fei
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. I did the web service call through a webtest and auto generated it.
Here is the complete code:
namespace Testing.TestProject
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;
[DeploymentItem("testing.testproject\\Users1.csv", "testing.testproject")]
[DataSource("DataSource1", "Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\testing.testproject\\Users1.csv", Microsoft.VisualStudio.TestTools.WebTesting.DataBindingAccessMethod.Unique, "Users1#csv")]
[DataBinding("DataSource1", "Users1#csv", "UserID", "DataSource1.Users1#csv.UserID")]
public class RequestFunctionCoded : WebTest
{public RequestFunctionCoded()
{
this.PreAuthenticate = true;
this.StopOnError = true;
}public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
// This will set up the client order ID and external candidate ID
string ClientOrderID = System.Guid.NewGuid().ToString();
string ExtCandID = System.Guid.NewGuid().ToString();// Set up the name
string index1 = this.Context["DataSource1.Users1#csv.UserID"].ToString();
string name = "test_";
string CurTime = DateTime.Now.ToString("MMddHHmmffff");
name += index1 + "_" + CurTime.ToString();// Set up client info
string projectKey = "2DCE0F3C-1136-47C2-B19C-CDE674148850";
string userNameIP = "testUser";
string passWordIP = "testPass";
string clientKey = "MyKey";
string AssessID = "1234";
WebTestRequest request1 = new WebTestRequest("http://localhost:50334/MyService.asmx");
request1.Method = "POST";
request1.Encoding = System.Text.Encoding.GetEncoding("utf-8");
request1.Headers.Add(new WebTestRequestHeader("SOAPAction", "\"http://ns.aURL.com/RequestFunction\""));
request1.QueryStringParameters.Add("CallMethod", "RequestFunction");
StringHttpBody request1Body = new StringHttpBody();
request1Body.ContentType = "text/xml";
request1Body.InsertByteOrderMark = false;
request1Body.BodyString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas" +
".xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instanc" +
"e\" xmlns:xsd=\http://www.w3.org/2001/XMLSchema\">\r\n<soap:Body>\r\n<RequestFunction"
+ " BODY CODE "
+ "\r\n</RequestFunction>\r\n</soap:Body"
+ ">\r\n</soap:Envelope>";request1.Body = request1Body;
DateTime current = DateTime.Now;
this.Context.Add("TimeStarted", current.ToString("yyyy-MM-dd HH:mm:ss.fff"));
this.BeginTransaction("TotalRequestTime");
yield return request1;
DateTime current2 = DateTime.Now;
this.Context.Add("TimeEnded", current2.ToString("yyyy-MM-dd HH:mm:ss.fff"));
request1 = null;
this.EndTransaction("TotalRequestTime");}
}
}- You could do it with a PostRequest event. Do request1.PostRequest+= <postr request handler> In that method you can add to context.
Blog - http://blogs.msdn.com/slumley/default.aspx- Marked As Answer byslumley MSFTMSFT, ModeratorThursday, November 05, 2009 3:14 PM


