Skip to content

Commit 4b3260c

Browse files
committed
- fixed reserve with timeout
- added a simple bulk perf test
1 parent 742d5b6 commit 4b3260c

File tree

5 files changed

+114
-8
lines changed

5 files changed

+114
-8
lines changed

Beanstalk.Client.IntegrationTest/Beanstalk.Client.IntegrationTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
</ItemGroup>
5252
<ItemGroup>
5353
<Compile Include="BeanstalkClientTests.cs" />
54+
<Compile Include="PerformanceTests.cs" />
5455
<Compile Include="Properties\AssemblyInfo.cs" />
5556
<Compile Include="TestConfig.cs" />
5657
</ItemGroup>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Diagnostics;
24+
using System.IO;
25+
using Droog.Beanstalk.Client.Test;
26+
using NUnit.Framework;
27+
28+
namespace Droog.Beanstalk.Client.IntegrationTest {
29+
30+
[Ignore("Performance tests, run by hand")]
31+
[TestFixture]
32+
public class PerformanceTests {
33+
34+
[Test]
35+
public void Sequential_bulk_put_reserve_delete() {
36+
var n = 10000;
37+
var data = new List<MemoryStream>();
38+
for(var i = 0; i < n; i++) {
39+
data.Add(("data-" + i).AsStream());
40+
}
41+
using(var client = CreateClient()) {
42+
var stopwatch = Stopwatch.StartNew();
43+
foreach(var item in data) {
44+
var put = client.Put(100, TimeSpan.Zero, TimeSpan.FromMinutes(2), item, item.Length);
45+
}
46+
stopwatch.Stop();
47+
Console.WriteLine("put: {0:0} items/sec", n / stopwatch.Elapsed.TotalSeconds);
48+
49+
var jobs = new List<Job>();
50+
stopwatch = Stopwatch.StartNew();
51+
while(true) {
52+
try {
53+
var job = client.Reserve(TimeSpan.Zero);
54+
jobs.Add(job);
55+
} catch(TimedoutException) {
56+
break;
57+
}
58+
}
59+
stopwatch.Stop();
60+
Console.WriteLine("reserve: {0:0} items/sec", n / stopwatch.Elapsed.TotalSeconds);
61+
Assert.AreEqual(data.Count, jobs.Count);
62+
for(var i = 0; i < n; i++) {
63+
Assert.AreEqual(data[i].AsText(), jobs[i].Data.AsText());
64+
}
65+
66+
stopwatch = Stopwatch.StartNew();
67+
foreach(var job in jobs) {
68+
Assert.IsTrue(client.Delete(job.Id));
69+
}
70+
stopwatch.Stop();
71+
Console.WriteLine("delete: {0:0} items/sec", n / stopwatch.Elapsed.TotalSeconds);
72+
}
73+
}
74+
private BeanstalkClient CreateClient() {
75+
return new BeanstalkClient(TestConfig.Host, TestConfig.Port);
76+
}
77+
}
78+
}

