我创建了一个名为test123的项目,其中包括settings.py和urls.py:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'dbuser.cnf'),
}
},
}
root@cdbe25bac912:~/test123/test123# cat urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^myapp/', include('myapp.urls')),
]
我创建了一个名为myapp的应用程序。
root@cdbe25bac912:~/test123/myapp# cat models.py
from django.db import models
class Package(models.Model):
name = models.CharField(max_length=50)
desc = models.CharField(max_length=50)
root@cdbe25bac912:~/test123/myapp# cat serializers.py
from myapp.models import *
from rest_framework import serializers
class PackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
fields = ( 'id', 'name', 'desc',)
root@cdbe25bac912:~/test123/myapp# cat views.py
from django.shortcuts import render
from myapp.models import *
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response
from myapp.serializers import *
class PackageViewList(APIView):
def get(self, request, format=None):
package = Package.objects.all()
serializer = PackageSerializer(package, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = PackageSerializer(data = request.data)
if serializer.is_valid():
print serializer.validated_data
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
root@cdbe25bac912:~/test123/myapp# cat urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^package/$', views.PackageViewList.as_view()),
]
root@cdbe25bac912:~/test123/myapp#
以下是使用Django Test Framework编写的测试脚本,它创建了多个APIClient对象来模拟多个用户并从这些对象中激活POST操作。
root@cdbe25bac912:~/test123/myapp# cat tests.py.bkup
from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework.test import APITestCase, APILiveServerTestCase
from myapp.models import Package
from multiprocessing import Process
class ConcurrentTest(APILiveServerTestCase):
def setUp(self):
self.apiclient_list = []
self.num_clients=4
package = Package.objects.create(name='pack333', desc='package333')
package.save()
self.pack = {"name" : "pack", "desc" : "package"}
#create number of APIClient objects and login.
for client_id in range(self.num_clients):
apiclient = APIClient()
self.apiclient_list.append(apiclient)
def post(self, apiclient, client_id, url, data):
print 'Started POST Client ID = %s' % (str(client_id))
data['name'] = 'packpost' + str(client_id)
data['desc'] = 'packdesc' + str(client_id)
print data
response = apiclient.post(url, data, format="json")
self.assertEqual(response.status_code, 201)
print 'Completed POST Client ID = %s' % (str(client_id))
def test_concurrent_restops(self):
"""
Description : Simulate multiple users and issue concurrent REST operations
"""
process_list = []
#Issue concurrent POST operations.
for client_id in range(len(self.apiclient_list)):
t = Process(target=self.post, args=(self.apiclient_list[client_id], client_id, '/myapp/package/', self.pack))
process_list.append(t)
for process in process_list :
process.start()
for process in process_list :
process.join()
根@ cdbe25bac912:〜/ test123 / MyApp的#
当我将上述测试问题运行为:python manage.py test myapp
test.py
中创建的至少一个进程将挂起。我假设Django和MySQL需要一些配置来支持来自不同用户的并发操作。
答案 0 :(得分:1)
看起来public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager locationManager;
private LocationListener locationListener;
private double latitude, longitude, flat, flong;
private String friendName;
private boolean addMarker = false;
private Connection connection;
private Button btnAddMyMarker, btnAddFriendMarker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
initialize();
setListener();
}
public void initialize() {
btnAddMyMarker = (Button)findViewById(R.id.btnAddMyMarker);
btnAddMyMarker.setEnabled(false);
btnAddFriendMarker = (Button)findViewById(R.id.btnAddFriendMarker);
connection = (Connection) getIntent().getSerializableExtra("class");
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocList();
}
public void setListener() {
btnAddMyMarker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addMyMarker();
}
});
btnAddFriendMarker.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
connection.listenToServer();
}
});
}
public void addMyMarker() {
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My position"));
}
private class LocList implements LocationListener {
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
if(addMarker == false && longitude != 0 && latitude != 0) {
btnAddMyMarker.setEnabled(true);
}
connection.sendLocation(latitude, longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
是单线程的,这可以解释为什么进程/请求会挂起,因为服务器一次只能处理一个请求:
我能想到为什么你没有看到使用sqlite进行阻塞的一件事是因为sqlite在内存中运行django unittest,大大提高了测试运行的速度。
Django LiveServerTestCase
是一种“玩具”,可能不太适合负载样式测试。 MySQL应该能够轻松地处理开箱即用的高级并发性,但我不确定LiveServerTestServer
可以。
如果您针对LiveServerTestServer
运行代码,现在是多线程的(我相信),您应该注意到有所不同。为了了解您的应用程序如何处理负载,运行您的代码可能是一个好主意,因为它将被部署(使用您的prod webserver:apache,uwsgi,gunicorn等等。)