Beanstalk.Client.Test/BeanstalkProtocolTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ public void Can_reserve_without_timeout() {
9595

9696
[Test]
9797
public void Can_reserve_with_timeout() {
98-
_mockSocket.Expect("reserve 10\r\n", "RESERVED 123 3\r\nbar\r\n");
98+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "RESERVED 123 3\r\nbar\r\n");
99+
var job = _client.Reserve(TimeSpan.FromSeconds(10));
100+
_mockSocket.Verify();
101+
Assert.AreEqual(123, job.Id);
102+
Assert.AreEqual("bar", job.Data.AsText());
103+
}
104+
105+
[Test]
106+
public void Can_reserve_with_zero_timeout() {
107+
_mockSocket.Expect("reserve-with-timeout 0\r\n", "RESERVED 123 3\r\nbar\r\n");
99108
var job = _client.Reserve(TimeSpan.FromSeconds(10));
100109
_mockSocket.Verify();
101110
Assert.AreEqual(123, job.Id);
@@ -104,7 +113,7 @@ public void Can_reserve_with_timeout() {
104113

105114
[Test]
106115
public void Can_reserve_with_timeout_throwing_deadline_soon() {
107-
_mockSocket.Expect("reserve 10\r\n", "DEADLINE_SOON\r\n");
116+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "DEADLINE_SOON\r\n");
108117
try {
109118
_client.Reserve(TimeSpan.FromSeconds(10));
110119
Assert.Fail("didn't throw deadline soon");
@@ -118,7 +127,7 @@ public void Can_reserve_with_timeout_throwing_deadline_soon() {
118127

119128
[Test]
120129
public void Can_reserve_with_timeout_timing_out() {
121-
_mockSocket.Expect("reserve 10\r\n", "TIMED_OUT\r\n");
130+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "TIMED_OUT\r\n");
122131
try {
123132
_client.Reserve(TimeSpan.FromSeconds(10));
124133
Assert.Fail("didn't time out");
@@ -132,7 +141,7 @@ public void Can_reserve_with_timeout_timing_out() {
132141

133142
[Test]
134143
public void Can_TryReserve_with_timeout() {
135-
_mockSocket.Expect("reserve 10\r\n", "RESERVED 123 3\r\nbar\r\n");
144+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "RESERVED 123 3\r\nbar\r\n");
136145
Job job;
137146
Assert.AreEqual(ReservationStatus.Reserved, _client.TryReserve(TimeSpan.FromSeconds(10), out job));
138147
_mockSocket.Verify();
@@ -142,7 +151,7 @@ public void Can_TryReserve_with_timeout() {
142151

143152
[Test]
144153
public void Can_TryReserve_with_timeout_timing_out() {
145-
_mockSocket.Expect("reserve 10\r\n", "TIMED_OUT\r\n");
154+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "TIMED_OUT\r\n");
146155
Job job;
147156
Assert.AreEqual(ReservationStatus.TimedOut, _client.TryReserve(TimeSpan.FromSeconds(10), out job));
148157
_mockSocket.Verify();
@@ -151,7 +160,7 @@ public void Can_TryReserve_with_timeout_timing_out() {
151160

152161
[Test]
153162
public void Can_TryReserve_with_timeout_throwing_deadline_soon() {
154-
_mockSocket.Expect("reserve 10\r\n", "DEADLINE_SOON\r\n");
163+
_mockSocket.Expect("reserve-with-timeout 10\r\n", "DEADLINE_SOON\r\n");
155164
Job job;
156165
Assert.AreEqual(ReservationStatus.DeadlineSoon, _client.TryReserve(TimeSpan.FromSeconds(10), out job));
157166
_mockSocket.Verify();

Beanstalk.Client.Test/MockSocket.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
119
using System;
220
using System.IO;
321
using Droog.Beanstalk.Client.Protocol;

Beanstalk.Client/BeanstalkClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Job Reserve() {
158158
}
159159

160160
public Job Reserve(TimeSpan timeout) {
161-
var response = Exec(Request.Create(RequestCommand.Reserve)
161+
var response = Exec(Request.Create(RequestCommand.ReserveWithTimeout)
162162
.AppendArgument(timeout)
163163
.ExpectStatuses(ResponseStatus.DeadlineSoon | ResponseStatus.TimedOut | ResponseStatus.Reserved));
164164
switch(response.Status) {
@@ -173,7 +173,7 @@ public Job Reserve(TimeSpan timeout) {
173173
}
174174

175175
public ReservationStatus TryReserve(TimeSpan timeout, out Job job) {
176-
var response = Exec(Request.Create(RequestCommand.Reserve)
176+
var response = Exec(Request.Create(RequestCommand.ReserveWithTimeout)
177177
.AppendArgument(timeout)
178178
.ExpectStatuses(ResponseStatus.DeadlineSoon | ResponseStatus.TimedOut | ResponseStatus.Reserved));
179179
switch(response.Status) {

0 commit comments

Comments
 (0)